Skip to content

Commit

Permalink
refactor: status codes and errors on creating, updating checking remo…
Browse files Browse the repository at this point in the history
…te triggers
  • Loading branch information
AleksandrMatsko committed Nov 26, 2024
1 parent 520c968 commit a4f8d09
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions api/handler/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ func getTriggerFromRequest(request *http.Request) (*dto.Trigger, *api.ErrorRespo
return nil, api.ErrorInvalidRequest(fmt.Errorf("invalid expression: %s", err.Error()))
case api.ErrInvalidRequestContent:
return nil, api.ErrorInvalidRequest(err)
case remote.ErrRemoteTriggerResponse:
case remote.ErrRemoteUnavailable:
response := api.ErrorRemoteServerUnavailable(err)
middleware.GetLoggerEntry(request).Error().
String("status", response.StatusText).
Error(err).
Msg("Remote server unavailable")
return nil, response
case remote.ErrRemoteTriggerResponse:
return nil, api.ErrorInvalidRequest(fmt.Errorf("error from graphite remote: %w", err))
case *json.UnmarshalTypeError:
return nil, api.ErrorInvalidRequest(fmt.Errorf("invalid payload: %s", err.Error()))
case *prometheus.Error:
Expand Down Expand Up @@ -236,6 +238,7 @@ func getMetricTTLByTrigger(request *http.Request, trigger *dto.Trigger) (time.Du
// @success 200 {object} dto.TriggerCheckResponse "Validation is done, see response body for validation result"
// @failure 400 {object} api.ErrorInvalidRequestExample "Bad request from client"
// @failure 500 {object} api.ErrorInternalServerExample "Internal server error"
// @failure 503 {object} api.ErrorRemoteServerUnavailableExample "Remote server unavailable"
// @router /trigger/check [put]
func triggerCheck(writer http.ResponseWriter, request *http.Request) {
trigger := &dto.Trigger{}
Expand All @@ -246,10 +249,22 @@ func triggerCheck(writer http.ResponseWriter, request *http.Request) {
case expression.ErrInvalidExpression, local.ErrParseExpr, local.ErrEvalExpr, local.ErrUnknownFunction:
// TODO: move ErrInvalidExpression to separate case

// These errors are skipped because if there are error from local source then it will be caught in
// Errors above are skipped because if there are error from local source then it will be caught in
// dto.TargetVerification and will be explained in detail.
case remote.ErrRemoteUnavailable:
errRsp := api.ErrorRemoteServerUnavailable(err)
middleware.GetLoggerEntry(request).Error().
String("status", errRsp.StatusText).
Error(err).
Msg("Remote server unavailable")
render.Render(writer, request, errRsp) //nolint
return
case remote.ErrRemoteTriggerResponse:
render.Render(writer, request, api.ErrorInvalidRequest(fmt.Errorf("error from graphite remote: %w", err)))
return
case *prometheus.Error:
render.Render(writer, request, errorResponseOnPrometheusError(typedErr)) //nolint
return
default:
render.Render(writer, request, api.ErrorInvalidRequest(err)) //nolint
return
Expand Down

0 comments on commit a4f8d09

Please sign in to comment.