ch4.2
This commit is contained in:
@@ -35,3 +35,7 @@ func (app *application) methodNotAllowedResponse(w http.ResponseWriter, r *http.
|
|||||||
message := fmt.Sprintf("the %s method is not supported for this resource", r.Method)
|
message := fmt.Sprintf("the %s method is not supported for this resource", r.Method)
|
||||||
app.errorResponse(w, r, http.StatusMethodNotAllowed, message)
|
app.errorResponse(w, r, http.StatusMethodNotAllowed, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) badRequestResponse(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -37,3 +39,31 @@ func (app *application) writeJSON(w http.ResponseWriter, status int, data envelo
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) readJSON(w http.ResponseWriter, r *http.Request, dst any) error {
|
||||||
|
err := json.NewDecoder(r.Body).Decode(dst)
|
||||||
|
if err != nil {
|
||||||
|
var syntaxError *json.SyntaxError
|
||||||
|
var unmarshalTypeError *json.UnmarshalTypeError
|
||||||
|
var invalidMarshallError *json.InvalidUnmarshalError
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case errors.As(err, &syntaxError):
|
||||||
|
return fmt.Errorf("body contains badly-formed JSON (at character %d)", syntaxError.Offset)
|
||||||
|
case errors.Is(err, io.ErrUnexpectedEOF):
|
||||||
|
return errors.New("body contains badly-formed JSON")
|
||||||
|
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 for field (at character %d)", unmarshalTypeError.Offset)
|
||||||
|
case errors.Is(err, io.EOF):
|
||||||
|
return errors.New("body must be not empty")
|
||||||
|
case errors.As(err, &invalidMarshallError):
|
||||||
|
panic(err)
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -17,9 +16,9 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques
|
|||||||
Genres []string `json:"genres"`
|
Genres []string `json:"genres"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&input)
|
err := app.readJSON(w, r, &input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
|
app.badRequestResponse(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "%+v\n", input)
|
fmt.Fprintf(w, "%+v\n", input)
|
||||||
|
|||||||
Reference in New Issue
Block a user