|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +var ( |
| 9 | + // RErrNotFound request error if something |
| 10 | + // was requested but wasn't found |
| 11 | + RErrNotFound = NewRequestError("not found", http.StatusNotFound) |
| 12 | + |
| 13 | + // RErrAlreadyExists request error if something |
| 14 | + // requested already exists |
| 15 | + RErrAlreadyExists = NewRequestError("already exists", http.StatusBadRequest) |
| 16 | + |
| 17 | + // RErrBadRequest if a request was bad |
| 18 | + RErrBadRequest = NewRequestError("Bad request", http.StatusBadRequest) |
| 19 | + |
| 20 | + // RErrInvalid if something is invalid |
| 21 | + RErrInvalid = NewRequestError("invalid", http.StatusUnprocessableEntity) |
| 22 | + |
| 23 | + // RErrTokenInvalid if a token is not valid |
| 24 | + RErrTokenInvalid = RErrInvalid.Prepend("Token").WithCode(http.StatusUnauthorized) |
| 25 | + |
| 26 | + // RErrNotSupported if an action is not supported |
| 27 | + RErrNotSupported = NewRequestError("not supported", http.StatusUnprocessableEntity) |
| 28 | + |
| 29 | + // RErrNotAllowed if a request is not allowed for a given user |
| 30 | + RErrNotAllowed = NewRequestError("not allowed", http.StatusForbidden) |
| 31 | + |
| 32 | + // RErrMissing if something required is missing |
| 33 | + RErrMissing = NewRequestError("missing", http.StatusUnprocessableEntity) |
| 34 | + |
| 35 | + // RErrTimeout timeout error |
| 36 | + RErrTimeout = NewRequestError("timeout", http.StatusRequestTimeout) |
| 37 | + |
| 38 | + // RErrPermissionDenied if a user has no permission to run a certain command |
| 39 | + RErrPermissionDenied = NewRequestError("permission denied", http.StatusForbidden) |
| 40 | +) |
| 41 | + |
| 42 | +// RequestError error appearing in a request |
| 43 | +type RequestError struct { |
| 44 | + Message string |
| 45 | + ResponseCode int |
| 46 | +} |
| 47 | + |
| 48 | +// NewRequestError create a new requestErorr |
| 49 | +func NewRequestError(msg string, code int) *RequestError { |
| 50 | + return &RequestError{ |
| 51 | + Message: msg, |
| 52 | + ResponseCode: code, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Prepend text to the error |
| 57 | +func (re *RequestError) Prepend(txt string) *RequestError { |
| 58 | + // Check if space has to be added |
| 59 | + if strings.HasSuffix(txt, " ") || strings.HasPrefix(re.Message, " ") { |
| 60 | + re.Message = txt + re.Message |
| 61 | + } else { |
| 62 | + re.Message = txt + " " + re.Message |
| 63 | + } |
| 64 | + |
| 65 | + return re |
| 66 | +} |
| 67 | + |
| 68 | +// Append text to the error |
| 69 | +func (re *RequestError) Append(txt string) *RequestError { |
| 70 | + // Check if space has to be added |
| 71 | + if strings.HasSuffix(re.Message, " ") || strings.HasPrefix(txt, " ") { |
| 72 | + re.Message += txt |
| 73 | + } else { |
| 74 | + re.Message += " " + txt |
| 75 | + } |
| 76 | + |
| 77 | + return re |
| 78 | +} |
| 79 | + |
| 80 | +// WithCode uses a different HTTP responsecode |
| 81 | +func (re *RequestError) WithCode(code int) *RequestError { |
| 82 | + re.ResponseCode = code |
| 83 | + return re |
| 84 | +} |
| 85 | + |
| 86 | +// Implement String for error |
| 87 | +func (re RequestError) String() string { |
| 88 | + return re.Message |
| 89 | +} |
| 90 | + |
| 91 | +func (re RequestError) Error() string { |
| 92 | + return re.String() |
| 93 | +} |
0 commit comments