github.com/micro/go-micro/v2@v2.9.1/errors/errors.go (about) 1 // Package errors provides a way to return detailed information 2 // for an RPC request error. The error is normally JSON encoded. 3 package errors 4 5 import ( 6 "encoding/json" 7 "fmt" 8 "net/http" 9 ) 10 11 //go:generate protoc -I. --go_out=paths=source_relative:. errors.proto 12 13 func (e *Error) Error() string { 14 b, _ := json.Marshal(e) 15 return string(b) 16 } 17 18 // New generates a custom error. 19 func New(id, detail string, code int32) error { 20 return &Error{ 21 Id: id, 22 Code: code, 23 Detail: detail, 24 Status: http.StatusText(int(code)), 25 } 26 } 27 28 // Parse tries to parse a JSON string into an error. If that 29 // fails, it will set the given string as the error detail. 30 func Parse(err string) *Error { 31 e := new(Error) 32 errr := json.Unmarshal([]byte(err), e) 33 if errr != nil { 34 e.Detail = err 35 } 36 return e 37 } 38 39 // BadRequest generates a 400 error. 40 func BadRequest(id, format string, a ...interface{}) error { 41 return &Error{ 42 Id: id, 43 Code: 400, 44 Detail: fmt.Sprintf(format, a...), 45 Status: http.StatusText(400), 46 } 47 } 48 49 // Unauthorized generates a 401 error. 50 func Unauthorized(id, format string, a ...interface{}) error { 51 return &Error{ 52 Id: id, 53 Code: 401, 54 Detail: fmt.Sprintf(format, a...), 55 Status: http.StatusText(401), 56 } 57 } 58 59 // Forbidden generates a 403 error. 60 func Forbidden(id, format string, a ...interface{}) error { 61 return &Error{ 62 Id: id, 63 Code: 403, 64 Detail: fmt.Sprintf(format, a...), 65 Status: http.StatusText(403), 66 } 67 } 68 69 // NotFound generates a 404 error. 70 func NotFound(id, format string, a ...interface{}) error { 71 return &Error{ 72 Id: id, 73 Code: 404, 74 Detail: fmt.Sprintf(format, a...), 75 Status: http.StatusText(404), 76 } 77 } 78 79 // MethodNotAllowed generates a 405 error. 80 func MethodNotAllowed(id, format string, a ...interface{}) error { 81 return &Error{ 82 Id: id, 83 Code: 405, 84 Detail: fmt.Sprintf(format, a...), 85 Status: http.StatusText(405), 86 } 87 } 88 89 // Timeout generates a 408 error. 90 func Timeout(id, format string, a ...interface{}) error { 91 return &Error{ 92 Id: id, 93 Code: 408, 94 Detail: fmt.Sprintf(format, a...), 95 Status: http.StatusText(408), 96 } 97 } 98 99 // Conflict generates a 409 error. 100 func Conflict(id, format string, a ...interface{}) error { 101 return &Error{ 102 Id: id, 103 Code: 409, 104 Detail: fmt.Sprintf(format, a...), 105 Status: http.StatusText(409), 106 } 107 } 108 109 // InternalServerError generates a 500 error. 110 func InternalServerError(id, format string, a ...interface{}) error { 111 return &Error{ 112 Id: id, 113 Code: 500, 114 Detail: fmt.Sprintf(format, a...), 115 Status: http.StatusText(500), 116 } 117 } 118 119 // Equal tries to compare errors 120 func Equal(err1 error, err2 error) bool { 121 verr1, ok1 := err1.(*Error) 122 verr2, ok2 := err2.(*Error) 123 124 if ok1 != ok2 { 125 return false 126 } 127 128 if !ok1 { 129 return err1 == err2 130 } 131 132 if verr1.Code != verr2.Code { 133 return false 134 } 135 136 return true 137 } 138 139 // FromError try to convert go error to *Error 140 func FromError(err error) *Error { 141 if verr, ok := err.(*Error); ok && verr != nil { 142 return verr 143 } 144 145 return Parse(err.Error()) 146 }