33 lines
760 B
Go
33 lines
760 B
Go
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"`
|
|
}
|