domain_errors

package
v0.0.0-...-b3a3e34 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package domain_errors provides errors that I use in my applications on the domain layer. Those errors are easy to handle and use on the API layer. Those errors have information about HTTP code, log it or not, api message, any additional states. Also, this package provides some

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HttpHandleDomainErr

func HttpHandleDomainErr(
	dErr DomainErr,
	logger *slog.Logger,
	w http.ResponseWriter,
	r *http.Request)

HttpHandleDomainErr handles domain error for you in http handler. Just pass values and it sets http body, http code and logs the error if err.Info().Log is true.

This function don't check if logger or error or request is nil. Check it before using this function in your code. Overwise you'll get a panic

Usage:

	...
	// Somewhere inside the http handler
	someData, errx := domain.SomeLogic( ... )
	if errx != nil {
	  HttpHandleDomainErr(errx, logger, w, r)
    return
	}

func LogDomainErr

func LogDomainErr(
	dErr DomainErr,
	logger *slog.Logger)

Just logs error if err.Info().Log is true It don't check if logger or error is nil. Check it before using this function in your code. Overwise this function will panic

Types

type DomainErr

type DomainErr errorsx.CustomErr[DomainErrInfo]

DomainErr is an error that is great for structured logging due it's ability to store any map of data and it contains information about how to handle it What makes it a perfect choise for using on the domain layer of the application That error can be easily handled using LogDomainErr and HttpHandleDomainErr functions

func NewDomainErr

func NewDomainErr(err string, inf DomainErrInfo) DomainErr

NewDomainErr create an error that is great for structured logging and contains information about how to handle it (log it or not, how handle this error on API layer)

type DomainErrInfo

type DomainErrInfo struct {
	Log        bool
	HttpCode   int
	ApiMessage string
	Vars       map[string]any
}

Jump to

Keyboard shortcuts

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