Change to new matrix error string types

This commit is contained in:
Devon Hudson 2023-05-16 11:15:54 -06:00
parent 826bb301ad
commit 956e3c4cbe
No known key found for this signature in database
GPG key ID: CD06B18E77F6A628
11 changed files with 27 additions and 21 deletions

View file

@ -107,13 +107,13 @@ func TestBadLoginFromJSONReader(t *testing.T) {
Name string Name string
Body string Body string
WantErrCode string WantErrCode spec.MatrixErrorCode
}{ }{
{Name: "empty", WantErrCode: "M_BAD_JSON"}, {Name: "empty", WantErrCode: spec.ErrorBadJSON},
{ {
Name: "badUnmarshal", Name: "badUnmarshal",
Body: `badsyntaxJSON`, Body: `badsyntaxJSON`,
WantErrCode: "M_BAD_JSON", WantErrCode: spec.ErrorBadJSON,
}, },
{ {
Name: "badPassword", Name: "badPassword",
@ -123,7 +123,7 @@ func TestBadLoginFromJSONReader(t *testing.T) {
"password": "invalidpassword", "password": "invalidpassword",
"device_id": "adevice" "device_id": "adevice"
}`, }`,
WantErrCode: "M_FORBIDDEN", WantErrCode: spec.ErrorForbidden,
}, },
{ {
Name: "badToken", Name: "badToken",
@ -132,7 +132,7 @@ func TestBadLoginFromJSONReader(t *testing.T) {
"token": "invalidtoken", "token": "invalidtoken",
"device_id": "adevice" "device_id": "adevice"
}`, }`,
WantErrCode: "M_FORBIDDEN", WantErrCode: spec.ErrorForbidden,
}, },
{ {
Name: "badType", Name: "badType",
@ -140,7 +140,7 @@ func TestBadLoginFromJSONReader(t *testing.T) {
"type": "m.login.invalid", "type": "m.login.invalid",
"device_id": "adevice" "device_id": "adevice"
}`, }`,
WantErrCode: "M_INVALID_PARAM", WantErrCode: spec.ErrorInvalidParam,
}, },
} }
for _, tst := range tsts { for _, tst := range tsts {

View file

@ -33,6 +33,7 @@ import (
uapi "github.com/matrix-org/dendrite/userapi/api" uapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrix"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -1105,7 +1106,7 @@ func Test3PID(t *testing.T) {
resp := threepid.GetValidatedResponse{} resp := threepid.GetValidatedResponse{}
switch r.URL.Query().Get("client_secret") { switch r.URL.Query().Get("client_secret") {
case "fail": case "fail":
resp.ErrCode = "M_SESSION_NOT_VALIDATED" resp.ErrCode = string(spec.ErrorSessionNotValidated)
case "fail2": case "fail2":
resp.ErrCode = "some other error" resp.ErrCode = "some other error"
case "fail3": case "fail3":

View file

@ -17,14 +17,14 @@ func errorResponse(ctx context.Context, err error, msg string, args ...interface
if eerr, ok := err.(spec.MatrixError); ok { if eerr, ok := err.(spec.MatrixError); ok {
var status int var status int
switch eerr.ErrCode { switch eerr.ErrCode {
case "M_INVALID_PARAM": case spec.ErrorInvalidParam:
status = http.StatusBadRequest status = http.StatusBadRequest
case "M_NOT_FOUND": case spec.ErrorNotFound:
status = http.StatusNotFound status = http.StatusNotFound
default: default:
status = http.StatusInternalServerError status = http.StatusInternalServerError
} }
return util.MatrixErrorResponse(status, eerr.ErrCode, eerr.Err) return util.MatrixErrorResponse(status, string(eerr.ErrCode), eerr.Err)
} }
util.GetLogger(ctx).WithError(err).Errorf(msg, args...) util.GetLogger(ctx).WithError(err).Errorf(msg, args...)
return util.JSONResponse{ return util.JSONResponse{

View file

@ -70,7 +70,7 @@ func RequestEmailToken(req *http.Request, threePIDAPI api.ClientUserAPI, cfg *co
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: spec.MatrixError{ JSON: spec.MatrixError{
ErrCode: "M_THREEPID_IN_USE", ErrCode: spec.ErrorThreePIDInUse,
Err: userdb.Err3PIDInUse.Error(), Err: userdb.Err3PIDInUse.Error(),
}, },
} }
@ -131,7 +131,7 @@ func CheckAndSave3PIDAssociation(
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: spec.MatrixError{ JSON: spec.MatrixError{
ErrCode: "M_THREEPID_AUTH_FAILED", ErrCode: spec.ErrorThreePIDAuthFailed,
Err: "Failed to auth 3pid", Err: "Failed to auth 3pid",
}, },
} }

View file

@ -26,6 +26,7 @@ import (
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib/fclient" "github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
) )
// EmailAssociationRequest represents the request defined at https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-register-email-requesttoken // EmailAssociationRequest represents the request defined at https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-register-email-requesttoken
@ -133,7 +134,7 @@ func CheckAssociation(
return false, "", "", err return false, "", "", err
} }
if respBody.ErrCode == "M_SESSION_NOT_VALIDATED" { if respBody.ErrCode == string(spec.ErrorSessionNotValidated) {
return false, "", "", nil return false, "", "", nil
} else if len(respBody.ErrCode) > 0 { } else if len(respBody.ErrCode) > 0 {
return false, "", "", errors.New(respBody.Error) return false, "", "", errors.New(respBody.Error)

View file

@ -173,7 +173,7 @@ func MakeJoin(
case spec.MatrixError: case spec.MatrixError:
util.GetLogger(httpReq.Context()).WithError(internalErr) util.GetLogger(httpReq.Context()).WithError(internalErr)
code := http.StatusInternalServerError code := http.StatusInternalServerError
switch e.Code { switch e.ErrCode {
case spec.ErrorForbidden: case spec.ErrorForbidden:
code = http.StatusForbidden code = http.StatusForbidden
case spec.ErrorNotFound: case spec.ErrorNotFound:

View file

@ -581,7 +581,7 @@ func MakeFedAPI(
go wakeup.Wakeup(req.Context(), fedReq.Origin()) go wakeup.Wakeup(req.Context(), fedReq.Origin())
vars, err := httputil.URLDecodeMapValues(mux.Vars(req)) vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
if err != nil { if err != nil {
return util.MatrixErrorResponse(400, "M_UNRECOGNISED", "badly encoded query params") return util.MatrixErrorResponse(400, string(spec.ErrorUnrecognized), "badly encoded query params")
} }
jsonRes := f(req, fedReq, vars) jsonRes := f(req, fedReq, vars)

2
go.mod
View file

@ -22,7 +22,7 @@ require (
github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e
github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91 github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530
github.com/matrix-org/gomatrixserverlib v0.0.0-20230515224826-c80a7e3dc01d github.com/matrix-org/gomatrixserverlib v0.0.0-20230516171129-bc4d64a7c071
github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a
github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 github.com/matrix-org/util v0.0.0-20221111132719-399730281e66
github.com/mattn/go-sqlite3 v1.14.16 github.com/mattn/go-sqlite3 v1.14.16

4
go.sum
View file

@ -323,8 +323,8 @@ github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91 h1:s7fexw
github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91/go.mod h1:e+cg2q7C7yE5QnAXgzo512tgFh1RbQLC0+jozuegKgo= github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91/go.mod h1:e+cg2q7C7yE5QnAXgzo512tgFh1RbQLC0+jozuegKgo=
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U= github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U=
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s= github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
github.com/matrix-org/gomatrixserverlib v0.0.0-20230515224826-c80a7e3dc01d h1:Tf9jUdnK6WyvdRgmeLtkpndhoOFsOdBWWqZGld6lpHU= github.com/matrix-org/gomatrixserverlib v0.0.0-20230516171129-bc4d64a7c071 h1:W6O4VhHp7t0+GrP1MTX6Lc2trSJ8orHgZH0/j+b3lRU=
github.com/matrix-org/gomatrixserverlib v0.0.0-20230515224826-c80a7e3dc01d/go.mod h1:H9V9N3Uqn1bBJqYJNGK1noqtgJTaCEhtTdcH/mp50uU= github.com/matrix-org/gomatrixserverlib v0.0.0-20230516171129-bc4d64a7c071/go.mod h1:H9V9N3Uqn1bBJqYJNGK1noqtgJTaCEhtTdcH/mp50uU=
github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a h1:awrPDf9LEFySxTLKYBMCiObelNx/cBuv/wzllvCCH3A= github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a h1:awrPDf9LEFySxTLKYBMCiObelNx/cBuv/wzllvCCH3A=
github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a/go.mod h1:HchJX9oKMXaT2xYFs0Ha/6Zs06mxLU8k6F1ODnrGkeQ= github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a/go.mod h1:HchJX9oKMXaT2xYFs0Ha/6Zs06mxLU8k6F1ODnrGkeQ=
github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 h1:6z4KxomXSIGWqhHcfzExgkH3Z3UkIXry4ibJS4Aqz2Y= github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 h1:6z4KxomXSIGWqhHcfzExgkH3Z3UkIXry4ibJS4Aqz2Y=

View file

@ -15,10 +15,12 @@
package httputil package httputil
import ( import (
"encoding/json"
"net/http" "net/http"
"net/url" "net/url"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/matrix-org/gomatrixserverlib/spec"
) )
// URLDecodeMapValues is a function that iterates through each of the items in a // URLDecodeMapValues is a function that iterates through each of the items in a
@ -66,13 +68,15 @@ func NewRouters() Routers {
var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell unrecognizedErr, _ := json.Marshal(spec.Unrecognized("Unrecognized request")) // nolint:misspell
_, _ = w.Write(unrecognizedErr) // nolint:misspell
})) }))
var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell unrecognizedErr, _ := json.Marshal(spec.Unrecognized("Unrecognized request")) // nolint:misspell
_, _ = w.Write(unrecognizedErr) // nolint:misspell
})) }))
func (r *Routers) configureHTTPErrors() { func (r *Routers) configureHTTPErrors() {

View file

@ -122,7 +122,7 @@ func MakeRelayAPI(
}() }()
vars, err := httputil.URLDecodeMapValues(mux.Vars(req)) vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
if err != nil { if err != nil {
return util.MatrixErrorResponse(400, "M_UNRECOGNISED", "badly encoded query params") return util.MatrixErrorResponse(400, string(spec.ErrorUnrecognized), "badly encoded query params")
} }
jsonRes := f(req, fedReq, vars) jsonRes := f(req, fedReq, vars)