Documentation ¶
Index ¶
- Constants
- Variables
- func CodeMessage(c *fiber.Ctx, code int, msg string) error
- func CodeMessagef(c *fiber.Ctx, code int, format string, args ...interface{}) error
- func Data(c *fiber.Ctx, data interface{}) error
- func Logger() fiber.Handler
- func Message(c *fiber.Ctx, msg string) error
- func Messagef(c *fiber.Ctx, format string, args ...interface{}) error
- func MobileRule(fl validator.FieldLevel) bool
- func Resp(c *fiber.Ctx, statusCode int, res Response) error
- func RespAccepted(c *fiber.Ctx, msg ...string) error
- func RespAlreadyReported(c *fiber.Ctx, msg ...string) error
- func RespCreated(c *fiber.Ctx, msg ...string) error
- func RespMultiStatus(c *fiber.Ctx, msg ...string) error
- func RespNoContent(c *fiber.Ctx, msg ...string) error
- func RespNonAuthoritativeInformation(c *fiber.Ctx, msg ...string) error
- func RespOK(c *fiber.Ctx, msg ...string) error
- func RespPartialContent(c *fiber.Ctx, msg ...string) error
- func RespResetContent(c *fiber.Ctx, msg ...string) error
- func ValidateBody(c *fiber.Ctx, obj interface{}) (err error)
- func ValidateQuery(c *fiber.Ctx, obj interface{}) (err error)
- type Error
- type JsonTime
- type Response
Constants ¶
const (
JsonTimeFormat = "2006-01-02 15:04:05"
)
Variables ¶
var ErrHandler = func(c *fiber.Ctx, err error) error { code := fiber.StatusInternalServerError res := Response{ Code: code, Message: utils.StatusMessage(code), } if errs, ok := err.(validator.ValidationErrors); ok { res.Code = fiber.StatusUnprocessableEntity res.Message = "" res.Data = removeTopStruct(errs.Translate(trans)) } else if e, ok := err.(*fiber.Error); ok { res.Code = e.Code res.Message = e.Message } else if e, ok := err.(*Error); ok { res.Code = e.code res.Message = e.message if res.Message == "" { res.Message = e.Error() } } return Resp(c, res.Code, res) }
ErrHandler is Dawn's error handler
var V *validator.Validate
V is an instance of *validator.Validate used for custom registrations.
Functions ¶
func CodeMessage ¶ added in v0.4.2
CodeMessage responses with specific status code and message
func CodeMessagef ¶ added in v0.4.2
CodeMessagef responses with specific status code and formatted message
func Data ¶
func Data(c *fiber.Ctx, data interface{}) error
Data returns data with status code OK by default
func Logger ¶
func Logger() fiber.Handler
Logger logs request and response info to os.Stdout or os.Stderr. The format is: time #pid[ request-id]: latency status clientIP method protocol://host_path[ error]
func MobileRule ¶
func MobileRule(fl validator.FieldLevel) bool
MobileRule validates mobile phone number
func RespAccepted ¶
RespAccepted responses with status code 202 RFC 7231, 6.3.3
func RespAlreadyReported ¶
RespAlreadyReported responses with status code 208 RFC 5842, 7.1
func RespCreated ¶
RespCreated responses with status code 201 RFC 7231, 6.3.2
func RespMultiStatus ¶
RespMultiStatus responses with status code 207 RFC 4918, 11.1
func RespNoContent ¶
RespNoContent responses with status code 204 RFC 7231, 6.3.5
func RespNonAuthoritativeInformation ¶
RespNonAuthoritativeInformation responses with status code 203 RFC 7231, 6.3.4
func RespPartialContent ¶
RespPartialContent responses with status code 206 RFC 7233, 4.1
func RespResetContent ¶
RespResetContent responses with status code 205 RFC 7231, 6.3.6
func ValidateBody ¶
func ValidateBody(c *fiber.Ctx, obj interface{}) (err error)
ValidateBody accepts a obj holds results from BodyParser and then do the validation by a validator
func ValidateQuery ¶
func ValidateQuery(c *fiber.Ctx, obj interface{}) (err error)
ValidateQuery accepts a obj holds results from QueryParser and then do the validation by a validator
Types ¶
type Error ¶ added in v0.4.2
type Error struct {
// contains filtered or unexported fields
}
Error represents an error that occurred while handling a request.
func CodeErr ¶ added in v0.4.2
CodeErr creates a new Error instance with specific status code and an optional message
func CodeErrf ¶ added in v0.4.2
CodeErrf creates a new Error instance with specific status code and a formatted message
func Err ¶ added in v0.4.2
Err creates a new internal server Error instance with an optional message
type JsonTime ¶ added in v0.4.0
JsonTime uses custom format to marshal time to json
func (JsonTime) MarshalJSON ¶ added in v0.4.0
MarshalJSON marshals JsonTime into json
func (*JsonTime) UnmarshalJSON ¶ added in v0.4.0
UnmarshalJSON unmarshal data to JsonTime
type Response ¶
type Response struct { // Code is the status code by default, but also can be // a custom code Code int `json:"code,omitempty"` // Message shows detail thing back to caller Message string `json:"message,omitempty"` // RequestID needs to be used with middleware RequestID string `json:"request_id,omitempty"` // Data accepts any thing as the response data Data interface{} `json:"data,omitempty"` }
Response is a formatted struct for api results