response

package
v0.0.0-...-7d6db92 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 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 e, ok := err.(validator.ValidationErrors); ok {
		resp.Code = fiber.StatusForbidden
		resp.Messages = Messages{removeTopStruct(e.Translate(trans))}
	} else if e, ok := err.(*fiber.Error); ok {
		resp.Code = e.Code
		resp.Messages = Messages{e.Message}
	} else if e, ok := err.(*Error); ok {
		resp.Code = e.Code
		resp.Messages = Messages{e.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

Nothing 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

A fuction to return beautiful responses.

func ValidateStruct

func ValidateStruct(input any) error

Types

type Error

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

A struct to handle error with custom error handler.

func NewError

func NewError(code int, messages ...any) *Error

NewError creates singular new Error message

func NewErrors

func NewErrors(code int, messages ...any) *Error

NewErrors creates multiple new Error messages

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,omitempty"`
	Data     any      `json:"data,omitempty"`
}

A struct to return normal responses.

Jump to

Keyboard shortcuts

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