ferror

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2024 License: MIT Imports: 1 Imported by: 0

README

F-Error

With this package it is possible to store more information about the errors that occur in the code, such as the error message, the layer where the error occured and a message for the user.

Usage

To import the package use go get github.com/fabiokaelin/ferror.

package main

import (
	"errors"
	"fmt"

	"github.com/fabiokaelin/ferror"
)

func main() {
	_, err1 := retrunNormalError()

	ferr1 := ferrors.FromError(err1)
	if ferr1 != nil {
		fmt.Println(ferr1)
	}

	err2 := ferrors.New("this is a ferror")
	err2.SetLayer("db")
	err2.SetUserMsg("Unable to persist the data")
	err2.SetKind("db connection")
	err2.SetInternal("db connection closed unexpectedly")
	fmt.Println(err2)
}

func retrunNormalError() (string, error) {
	return "", errors.New("this is a normal error")
}
{Error: 'this is a normal error', Layer: '', UserMsg: '', Kind: '', Internal: ''}
{Error: 'this is a ferror', Layer: 'db', UserMsg: 'Unable to persist the data', Kind: 'db connection', Internal: 'db connection closed unexpectedly'}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FError

type FError interface {
	Message() string             // error message from the origin
	Layer() string               // layer where the error occurred (e.g. "controller", "service", "db", "validator")
	UserMsg() string             // message to be shown to the user
	Kind() string                // type of the error (e.g. "db execution")
	Internal() string            // internal error message
	SetLayer(layer string)       // set the layer where the error occurred (e.g. "controller", "service", "db", "validator")
	SetUserMsg(userMsg string)   // set the message to be shown to the user
	SetKind(kind string)         // set the type of the error (e.g. "db execution")
	SetInternal(internal string) // set the internal error message
	Error() string               // string representation of the error
}

func FromError

func FromError(err error) FError

FromError returns a FError from a normal error

func New

func New(text string) FError

New returns a new FError

Jump to

Keyboard shortcuts

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