diff --git a/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go b/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go index 110d28e70..ee6033416 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go +++ b/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go @@ -17,15 +17,12 @@ import ( "net/http" "strconv" "time" - - "github.com/matrix-org/dendrite/clientapi/jsonerror" - "github.com/matrix-org/util" ) // ParseTSParam takes a req (typically from an application service) and parses a Time object // from the req if it exists in the query parameters. If it doesn't exist, the // current time is returned. -func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) { +func ParseTSParam(req *http.Request) (time.Time, error) { // Use the ts parameter's value for event time if present tsStr := req.URL.Query().Get("ts") if tsStr == "" { @@ -35,12 +32,7 @@ func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) { // The parameter exists, parse into a Time object ts, err := strconv.ParseInt(tsStr, 10, 64) if err != nil { - return time.Time{}, &util.JSONResponse{ - Code: http.StatusBadRequest, - JSON: jsonerror.InvalidArgumentValue( - fmt.Sprintf("Param 'ts' is no valid int (%s)", err.Error()), - ), - } + return time.Time{}, fmt.Errorf("Param 'ts' is no valid int (%s)", err.Error()) } return time.Unix(ts/1000, 0), nil diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go index 9a10c33ae..a7187c495 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go @@ -147,9 +147,12 @@ func createRoom( return *resErr } - evTime, resErr := httputil.ParseTSParam(req) - if resErr != nil { - return *resErr + evTime, err := httputil.ParseTSParam(req) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue(err.Error()), + } } // TODO: visibility/presets/raw initial state/creation content // TODO: Create room alias association diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go index 834c1b73a..98c7cd6a7 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go @@ -52,9 +52,12 @@ func JoinRoomByIDOrAlias( return *resErr } - evTime, resErr := httputil.ParseTSParam(req) - if resErr != nil { - return *resErr + evTime, err := httputil.ParseTSParam(req) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue(err.Error()), + } } localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go b/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go index d2873d0fa..b308de79a 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go @@ -50,9 +50,12 @@ func SendMembership( return *reqErr } - evTime, resErr := httputil.ParseTSParam(req) - if resErr != nil { - return *resErr + evTime, err := httputil.ParseTSParam(req) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue(err.Error()), + } } inviteStored, err := threepid.CheckAndProcessInvite( diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go index e80e290b7..e57d16fbf 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go @@ -107,9 +107,12 @@ func SetAvatarURL( return httputil.LogThenError(req, err) } - evTime, resErr := httputil.ParseTSParam(req) - if resErr != nil { - return *resErr + evTime, err := httputil.ParseTSParam(req) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue(err.Error()), + } } oldProfile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) @@ -201,9 +204,12 @@ func SetDisplayName( return httputil.LogThenError(req, err) } - evTime, resErr := httputil.ParseTSParam(req) - if resErr != nil { - return *resErr + evTime, err := httputil.ParseTSParam(req) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue(err.Error()), + } } oldProfile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/sendevent.go b/src/github.com/matrix-org/dendrite/clientapi/routing/sendevent.go index e7c2c13bf..e916e451e 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/sendevent.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/sendevent.go @@ -104,9 +104,12 @@ func generateSendEvent( return nil, resErr } - evTime, resErr := httputil.ParseTSParam(req) - if resErr != nil { - return nil, resErr + evTime, err := httputil.ParseTSParam(req) + if err != nil { + return nil, &util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue(err.Error()), + } } // create the new event and set all the fields we can @@ -116,7 +119,7 @@ func generateSendEvent( Type: eventType, StateKey: stateKey, } - err := builder.SetContent(r) + err = builder.SetContent(r) if err != nil { resErr := httputil.LogThenError(req, err) return nil, &resErr