mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore] Prefer JSON errors in API endpoints (#1766)
* Default to JSON over HTML for error handling * Change the default error display for web endpoints to html
This commit is contained in:
@@ -88,7 +88,8 @@ func genericErrorHandler(c *gin.Context, instanceGet func(ctx context.Context) (
|
||||
// the caller prefers to see an html page with the error rendered there. If not, or
|
||||
// if something goes wrong during the function, it will recover and just try to serve
|
||||
// an appropriate application/json content-type error.
|
||||
func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode)) {
|
||||
// To override the default response type, specify `offers`.
|
||||
func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), offers ...MIME) {
|
||||
// set the error on the gin context so that it can be logged
|
||||
// in the gin logger middleware (internal/router/logger.go)
|
||||
c.Error(errWithCode) //nolint:errcheck
|
||||
@@ -97,7 +98,7 @@ func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet fun
|
||||
// or if we should just use a json. Normally we would want to
|
||||
// check for a returned error, but if an error occurs here we
|
||||
// can just fall back to default behavior (serve json error).
|
||||
accept, _ := NegotiateAccept(c, HTMLOrJSONAcceptHeaders...)
|
||||
accept, _ := NegotiateAccept(c, JSONOrHTMLAcceptHeaders...)
|
||||
|
||||
if errWithCode.Code() == http.StatusNotFound {
|
||||
// use our special not found handler with useful status text
|
||||
@@ -107,6 +108,11 @@ func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet fun
|
||||
}
|
||||
}
|
||||
|
||||
// WebErrorHandler is like ErrorHandler, but will display HTML over JSON by default.
|
||||
func WebErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode)) {
|
||||
ErrorHandler(c, errWithCode, instanceGet, TextHTML, AppJSON)
|
||||
}
|
||||
|
||||
// OAuthErrorHandler is a lot like ErrorHandler, but it specifically returns errors
|
||||
// that are compatible with https://datatracker.ietf.org/doc/html/rfc6749#section-5.2,
|
||||
// but serializing errWithCode.Error() in the 'error' field, and putting any help text
|
||||
|
@@ -44,12 +44,12 @@ var WebfingerJSONAcceptHeaders = []MIME{
|
||||
AppJSON,
|
||||
}
|
||||
|
||||
// HTMLOrJSONAcceptHeaders is a slice of offers that prefers TextHTML and will
|
||||
// fall back to JSON if necessary. This is useful for error handling, since it can
|
||||
// JSONOrHTMLAcceptHeaders is a slice of offers that prefers AppJSON and will
|
||||
// fall back to HTML if necessary. This is useful for error handling, since it can
|
||||
// be used to serve a nice HTML page if the caller accepts that, or just JSON if not.
|
||||
var HTMLOrJSONAcceptHeaders = []MIME{
|
||||
TextHTML,
|
||||
var JSONOrHTMLAcceptHeaders = []MIME{
|
||||
AppJSON,
|
||||
TextHTML,
|
||||
}
|
||||
|
||||
// HTMLAcceptHeaders is a slice of offers that just contains text/html types.
|
||||
|
Reference in New Issue
Block a user