Files
msw-open-music/pkg/api/handle_error.go

33 lines
817 B
Go

package api
import (
"encoding/json"
"log"
"net/http"
)
type Error struct {
Error string `json:"error,omitempty"`
}
func (api *API) HandleError(w http.ResponseWriter, r *http.Request, err error) {
api.HandleErrorString(w, r, err.Error())
}
func (api *API) HandleErrorCode(w http.ResponseWriter, r *http.Request, err error, code int) {
api.HandleErrorStringCode(w, r, err.Error(), code)
}
func (api *API) HandleErrorString(w http.ResponseWriter, r *http.Request, errorString string) {
api.HandleErrorStringCode(w, r, errorString, 500)
}
func (api *API) HandleErrorStringCode(w http.ResponseWriter, r *http.Request, errorString string, code int) {
log.Println("[api] [Error]", code, errorString)
errStatus := &Error{
Error: errorString,
}
w.WriteHeader(code)
json.NewEncoder(w).Encode(errStatus)
}