| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | package config | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-09-12 01:21:43 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2020-01-03 23:45:26 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 	"syscall" | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/minio/minio/pkg/color" | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | // Err is a structure which contains all information
 | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | // to print a fatal error message in json or pretty mode
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | // Err implements error so we can use it anywhere
 | 
					
						
							|  |  |  | type Err struct { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	msg    string | 
					
						
							|  |  |  | 	detail string | 
					
						
							|  |  |  | 	action string | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 	hint   string | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 14:05:54 +08:00
										 |  |  | // Clone returns a new Err struct with the same information
 | 
					
						
							|  |  |  | func (u Err) Clone() Err { | 
					
						
							|  |  |  | 	return Err{ | 
					
						
							|  |  |  | 		msg:    u.msg, | 
					
						
							|  |  |  | 		detail: u.detail, | 
					
						
							|  |  |  | 		action: u.action, | 
					
						
							|  |  |  | 		hint:   u.hint, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 07:19:29 +08:00
										 |  |  | // Error returns the error message
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | func (u Err) Error() string { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	if u.detail == "" { | 
					
						
							| 
									
										
										
										
											2019-12-24 08:31:03 +08:00
										 |  |  | 		if u.msg != "" { | 
					
						
							|  |  |  | 			return u.msg | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return "<nil>" | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return u.detail | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | // Msg - Replace the current error's message
 | 
					
						
							|  |  |  | func (u Err) Msg(m string, args ...interface{}) Err { | 
					
						
							| 
									
										
										
										
											2019-12-26 14:05:54 +08:00
										 |  |  | 	e := u.Clone() | 
					
						
							|  |  |  | 	e.msg = fmt.Sprintf(m, args...) | 
					
						
							|  |  |  | 	return e | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Hint - Replace the current error's message
 | 
					
						
							|  |  |  | func (u Err) Hint(m string, args ...interface{}) Err { | 
					
						
							|  |  |  | 	e := u.Clone() | 
					
						
							|  |  |  | 	e.hint = fmt.Sprintf(m, args...) | 
					
						
							|  |  |  | 	return e | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | // ErrFn function wrapper
 | 
					
						
							|  |  |  | type ErrFn func(err error) Err | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Create a UI error generator, this is needed to simplify
 | 
					
						
							|  |  |  | // the update of the detailed error message in several places
 | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | // in MinIO code
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | func newErrFn(msg, action, hint string) ErrFn { | 
					
						
							|  |  |  | 	return func(err error) Err { | 
					
						
							|  |  |  | 		u := Err{ | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 			msg:    msg, | 
					
						
							|  |  |  | 			action: action, | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 			hint:   hint, | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			u.detail = err.Error() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return u | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | // ErrorToErr inspects the passed error and transforms it
 | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | // to the appropriate UI error.
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | func ErrorToErr(err error) Err { | 
					
						
							| 
									
										
										
										
											2019-12-24 08:31:03 +08:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		return Err{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 	// If this is already a Err, do nothing
 | 
					
						
							|  |  |  | 	if e, ok := err.(Err); ok { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 		return e | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Show a generic message for known golang errors
 | 
					
						
							| 
									
										
										
										
											2019-09-12 01:21:43 +08:00
										 |  |  | 	if errors.Is(err, syscall.EADDRINUSE) { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return ErrPortAlreadyInUse(err).Msg("Specified port is already in use") | 
					
						
							| 
									
										
										
										
											2020-01-03 23:45:26 +08:00
										 |  |  | 	} else if errors.Is(err, syscall.EACCES) || errors.Is(err, syscall.EPERM) { | 
					
						
							|  |  |  | 		switch err.(type) { | 
					
						
							|  |  |  | 		case *net.OpError: | 
					
						
							|  |  |  | 			return ErrPortAccess(err).Msg("Insufficient permissions to use specified port") | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return ErrNoPermissionsToAccessDirFiles(err).Msg("Insufficient permissions to access path") | 
					
						
							| 
									
										
										
										
											2019-09-12 01:21:43 +08:00
										 |  |  | 	} else if errors.Is(err, io.ErrUnexpectedEOF) { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return ErrUnexpectedDataContent(err) | 
					
						
							| 
									
										
										
										
											2019-09-12 01:21:43 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 		// Failed to identify what type of error this, return a simple UI error
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return Err{msg: err.Error()} | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | // FmtError converts a fatal error message to a more clear error
 | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | // using some colors
 | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | func FmtError(introMsg string, err error, jsonFlag bool) string { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	renderedTxt := "" | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 	uiErr := ErrorToErr(err) | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	// JSON print
 | 
					
						
							|  |  |  | 	if jsonFlag { | 
					
						
							|  |  |  | 		// Message text in json should be simple
 | 
					
						
							|  |  |  | 		if uiErr.detail != "" { | 
					
						
							|  |  |  | 			return uiErr.msg + ": " + uiErr.detail | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return uiErr.msg | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Pretty print error message
 | 
					
						
							|  |  |  | 	introMsg += ": " | 
					
						
							|  |  |  | 	if uiErr.msg != "" { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		introMsg += color.Bold(uiErr.msg) | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		introMsg += color.Bold(err.Error()) | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 	renderedTxt += color.Red(introMsg) + "\n" | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	// Add action message
 | 
					
						
							|  |  |  | 	if uiErr.action != "" { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		renderedTxt += "> " + color.BgYellow(color.Black(uiErr.action)) + "\n" | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 	// Add hint
 | 
					
						
							|  |  |  | 	if uiErr.hint != "" { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		renderedTxt += color.Bold("HINT:") + "\n" | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 		renderedTxt += "  " + uiErr.hint | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return renderedTxt | 
					
						
							|  |  |  | } |