You've already forked focalboard
mirror of
https://github.com/mattermost/focalboard.git
synced 2025-07-03 23:30:29 +02:00
Refactor error usage from the store level up and add API helpers (#3792)
* Refactor error usage from the store level up and add API helpers * Complete API tests * Fix merge errorResponse calls * Remove ensure helpers to allow for custom messages on permission errors * Fix bad import and call * Remove bad user check on auth that was added as part of the main merge * Fix empty list test * Replace deprecated proxy calls to ioutil.ReadAll with io.ReadAll * Add information to the NotFound errors * Add context to all remaining errors and address review comments * Fix linter * Adapt the new card API endpoints to the error refactor * Remove almost all customErrorResponse calls * Add request entity too large to errorResponse and remove customErrorResponse * Fix linter
This commit is contained in:
committed by
GitHub
parent
ed655ac996
commit
08c0b7a2fd
@ -21,22 +21,14 @@ const (
|
||||
HeaderRequestedWithXML = "XMLHttpRequest"
|
||||
UploadFormFileKey = "file"
|
||||
True = "true"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorNoTeamCode = 1000
|
||||
ErrorNoTeamMessage = "No team"
|
||||
)
|
||||
|
||||
var errAPINotSupportedInStandaloneMode = errors.New("API not supported in standalone mode")
|
||||
|
||||
type PermissionError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (pe PermissionError) Error() string {
|
||||
return pe.msg
|
||||
}
|
||||
var (
|
||||
ErrHandlerPanic = errors.New("http handler panic")
|
||||
)
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// REST APIs
|
||||
@ -133,7 +125,7 @@ func (a *API) panicHandler(next http.Handler) http.Handler {
|
||||
mlog.String("stack", string(debug.Stack())),
|
||||
mlog.String("uri", r.URL.Path),
|
||||
)
|
||||
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", nil)
|
||||
a.errorResponse(w, r, ErrHandlerPanic)
|
||||
}
|
||||
}()
|
||||
|
||||
@ -145,7 +137,7 @@ func (a *API) requireCSRFToken(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.checkCSRFToken(r) {
|
||||
a.logger.Error("checkCSRFToken FAILED")
|
||||
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "checkCSRFToken FAILED", nil)
|
||||
a.errorResponse(w, r, model.NewErrBadRequest("checkCSRFToken FAILED"))
|
||||
return
|
||||
}
|
||||
|
||||
@ -184,34 +176,39 @@ func (a *API) userIsGuest(userID string) (bool, error) {
|
||||
|
||||
// Response helpers
|
||||
|
||||
func (a *API) errorResponse(w http.ResponseWriter, api string, code int, message string, sourceError error) {
|
||||
if code == http.StatusUnauthorized || code == http.StatusForbidden {
|
||||
a.logger.Debug("API DEBUG",
|
||||
mlog.Int("code", code),
|
||||
mlog.Err(sourceError),
|
||||
mlog.String("msg", message),
|
||||
mlog.String("api", api),
|
||||
)
|
||||
} else {
|
||||
func (a *API) errorResponse(w http.ResponseWriter, r *http.Request, err error) {
|
||||
errorResponse := model.ErrorResponse{Error: err.Error()}
|
||||
|
||||
switch {
|
||||
case model.IsErrBadRequest(err):
|
||||
errorResponse.ErrorCode = http.StatusBadRequest
|
||||
case model.IsErrUnauthorized(err):
|
||||
errorResponse.ErrorCode = http.StatusUnauthorized
|
||||
case model.IsErrForbidden(err):
|
||||
errorResponse.ErrorCode = http.StatusForbidden
|
||||
case model.IsErrNotFound(err):
|
||||
errorResponse.ErrorCode = http.StatusNotFound
|
||||
case model.IsErrRequestEntityTooLarge(err):
|
||||
errorResponse.ErrorCode = http.StatusRequestEntityTooLarge
|
||||
case model.IsErrNotImplemented(err):
|
||||
errorResponse.ErrorCode = http.StatusNotImplemented
|
||||
default:
|
||||
a.logger.Error("API ERROR",
|
||||
mlog.Int("code", code),
|
||||
mlog.Err(sourceError),
|
||||
mlog.String("msg", message),
|
||||
mlog.String("api", api),
|
||||
mlog.Int("code", http.StatusInternalServerError),
|
||||
mlog.Err(err),
|
||||
mlog.String("api", r.URL.Path),
|
||||
)
|
||||
errorResponse.Error = "internal server error"
|
||||
errorResponse.ErrorCode = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
setResponseHeader(w, "Content-Type", "application/json")
|
||||
|
||||
if sourceError != nil && message != sourceError.Error() {
|
||||
message += "; " + sourceError.Error()
|
||||
}
|
||||
|
||||
data, err := json.Marshal(model.ErrorResponse{Error: message, ErrorCode: code})
|
||||
data, err := json.Marshal(errorResponse)
|
||||
if err != nil {
|
||||
data = []byte("{}")
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
|
||||
w.WriteHeader(errorResponse.ErrorCode)
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user