mistake

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 3 Imported by: 0

README

Mistake

My personal simple error handler built in Go.

Why Mistake?

Our approach begins with designing an unified error handler just for being more clear due to error package is extremely flexible and then Mistake born to be it.
Also we have a preference on go-playground/validator package and was integrated from the beginning.

This package is inspired on API error handler and we recommend used it for this.

How to start?

We have 2 ways to create a mistake object:

Create a new single error with id and message.

func New(statusCode int, messageID string, message string) *M

But if your project uses the package github.com/go-playground/validator, you can use this constructor and pass the error.
Internally the errors are converted to validator.ValidationErrors and retrieve Field and Tag attributes only.

func NewStructValidation(err error, statusCode int, messageID string, message string) *M

Dynamic messages

Mistake provides two functions for those messages where use verbs (%v).
Append values add all those variables will be replaced on the verbs.
Formatter takes the message with verbs and apply fmt.Sprintf on it, at the end append values will be dropped.

func AppendValue(v any)
func Formatter(message string) string

Here an example:

mistake.AppendValue(request.Email)
mt := mistake.New(
    resources.DUPLICATED,
    resources.CREATE_USER_EXIST,
    mistake.Formatter("here is my email: '%v'"), // here is my email: 'mymy@emailme.com'
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendValue

func AppendValue(v any)

append value to be applied into AppendDetail method

func Formatter

func Formatter(message string) string

Types

type Detail

type Detail struct {
	ID      string
	Message string
}

mistake Detail

type M

type M struct {
	ReturnCode int
	Details    []Detail
}

main structure to hold M info

func New

func New(statusCode int, messageID string, message string) *M

creates a new mistake with one message

func NewStructValidation

func NewStructValidation(err error, statusCode int, messageID string, message string) *M

Creates a new Mistake(M) from "github.com/go-playground/validator" type

func (M) Error

func (e M) Error() string

return string error

Jump to

Keyboard shortcuts

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