response

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorHandler = func(c *fiber.Ctx, err error) error {
	resp := Response{
		Code: fiber.StatusInternalServerError,
	}

	if c, ok := err.(validator.ValidationErrors); ok {
		resp.Code = fiber.StatusUnprocessableEntity
		resp.Messages = Messages{removeTopStruct(c.Translate(trans))}
	} else if c, ok := err.(*fiber.Error); ok {
		resp.Code = c.Code
		resp.Messages = Messages{c.Message}
	} else if c, ok := err.(*Error); ok {
		resp.Code = c.Code
		resp.Messages = Messages{c.Message}

		if resp.Messages == nil {
			resp.Messages = Messages{err}
		}
	} else {
		resp.Messages = Messages{err.Error()}
	}

	if !IsProduction {
		log.Error().Err(err).Msg("From: Fiber's error handler")
	}

	return Resp(c, resp)
}

Default error handler

View Source
var IsProduction bool

nothiing to describe this fucking variable

Functions

func ParseAndValidate

func ParseAndValidate(c *fiber.Ctx, body any) error

func ParseBody

func ParseBody(c *fiber.Ctx, body any) error

func Resp

func Resp(c *fiber.Ctx, resp Response) error

function to return pretty json response

func ValidateStruct

func ValidateStruct(input any) error

Types

type Error

type Error struct {
	Code    int `json:"code"`
	Message any `json:"message"`
}

func (*Error) Error

func (e *Error) Error() string

error makes it compatible with the error interface

type Messages

type Messages = []any

Alias for any slice

type Response

type Response struct {
	Code     int      `json:"code"`
	Messages Messages `json:"messages"`
	Data     any      `json:"data"`
}

A struct to return normal response

Jump to

Keyboard shortcuts

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