Move json errors over to gmsl (#3080)

This commit is contained in:
devonh 2023-05-09 22:46:49 +00:00 committed by GitHub
parent a49c9f01e2
commit 0489d16f95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
109 changed files with 808 additions and 1217 deletions

View file

@ -20,7 +20,6 @@ import (
"fmt"
"net/http"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/dendrite/syncapi/synctypes"
@ -57,12 +56,12 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
StateToFetch: []gomatrixserverlib.StateKeyTuple{},
}, &stateRes); err != nil {
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if !stateRes.RoomExists {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("room does not exist"),
JSON: spec.Forbidden("room does not exist"),
}
}
@ -74,7 +73,7 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
content := map[string]string{}
if err := json.Unmarshal(ev.Content(), &content); err != nil {
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal for history visibility failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if visibility, ok := content["history_visibility"]; ok {
worldReadable = visibility == "world_readable"
@ -100,14 +99,14 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
}, &membershipRes)
if err != nil {
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
// If the user has never been in the room then stop at this point.
// We won't tell the user about a room they have never joined.
if !membershipRes.HasBeenInRoom {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
JSON: spec.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
}
}
// Otherwise, if the user has been in the room, whether or not we
@ -148,7 +147,7 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
}, &stateAfterRes)
if err != nil {
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
for _, ev := range stateAfterRes.StateEvents {
stateEvents = append(
@ -203,7 +202,7 @@ func OnIncomingStateTypeRequest(
StateToFetch: stateToFetch,
}, &stateRes); err != nil {
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
// Look at the room state and see if we have a history visibility event
@ -214,7 +213,7 @@ func OnIncomingStateTypeRequest(
content := map[string]string{}
if err := json.Unmarshal(ev.Content(), &content); err != nil {
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal for history visibility failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if visibility, ok := content["history_visibility"]; ok {
worldReadable = visibility == "world_readable"
@ -240,14 +239,14 @@ func OnIncomingStateTypeRequest(
}, &membershipRes)
if err != nil {
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
// If the user has never been in the room then stop at this point.
// We won't tell the user about a room they have never joined.
if !membershipRes.HasBeenInRoom || membershipRes.Membership == spec.Ban {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
JSON: spec.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
}
}
// Otherwise, if the user has been in the room, whether or not we
@ -295,7 +294,7 @@ func OnIncomingStateTypeRequest(
}, &stateAfterRes)
if err != nil {
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if len(stateAfterRes.StateEvents) > 0 {
event = stateAfterRes.StateEvents[0]
@ -307,7 +306,7 @@ func OnIncomingStateTypeRequest(
if event == nil {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound(fmt.Sprintf("Cannot find state event for %q", evType)),
JSON: spec.NotFound(fmt.Sprintf("Cannot find state event for %q", evType)),
}
}