From 50d8258c544a32915ebf101f4eafec745c1673af Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 3 Jun 2020 17:11:16 +0100 Subject: [PATCH] Return some more M_BAD_JSON in the right places --- clientapi/routing/membership.go | 8 +++++++- clientapi/routing/profile.go | 24 ++++++++++++++++++++---- federationapi/routing/join.go | 5 +++++ federationapi/routing/leave.go | 5 +++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/clientapi/routing/membership.go b/clientapi/routing/membership.go index cd0ce7328..74d92a059 100644 --- a/clientapi/routing/membership.go +++ b/clientapi/routing/membership.go @@ -276,7 +276,13 @@ func checkAndProcessThreepid( Code: http.StatusNotFound, JSON: jsonerror.NotFound(err.Error()), } - } else if err != nil { + } else if e, ok := err.(gomatrixserverlib.BadJSONError); ok { + return inviteStored, &util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON(e.Error()), + } + } + if err != nil { util.GetLogger(req.Context()).WithError(err).Error("threepid.CheckAndProcessInvite failed") er := jsonerror.InternalServerError() return inviteStored, &er diff --git a/clientapi/routing/profile.go b/clientapi/routing/profile.go index 1c1ee8036..a34177161 100644 --- a/clientapi/routing/profile.go +++ b/clientapi/routing/profile.go @@ -157,8 +157,16 @@ func SetAvatarURL( req.Context(), memberships, newProfile, userID, cfg, evTime, rsAPI, ) if err != nil { - util.GetLogger(req.Context()).WithError(err).Error("buildMembershipEvents failed") - return jsonerror.InternalServerError() + switch e := err.(type) { + case gomatrixserverlib.BadJSONError: + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON(e.Error()), + } + default: + util.GetLogger(req.Context()).WithError(err).Error("buildMembershipEvents failed") + return jsonerror.InternalServerError() + } } if _, err := rsProducer.SendEvents(req.Context(), events, cfg.Matrix.ServerName, nil); err != nil { @@ -271,8 +279,16 @@ func SetDisplayName( req.Context(), memberships, newProfile, userID, cfg, evTime, rsAPI, ) if err != nil { - util.GetLogger(req.Context()).WithError(err).Error("buildMembershipEvents failed") - return jsonerror.InternalServerError() + switch e := err.(type) { + case gomatrixserverlib.BadJSONError: + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON(e.Error()), + } + default: + util.GetLogger(req.Context()).WithError(err).Error("buildMembershipEvents failed") + return jsonerror.InternalServerError() + } } if _, err := rsProducer.SendEvents(req.Context(), events, cfg.Matrix.ServerName, nil); err != nil { diff --git a/federationapi/routing/join.go b/federationapi/routing/join.go index 617a6cab0..f0d3415c8 100644 --- a/federationapi/routing/join.go +++ b/federationapi/routing/join.go @@ -102,6 +102,11 @@ func MakeJoin( Code: http.StatusNotFound, JSON: jsonerror.NotFound("Room does not exist"), } + } else if e, ok := err.(gomatrixserverlib.BadJSONError); ok { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON(e.Error()), + } } else if err != nil { util.GetLogger(httpReq.Context()).WithError(err).Error("internal.BuildEvent failed") return jsonerror.InternalServerError() diff --git a/federationapi/routing/leave.go b/federationapi/routing/leave.go index bd226d7ee..62ca11457 100644 --- a/federationapi/routing/leave.go +++ b/federationapi/routing/leave.go @@ -76,6 +76,11 @@ func MakeLeave( Code: http.StatusNotFound, JSON: jsonerror.NotFound("Room does not exist"), } + } else if e, ok := err.(gomatrixserverlib.BadJSONError); ok { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON(e.Error()), + } } else if err != nil { util.GetLogger(httpReq.Context()).WithError(err).Error("internal.BuildEvent failed") return jsonerror.InternalServerError()