Compare commits

...

1 Commits

Author SHA1 Message Date
Evan Chen 1ef952a490 add error template 2021-11-12 07:59:21 +08:00
2 changed files with 30 additions and 0 deletions

27
error.go Normal file
View File

@ -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
}

3
net.go
View File

@ -41,6 +41,9 @@ func Abort(w http.ResponseWriter, errs ...interface{}) (int, error) {
msg := []byte{} msg := []byte{}
for _, err := range errs { for _, err := range errs {
switch v := err.(type) { switch v := err.(type) {
case Error:
code = v.Code
msg = v.Json()
case int: case int:
if v >= 100 || v < 600 { if v >= 100 || v < 600 {
code = v code = v