This commit is contained in:
sam 2023-09-03 00:23:48 +02:00
commit 2586161abd
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
49 changed files with 4171 additions and 0 deletions

33
web/app/errors.go Normal file
View file

@ -0,0 +1,33 @@
package app
import (
"net/http"
"github.com/go-chi/render"
)
// WriteError writes an error message to w.
// If one or more messages are passed, a JSON response is also sent,
// with the first message becoming `error` and the second becoming `error_description`.
func (a *App) WriteError(w http.ResponseWriter, r *http.Request, status int, messages ...string) {
render.Status(r, status)
switch len(messages) {
case 0:
return
case 1:
render.JSON(w, r, errorMessage{
Error: messages[0],
})
default:
render.JSON(w, r, errorMessage{
Error: messages[0],
ErrorDescription: messages[1],
})
}
}
type errorMessage struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
}