worker/api: align & improve error handlers

This commit is contained in:
Diaa Sami 2021-12-22 10:38:04 +01:00 committed by Sanne Raymaekers
parent 11e2ae4528
commit 7c52db1ae1
2 changed files with 11 additions and 12 deletions

View file

@ -224,18 +224,17 @@ func apiErrorFromEchoError(echoError *echo.HTTPError) ServiceErrorCode {
// Convert an echo error into an AOC compliant one so we send a correct json error response // Convert an echo error into an AOC compliant one so we send a correct json error response
func (s *Server) HTTPErrorHandler(echoError error, c echo.Context) { func (s *Server) HTTPErrorHandler(echoError error, c echo.Context) {
doResponse := func(code ServiceErrorCode, c echo.Context) { doResponse := func(code ServiceErrorCode, c echo.Context, internal error) {
if !c.Response().Committed { if !c.Response().Committed {
var err error var err error
sec := find(code) sec := find(code)
apiErr := APIError(code, sec, c) apiErr := APIError(code, sec, c)
if sec.httpStatus == http.StatusInternalServerError { if sec.httpStatus == http.StatusInternalServerError {
internalError, ok := echoError.(*echo.HTTPError)
errMsg := fmt.Sprintf("Internal server error. Code: %s, OperationId: %s", apiErr.Code, apiErr.OperationId) errMsg := fmt.Sprintf("Internal server error. Code: %s, OperationId: %s", apiErr.Code, apiErr.OperationId)
if ok { if internal != nil {
errMsg += fmt.Sprintf(", InternalError: %v", internalError) errMsg += fmt.Sprintf(", InternalError: %v", internal)
} }
c.Logger().Error(errMsg) c.Logger().Error(errMsg)
@ -257,7 +256,7 @@ func (s *Server) HTTPErrorHandler(echoError error, c echo.Context) {
he, ok := echoError.(*echo.HTTPError) he, ok := echoError.(*echo.HTTPError)
if !ok { if !ok {
c.Logger().Errorf("ErrorNotHTTPError %v", echoError) c.Logger().Errorf("ErrorNotHTTPError %v", echoError)
doResponse(ErrorNotHTTPError, c) doResponse(ErrorNotHTTPError, c, echoError)
return return
} }
@ -271,8 +270,8 @@ func (s *Server) HTTPErrorHandler(echoError error, c echo.Context) {
sec, ok := he.Message.(ServiceErrorCode) sec, ok := he.Message.(ServiceErrorCode)
if !ok { if !ok {
// No service code was set, so Echo threw this error // No service code was set, so Echo threw this error
doResponse(apiErrorFromEchoError(he), c) doResponse(apiErrorFromEchoError(he), c, he.Internal)
return return
} }
doResponse(sec, c) doResponse(sec, c, he.Internal)
} }

View file

@ -155,11 +155,11 @@ func HTTPErrorHandler(echoError error, c echo.Context) {
apiErr := APIError(code, sec, c) apiErr := APIError(code, sec, c)
if sec.httpStatus == http.StatusInternalServerError { if sec.httpStatus == http.StatusInternalServerError {
c.Logger().Errorf("Internal server error. Internal: %s, Code: %s, OperationId: %s", c.Logger().Errorf("Internal server error. Internal: %v, Code: %s, OperationId: %s",
internal, apiErr.Code, apiErr.OperationId) internal, apiErr.Code, apiErr.OperationId)
} else { } else {
c.Logger().Infof("%s, Code: %s, OperationId: %s", internal, c.Logger().Infof("Code: %s, OperationId: %s, Internal: %v",
apiErr.Code, apiErr.OperationId) apiErr.Code, apiErr.OperationId, internal)
} }
if c.Request().Method == http.MethodHead { if c.Request().Method == http.MethodHead {
@ -168,7 +168,7 @@ func HTTPErrorHandler(echoError error, c echo.Context) {
err = c.JSON(sec.httpStatus, apiErr) err = c.JSON(sec.httpStatus, apiErr)
} }
if err != nil { if err != nil {
c.Logger().Error(err) c.Logger().Errorf("Failed to return error response: %v", err)
} }
} }
} }
@ -185,5 +185,5 @@ func HTTPErrorHandler(echoError error, c echo.Context) {
doResponse(apiErrorFromEchoError(he), c, he.Internal) doResponse(apiErrorFromEchoError(he), c, he.Internal)
return return
} }
doResponse(sec, c, nil) doResponse(sec, c, he.Internal)
} }