iris/_examples/database/mysql/api/httperror.go
Gerasimos (Makis) Maropoulos ed45c77be5 reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
2020-06-07 15:26:06 +03:00

61 lines
1.4 KiB
Go

package api
import (
"fmt"
"time"
"github.com/kataras/iris/v12"
)
// Error holds the error sent by server to clients (JSON).
type Error struct {
StatusCode int `json:"code"`
Method string `json:"-"`
Path string `json:"-"`
Message string `json:"message"`
Timestamp int64 `json:"timestamp"`
}
func newError(statusCode int, method, path, format string, args ...interface{}) Error {
msg := format
if len(args) > 0 {
// why we check for that? If the original error message came from our database
// and it contains fmt-reserved words
// like %s or %d we will get MISSING(=...) in our error message and we don't want that.
msg = fmt.Sprintf(msg, args...)
}
if msg == "" {
msg = iris.StatusText(statusCode)
}
return Error{
StatusCode: statusCode,
Method: method,
Path: path,
Message: msg,
Timestamp: time.Now().Unix(),
}
}
// Error implements the internal Go error interface.
func (e Error) Error() string {
return fmt.Sprintf("[%d] %s: %s: %s", e.StatusCode, e.Method, e.Path, e.Message)
}
// Is implements the standard `errors.Is` internal interface.
// Usage: errors.Is(e, target)
func (e Error) Is(target error) bool {
if target == nil {
return false
}
err, ok := target.(Error)
if !ok {
return false
}
return (err.StatusCode == e.StatusCode || e.StatusCode == 0) &&
(err.Message == e.Message || e.Message == "")
}