diff --git a/internal/httputil/httpapi.go b/internal/httputil/httpapi.go index 365bbe928..e0c80a965 100644 --- a/internal/httputil/httpapi.go +++ b/internal/httputil/httpapi.go @@ -218,13 +218,34 @@ func MakeFedAPI( if fedReq == nil { return errResp } + // add the user to Sentry, if enabled + hub := sentry.GetHubFromContext(req.Context()) + if hub != nil { + hub.Scope().SetTag("origin", string(fedReq.Origin())) + hub.Scope().SetTag("uri", fedReq.RequestURI()) + } + defer func() { + if r := recover(); r != nil { + if hub != nil { + hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path)) + } + // re-panic to return the 500 + panic(r) + } + }() go wakeup.Wakeup(req.Context(), fedReq.Origin()) vars, err := URLDecodeMapValues(mux.Vars(req)) if err != nil { - return util.ErrorResponse(err) + return util.MatrixErrorResponse(400, "M_UNRECOGNIZED", "badly encoded query params") } - return f(req, fedReq, vars) + jsonRes := f(req, fedReq, vars) + // do not log 4xx as errors as they are client fails, not server fails + if hub != nil && jsonRes.Code >= 500 { + hub.Scope().SetExtra("response", jsonRes) + hub.CaptureException(fmt.Errorf("%s returned HTTP %d", req.URL.Path, jsonRes.Code)) + } + return jsonRes } return MakeExternalAPI(metricsName, h) }