fiberx

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2020 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	JsonTimeFormat = "2006-01-02 15:04:05"
)

Variables

View Source
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

View Source
var V *validator.Validate

V is an instance of *validator.Validate used for custom registrations.

Functions

func CodeMessage added in v0.4.2

func CodeMessage(c *fiber.Ctx, code int, msg string) error

CodeMessage responses with specific status code and message

func CodeMessagef added in v0.4.2

func CodeMessagef(c *fiber.Ctx, code int, format string, args ...interface{}) error

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 Message

func Message(c *fiber.Ctx, msg string) error

Message responses with 200 and specific message

func Messagef added in v0.4.1

func Messagef(c *fiber.Ctx, format string, args ...interface{}) error

Messagef responses with 200 and specific formatted message

func MobileRule

func MobileRule(fl validator.FieldLevel) bool

MobileRule validates mobile phone number

func Resp

func Resp(c *fiber.Ctx, statusCode int, res Response) error

Resp returns the custom response

func RespAccepted

func RespAccepted(c *fiber.Ctx, msg ...string) error

RespAccepted responses with status code 202 RFC 7231, 6.3.3

func RespAlreadyReported

func RespAlreadyReported(c *fiber.Ctx, msg ...string) error

RespAlreadyReported responses with status code 208 RFC 5842, 7.1

func RespCreated

func RespCreated(c *fiber.Ctx, msg ...string) error

RespCreated responses with status code 201 RFC 7231, 6.3.2

func RespMultiStatus

func RespMultiStatus(c *fiber.Ctx, msg ...string) error

RespMultiStatus responses with status code 207 RFC 4918, 11.1

func RespNoContent

func RespNoContent(c *fiber.Ctx, msg ...string) error

RespNoContent responses with status code 204 RFC 7231, 6.3.5

func RespNonAuthoritativeInformation

func RespNonAuthoritativeInformation(c *fiber.Ctx, msg ...string) error

RespNonAuthoritativeInformation responses with status code 203 RFC 7231, 6.3.4

func RespOK

func RespOK(c *fiber.Ctx, msg ...string) error

RespOK responses with status code 200 RFC 7231, 6.3.1

func RespPartialContent

func RespPartialContent(c *fiber.Ctx, msg ...string) error

RespPartialContent responses with status code 206 RFC 7233, 4.1

func RespResetContent

func RespResetContent(c *fiber.Ctx, msg ...string) error

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

func CodeErr(code int, err error, message ...string) *Error

CodeErr creates a new Error instance with specific status code and an optional message

func CodeErrf added in v0.4.2

func CodeErrf(code int, err error, format string, args ...interface{}) *Error

CodeErrf creates a new Error instance with specific status code and a formatted message

func Err added in v0.4.2

func Err(err error, message ...string) *Error

Err creates a new internal server Error instance with an optional message

func Errf added in v0.4.2

func Errf(err error, format string, args ...interface{}) *Error

Errf creates a new internal server Error instance with formatted message

func (*Error) Error added in v0.4.2

func (e *Error) Error() string

Error makes it compatible with the `error` interface.

type JsonTime added in v0.4.0

type JsonTime time.Time

JsonTime uses custom format to marshal time to json

func (JsonTime) MarshalJSON added in v0.4.0

func (jt JsonTime) MarshalJSON() ([]byte, error)

MarshalJSON marshals JsonTime into json

func (JsonTime) String added in v0.4.0

func (jt JsonTime) String() string

String returns custom formatted time string

func (*JsonTime) UnmarshalJSON added in v0.4.0

func (jt *JsonTime) UnmarshalJSON(data []byte) error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL