mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-02 09:39:35 -06:00
44 lines
705 B
Go
44 lines
705 B
Go
package errors
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ApiErr struct {
|
|
Status int `json:"status"`
|
|
Code string `json:"code"`
|
|
Message string `json:"msg"`
|
|
}
|
|
|
|
func New(httpStatus int, code, msg string) error {
|
|
return &ApiErr{
|
|
Status: httpStatus,
|
|
Code: code,
|
|
Message: msg,
|
|
}
|
|
}
|
|
|
|
func (e *ApiErr) Error() string {
|
|
return fmt.Sprintf("%d %s (%s)", e.Status, e.Code, e.Message)
|
|
}
|
|
|
|
func Is(err, target error) bool {
|
|
return errors.Is(err, target)
|
|
}
|
|
|
|
func RecoverApiError(c *gin.Context) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
if e, ok := err.(*ApiErr); ok {
|
|
c.AbortWithStatusJSON(e.Status, e)
|
|
} else {
|
|
panic(err)
|
|
}
|
|
}
|
|
}()
|
|
c.Next()
|
|
}
|