100 lines
3.5 KiB
Go
100 lines
3.5 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"runtime/debug"
|
|
)
|
|
|
|
func (app *application) logError(r *http.Request, err error) {
|
|
var (
|
|
method = r.Method
|
|
uri = r.URL.RequestURI()
|
|
)
|
|
|
|
app.logger.Error(err.Error(), "method", method, "uri", uri)
|
|
fmt.Printf("%v\n", string(debug.Stack()))
|
|
}
|
|
|
|
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message any) {
|
|
env := envelope{"error": message}
|
|
|
|
err := app.writeJSON(w, status, env, nil)
|
|
if err != nil {
|
|
app.logError(r, err)
|
|
w.WriteHeader(500)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (app *application) badRequestResponse(w http.ResponseWriter, r *http.Request, err error) {
|
|
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
|
|
}
|
|
|
|
func (app *application) failedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string) {
|
|
app.errorResponse(w, r, http.StatusUnprocessableEntity, errors)
|
|
}
|
|
|
|
func (app *application) editConflictResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "unable to update the record due to an edit conflict, please try again"
|
|
app.errorResponse(w, r, http.StatusConflict, message)
|
|
}
|
|
|
|
func (app *application) conflictResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "a conflicting record already exists"
|
|
app.errorResponse(w, r, http.StatusConflict, message)
|
|
}
|
|
|
|
func (app *application) rateLimitExceededResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "rate limit exceeded"
|
|
app.errorResponse(w, r, http.StatusTooManyRequests, message)
|
|
}
|
|
|
|
func (app *application) invalidCredentialsResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "invalid authentication credentials"
|
|
app.errorResponse(w, r, http.StatusUnauthorized, message)
|
|
}
|
|
|
|
func (app *application) invalidAuthenticationTokenResponse(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("WWW-Authenticate", "Bearer")
|
|
|
|
message := "invalid or missing authentication token"
|
|
app.errorResponse(w, r, http.StatusUnauthorized, message)
|
|
}
|
|
|
|
func (app *application) authenticationRequiredResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "you must be authenticated to access this resource"
|
|
app.errorResponse(w, r, http.StatusUnauthorized, message)
|
|
}
|
|
|
|
func (app *application) inactiveAccountResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "your user account must be activated to access this resource"
|
|
app.errorResponse(w, r, http.StatusForbidden, message)
|
|
}
|
|
|
|
func (app *application) permissionDeniedResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "you do not have permission to perform this action"
|
|
app.errorResponse(w, r, http.StatusForbidden, message)
|
|
}
|
|
|
|
func (app *application) untrustedOriginResponse(w http.ResponseWriter, r *http.Request) {
|
|
message := "the request origin is not trusted"
|
|
app.errorResponse(w, r, http.StatusForbidden, message)
|
|
}
|