summaryrefslogtreecommitdiff
path: root/internal/api/util/errorhandling.go
diff options
context:
space:
mode:
authorLibravatar Vyr Cossont <vyr@noreply.codeberg.org>2025-11-11 19:39:09 +0100
committerLibravatar tobi <tobi.smethurst@protonmail.com>2025-11-17 14:15:04 +0100
commit0c7b069c4a09f01a3a6c59bd7b66f096f8dcec40 (patch)
treee5306d5ebad1e5a6efe5393bfafde95f5b94b237 /internal/api/util/errorhandling.go
parent[performance] remove hard reliance on .Cached field to indicate whether media... (diff)
downloadgotosocial-0c7b069c4a09f01a3a6c59bd7b66f096f8dcec40.tar.xz
[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 <kipvandenbos@noreply.codeberg.org> Co-authored-by: Vyr Cossont <vyr@noreply.codeberg.org> Co-committed-by: Vyr Cossont <vyr@noreply.codeberg.org>
Diffstat (limited to 'internal/api/util/errorhandling.go')
-rw-r--r--internal/api/util/errorhandling.go26
1 files changed, 13 insertions, 13 deletions
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,
})
}