Removing string json's parameter from Runtime variable inside Movie's struct. Creating errors.go handling different json's error for our API.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 54s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 54s
This commit is contained in:
48
cmd/api/errors.go
Normal file
48
cmd/api/errors.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The logError() method is a generic helper for logging an error message along with the current request method and URL as attributes in the log entry.
|
||||||
|
func (app *application) logError(r *http.Request, err error) {
|
||||||
|
var (
|
||||||
|
method = r.Method
|
||||||
|
uri = r.URL.RequestURI()
|
||||||
|
)
|
||||||
|
|
||||||
|
app.logger.Error(err.Error(), "method", method, "url", uri)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The errorResponse() method is a generic helper for sending JSON-formatted error messages to the client with a given status code. Note that we're using the "any" type for the message parameter, rather than just a string type, as this gives us more flexibility over the values that we can include in the response.
|
||||||
|
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message any) {
|
||||||
|
env := envelope{"error": message}
|
||||||
|
|
||||||
|
// Write the response using the writeJSON() helper. If this happens to return an error then og it, and fall back to sending the client an empty response with a 500 Internal Server Error status code.
|
||||||
|
err := app.writeJSON(w, status, env, nil)
|
||||||
|
if err != nil {
|
||||||
|
app.logError(r, err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The serverErrorResponse() method will be used when our application encounters an unexpected problem at runtime. It logs the detailed error message, then uses the errorResponse() helper to send a 500 Internal Server Error status code and JSON response (containing a generic error message) to the client.
|
||||||
|
func (app *application) serverErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
app.logError(r, err)
|
||||||
|
|
||||||
|
message := "the server encountered a problem and could not process your request"
|
||||||
|
app.errorResponse(w, r, http.StatusInternalServerError, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The notFoundResponse() method will be used to send a 404 Not Found status code and JSON response to the client.
|
||||||
|
func (app *application) notFoundResponse(w http.ResponseWriter, r *http.Request) {
|
||||||
|
message := "the requested resource could not be found"
|
||||||
|
app.errorResponse(w, r, http.StatusNotFound, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The methodNotAllowedResponse() method will be used to send a 405 Method Not Allowed status code and JSON response to the client.
|
||||||
|
func (app *application) methodNotAllowedResponse(w http.ResponseWriter, r *http.Request) {
|
||||||
|
message := fmt.Sprintf("the %s method is not supported for this resource", r.Method)
|
||||||
|
app.errorResponse(w, r, http.StatusMethodNotAllowed, message)
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ type Movie struct {
|
|||||||
/*
|
/*
|
||||||
Use the Runtime type instead of int32. Note that the omitempty directive will still work on this: if the Runtime field has the underlying value 0, then it will be considered empty and omitted -- and the MarshalJSON() method we just made won't be called at all.
|
Use the Runtime type instead of int32. Note that the omitempty directive will still work on this: if the Runtime field has the underlying value 0, then it will be considered empty and omitted -- and the MarshalJSON() method we just made won't be called at all.
|
||||||
*/
|
*/
|
||||||
Runtime Runtime `json:"runtime,omitempty,string"`
|
Runtime Runtime `json:"runtime,omitempty"`
|
||||||
Genres []string `json:"genres,omitempty"`
|
Genres []string `json:"genres,omitempty"`
|
||||||
Version int32 `json:"version"`
|
Version int32 `json:"version"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user