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:
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user