init
This commit is contained in:
commit
2586161abd
49 changed files with 4171 additions and 0 deletions
33
web/app/errors.go
Normal file
33
web/app/errors.go
Normal 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"`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue