1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-26 05:15:56 +00:00

rest json errors: handle unspported NaN values as the standard package's error doesn't give us more information

This commit is contained in:
Gerasimos (Makis) Maropoulos
2024-09-13 11:52:03 +03:00
parent 6776bf0dc9
commit 3137d0d791
7 changed files with 196 additions and 98 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
@@ -404,22 +405,49 @@ func handleAPIError(ctx *context.Context, apiErr client.APIError) {
}
func handleJSONError(ctx *context.Context, err error) bool {
if err == nil {
return false
}
messageText := "unable to parse request body"
wireError := InvalidArgument
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
wireError.Details(ctx, messageText, "empty body")
return true
}
var syntaxErr *json.SyntaxError
if errors.As(err, &syntaxErr) {
InvalidArgument.Details(ctx, "unable to parse body", "syntax error at byte offset %d", syntaxErr.Offset)
wireError.Details(ctx, messageText, "json: syntax error at byte offset %d", syntaxErr.Offset)
return true
}
var unmarshalErr *json.UnmarshalTypeError
if errors.As(err, &unmarshalErr) {
InvalidArgument.Details(ctx, "unable to parse body", "unmarshal error for field %q", unmarshalErr.Field)
wireError.Details(ctx, messageText, unmarshalErr.Error())
return true
}
var unsupportedValueErr *json.UnsupportedValueError
if errors.As(err, &unsupportedValueErr) {
wireError.Details(ctx, messageText, err.Error())
return true
}
var unsupportedTypeErr *json.UnsupportedTypeError
if errors.As(err, &unsupportedTypeErr) {
wireError.Details(ctx, messageText, err.Error())
return true
}
var invalidUnmarshalErr *json.InvalidUnmarshalError
if errors.As(err, &invalidUnmarshalErr) {
wireError.Details(ctx, messageText, err.Error())
return true
}
return false
// else {
// InvalidArgument.Details(ctx, "unable to parse body", err.Error())
// }
}
var (