github.com/annwntech/go-micro/v2@v2.9.5/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:. proto/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 // NotImplemented generates a 501 error 120 func NotImplemented(id, format string, a ...interface{}) error { 121 return &Error{ 122 Id: id, 123 Code: 501, 124 Detail: fmt.Sprintf(format, a...), 125 Status: http.StatusText(501), 126 } 127 } 128 129 // BadGateway generates a 502 error 130 func BadGateway(id, format string, a ...interface{}) error { 131 return &Error{ 132 Id: id, 133 Code: 502, 134 Detail: fmt.Sprintf(format, a...), 135 Status: http.StatusText(502), 136 } 137 } 138 139 // ServiceUnavailable generates a 503 error 140 func ServiceUnavailable(id, format string, a ...interface{}) error { 141 return &Error{ 142 Id: id, 143 Code: 503, 144 Detail: fmt.Sprintf(format, a...), 145 Status: http.StatusText(503), 146 } 147 } 148 149 // GatewayTimeout generates a 504 error 150 func GatewayTimeout(id, format string, a ...interface{}) error { 151 return &Error{ 152 Id: id, 153 Code: 504, 154 Detail: fmt.Sprintf(format, a...), 155 Status: http.StatusText(504), 156 } 157 } 158 159 // Equal tries to compare errors 160 func Equal(err1 error, err2 error) bool { 161 verr1, ok1 := err1.(*Error) 162 verr2, ok2 := err2.(*Error) 163 164 if ok1 != ok2 { 165 return false 166 } 167 168 if !ok1 { 169 return err1 == err2 170 } 171 172 if verr1.Code != verr2.Code { 173 return false 174 } 175 176 return true 177 } 178 179 // FromError try to convert go error to *Error 180 func FromError(err error) *Error { 181 if verr, ok := err.(*Error); ok && verr != nil { 182 return verr 183 } 184 185 return Parse(err.Error()) 186 }