Files
imaginary_mirror/error.go
2015-04-19 22:50:03 +02:00

69 lines
1.2 KiB
Go

package main
import (
"encoding/json"
"net/http"
"strings"
)
const (
UNAVAILABLE uint8 = iota
BAD_REQUEST
NOT_ALLOWED
UNSUPPORTED
UNAUTHORIZED
INTERNAL
NOT_FOUND
)
type Error struct {
Message string `json:"message,omitempty"`
Code uint8 `json:"code"`
}
func (e *Error) JSON() []byte {
buf, _ := json.Marshal(e)
return buf
}
func (e *Error) Error() string {
return e.Message
}
func (e *Error) HttpCode() int {
code := http.StatusServiceUnavailable
switch e.Code {
case BAD_REQUEST:
code = http.StatusBadRequest
break
case NOT_ALLOWED:
code = http.StatusMethodNotAllowed
break
case UNSUPPORTED:
code = http.StatusUnsupportedMediaType
break
case INTERNAL:
code = http.StatusInternalServerError
break
case UNAUTHORIZED:
code = http.StatusUnauthorized
break
case NOT_FOUND:
code = http.StatusNotFound
break
}
return code
}
func NewError(err string, code uint8) *Error {
err = strings.Replace(err, "\n", "", -1)
return &Error{err, code}
}
func ErrorReply(w http.ResponseWriter, msg string, code uint8) {
err := NewError(msg, code)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(err.HttpCode())
w.Write(err.JSON())
}