diff options
author | Kegsay <kegan@matrix.org> | 2020-06-24 15:06:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-24 15:06:14 +0100 |
commit | 002fe05a203e316818c108a0dac438e5cd796a68 (patch) | |
tree | bc597b82d09007d9cff14bf2c4c6557bfe9eb125 /clientapi | |
parent | ebaaf65c54a624e693341e32619806028a45ba2f (diff) |
Add PerformInvite and refactor how errors get handled (#1158)
* Add PerformInvite and refactor how errors get handled
- Rename `JoinError` to `PerformError`
- Remove `error` from the API function signature entirely. This forces
errors to be bundled into `PerformError` which makes it easier for callers
to detect and handle errors. On network errors, HTTP clients will make a
`PerformError`.
* Unbreak everything; thanks Go!
* Send back JSONResponse according to the PerformError
* Update federation invite code too
Diffstat (limited to 'clientapi')
-rw-r--r-- | clientapi/routing/createroom.go | 8 | ||||
-rw-r--r-- | clientapi/routing/joinroom.go | 31 | ||||
-rw-r--r-- | clientapi/routing/membership.go | 8 |
3 files changed, 11 insertions, 36 deletions
diff --git a/clientapi/routing/createroom.go b/clientapi/routing/createroom.go index be712482..8682b03a 100644 --- a/clientapi/routing/createroom.go +++ b/clientapi/routing/createroom.go @@ -403,15 +403,15 @@ func createRoom( } } // Send the invite event to the roomserver. - if err = roomserverAPI.SendInvite( + if perr := roomserverAPI.SendInvite( req.Context(), rsAPI, inviteEvent.Headered(roomVersion), strippedState, // invite room state cfg.Matrix.ServerName, // send as server nil, // transaction ID - ); err != nil { - util.GetLogger(req.Context()).WithError(err).Error("SendInvite failed") - return jsonerror.InternalServerError() + ); perr != nil { + util.GetLogger(req.Context()).WithError(perr).Error("SendInvite failed") + return perr.JSONResponse() } } diff --git a/clientapi/routing/joinroom.go b/clientapi/routing/joinroom.go index e606e35f..cb68fe19 100644 --- a/clientapi/routing/joinroom.go +++ b/clientapi/routing/joinroom.go @@ -15,12 +15,10 @@ package routing import ( - "errors" "net/http" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/httputil" - "github.com/matrix-org/dendrite/clientapi/jsonerror" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage/accounts" @@ -65,32 +63,9 @@ func JoinRoomByIDOrAlias( } // Ask the roomserver to perform the join. - err = rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes) - // Handle known errors first, if this is 0 then there will be no matches (eg on success) - switch joinRes.Error { - case roomserverAPI.JoinErrorBadRequest: - return util.JSONResponse{ - Code: http.StatusBadRequest, - JSON: jsonerror.Unknown(joinRes.ErrMsg), - } - case roomserverAPI.JoinErrorNoRoom: - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound(joinRes.ErrMsg), - } - case roomserverAPI.JoinErrorNotAllowed: - return util.JSONResponse{ - Code: http.StatusForbidden, - JSON: jsonerror.Forbidden(joinRes.ErrMsg), - } - } - // this is always populated on generic errors - if joinRes.ErrMsg != "" { - return util.ErrorResponse(errors.New(joinRes.ErrMsg)) - } - // this is set on network errors in polylith mode - if err != nil { - return util.ErrorResponse(err) + rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes) + if joinRes.Error != nil { + return joinRes.Error.JSONResponse() } return util.JSONResponse{ diff --git a/clientapi/routing/membership.go b/clientapi/routing/membership.go index 0d4b0d88..aff1730c 100644 --- a/clientapi/routing/membership.go +++ b/clientapi/routing/membership.go @@ -111,16 +111,16 @@ func SendMembership( switch membership { case gomatrixserverlib.Invite: // Invites need to be handled specially - err = roomserverAPI.SendInvite( + perr := roomserverAPI.SendInvite( req.Context(), rsAPI, event.Headered(verRes.RoomVersion), nil, // ask the roomserver to draw up invite room state for us cfg.Matrix.ServerName, nil, ) - if err != nil { - util.GetLogger(req.Context()).WithError(err).Error("producer.SendInvite failed") - return jsonerror.InternalServerError() + if perr != nil { + util.GetLogger(req.Context()).WithError(perr).Error("producer.SendInvite failed") + return perr.JSONResponse() } case gomatrixserverlib.Join: // The join membership requires the room id to be sent in the response |