Creating readJSON method to manage errors that can happen once.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 54s

This commit is contained in:
Maxime Delporte
2025-10-24 14:27:46 +02:00
parent b3502631c2
commit c6baa807e4

View File

@@ -3,7 +3,9 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/julienschmidt/httprouter"
"io"
"net/http"
"strconv"
)
@@ -82,3 +84,45 @@ func (app *application) writeJSON(w http.ResponseWriter, status int, data envelo
return nil
}
func (app *application) readJSON(w http.ResponseWriter, r *http.Request, dst any) error {
// Decode the request body into the target destination.
err := json.NewDecoder(r.Body).Decode(dst)
if err != nil {
// If there is an error during decoding, start the triage...
var syntaxError *json.SyntaxError
var unmarshalTypeError *json.UnmarshalTypeError
var invalidUnmarshalError *json.InvalidUnmarshalError
switch {
// Use the errors.As() function to check whether the error has the type *json.SyntaxError. If it does, then return a plain-english error message which includes the location problem.
case errors.As(err, &syntaxError):
return fmt.Errorf("body contains badly-formed JSON (at character %d)", syntaxError.Offset)
// In some circumstances Decode() may also return an io.ErrUnexpectedEOF error for syntax errors in the JSON. So we check for this using errors.Is() and return a generic error message.
case errors.Is(err, io.ErrUnexpectedEOF):
return errors.New("body contains badly-formed JSON")
// Likewise, catch any *json.UnmarshalTypeError errors. These occur when the JSON value is the wrong type for the target destination. If the error relates to a specific field, then we include that in our error message to make it easier for the client to debug.
case errors.As(err, &unmarshalTypeError):
if unmarshalTypeError.Field != "" {
return fmt.Errorf("body contains incorrect JSON type for field %q", unmarshalTypeError.Field)
}
return fmt.Errorf("body contains incorrect JSON type (at character %d)", unmarshalTypeError.Offset)
// An io.EOF error will be returned by Decode() if the request body is empty. We check for this with errors.Is() and return a plain-english error message instead.
case errors.Is(err, io.EOF):
return errors.New("body must not be empty")
// A json.InvalidUnmarshalError error will be returned if we pass something that is not a non-nil pointer to Decode(). We catch this and panic, rather than returning an error to our handler.
case errors.As(err, &invalidUnmarshalError):
panic(err)
// For anything else, return the error mesasge as-is.
default:
return err
}
}
return nil
}