From 0c7b069c4a09f01a3a6c59bd7b66f096f8dcec40 Mon Sep 17 00:00:00 2001 From: Vyr Cossont Date: Tue, 11 Nov 2025 19:39:09 +0100 Subject: [chore] Apply schema for OpenAPI errors (#4511) Adds an error struct to Swagger covering normal and OAuth errors for client API endpoints. Now clients using Swagger codegen can handle errors. Specifically, if you _don't_ have typed errors in your Swagger IDL and you're using `go-swagger` to generate your API client, the error message is discarded and you are only told the status code. With this change in place, clients like `slurp` can tell the user that they tried to upload an emoji that was too big or whatever. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4511 Reviewed-by: tobi Co-authored-by: Vyr Cossont Co-committed-by: Vyr Cossont --- internal/api/util/errorhandling.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'internal/api/util/errorhandling.go') diff --git a/internal/api/util/errorhandling.go b/internal/api/util/errorhandling.go index 7ffbbbf88..f2f4d94a7 100644 --- a/internal/api/util/errorhandling.go +++ b/internal/api/util/errorhandling.go @@ -43,7 +43,7 @@ import ( // If an error is returned by InstanceGet, the function will panic. func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), accept string, errWithCode gtserror.WithCode) { switch accept { - case string(TextHTML): + case TextHTML: ctx := c.Request.Context() instance, err := instanceGet(ctx) if err != nil { @@ -55,8 +55,8 @@ func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*api gtscontext.RequestID(ctx), ) default: - JSON(c, http.StatusNotFound, map[string]string{ - "error": errWithCode.Safe(), + JSON(c, http.StatusNotFound, apimodel.Error{ + Error: errWithCode.Safe(), }) } } @@ -66,7 +66,7 @@ func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*api // or just some error json if the caller prefers (or has no preference). func genericErrorHandler(c *gin.Context, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), accept string, errWithCode gtserror.WithCode) { switch accept { - case string(TextHTML): + case TextHTML: ctx := c.Request.Context() instance, err := instanceGet(ctx) if err != nil { @@ -80,8 +80,8 @@ func genericErrorHandler(c *gin.Context, instanceGet func(ctx context.Context) ( gtscontext.RequestID(ctx), ) default: - JSON(c, errWithCode.Code(), map[string]string{ - "error": errWithCode.Safe(), + JSON(c, errWithCode.Code(), apimodel.Error{ + Error: errWithCode.Safe(), }) } } @@ -179,9 +179,9 @@ func OAuthErrorHandler(c *gin.Context, errWithCode gtserror.WithCode) { l.Debug("handling OAuth error") } - JSON(c, statusCode, map[string]string{ - "error": errWithCode.Error(), - "error_description": errWithCode.Safe(), + JSON(c, statusCode, apimodel.Error{ + Error: errWithCode.Error(), + ErrorDescription: errWithCode.Safe(), }) } @@ -189,8 +189,8 @@ func OAuthErrorHandler(c *gin.Context, errWithCode gtserror.WithCode) { // Specifically used for accounts trying to access endpoints they cannot use while moving. func NotFoundAfterMove(c *gin.Context) { const errMsg = "your account has Moved or is currently Moving; you cannot use this endpoint" - JSON(c, http.StatusForbidden, map[string]string{ - "error": errMsg, + JSON(c, http.StatusForbidden, apimodel.Error{ + Error: errMsg, }) } @@ -198,7 +198,7 @@ func NotFoundAfterMove(c *gin.Context) { // Specifically used for accounts trying to take actions on endpoints they cannot do while moving. func ForbiddenAfterMove(c *gin.Context) { const errMsg = "your account has Moved or is currently Moving; you cannot take create or update type actions" - JSON(c, http.StatusForbidden, map[string]string{ - "error": errMsg, + JSON(c, http.StatusForbidden, apimodel.Error{ + Error: errMsg, }) } -- cgit v1.2.3