diff --git a/error.go b/error.go new file mode 100644 index 0000000..92cfe64 --- /dev/null +++ b/error.go @@ -0,0 +1,27 @@ +package ksrv + +import ( + "encoding/json" + "fmt" +) + +type Error struct { + Code int `json:"code"` + Status string `json:"status"` + Message string `json:"msg"` + Tmpl string `json:"-"` +} + +func (e Error) New(v ...interface{}) Error { + e.Message = fmt.Sprintf(e.Tmpl, v...) + return e +} + +func (e Error) Error() string { + return e.Message +} + +func (e Error) Json() []byte { + data, _ := json.Marshal(e) + return data +} diff --git a/net.go b/net.go index edb0187..419476f 100644 --- a/net.go +++ b/net.go @@ -41,6 +41,9 @@ func Abort(w http.ResponseWriter, errs ...interface{}) (int, error) { msg := []byte{} for _, err := range errs { switch v := err.(type) { + case Error: + code = v.Code + msg = v.Json() case int: if v >= 100 || v < 600 { code = v