fatal

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2024 License: MIT Imports: 4 Imported by: 1

README

fatal

fatal by LitFill. library for fatal assignment or logging (error management)

Example

Log(http.ServeAndListen(port),
    "Can not serve and listen",
    "port", port
)

file := Assign(os.Create("log.txt"),
    "Can not create file",
    "file", "log.txt"
)

Documentation

Overview

fatal, LitFill <marrazzy54 at email dot com> library for fatal assignment or logging (error management)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign[T any](val T, err error, msg string, log ...any) T

Assign wraps function call returning a `val` and error to log it if error != nil using `log/slog` so it has `msg` and `log`. example:

file := Assign(os.Create("log.txt"),
    "Can not create file",
    "file", "log.txt"
)

func AssignR added in v0.0.3

func AssignR[T any](r ErrorResult[T], msg string, log ...any) T

func AssignV2 added in v0.0.3

func AssignV2[T any](val T, err error, msg string, log ...any) T

func Debug added in v0.0.3

func Debug(msg string, log ...any)

func Error added in v0.0.3

func Error(msg string, log ...any)

func Info added in v0.0.3

func Info(msg string, log ...any)

func Log

func Log(err error, msg string, log ...any)

Log wraps function call returning error to log it using `log/slog` so it has `msg` and `log`. example:

Log(http.ServeAndListen(port),
    "Can not serve and listen",
    "port", port
)

func SetLogger added in v0.0.3

func SetLogger(logr Logger)

Types

type ErrorHandler added in v0.0.3

type ErrorHandler[T, E any] func(E) Result[T, E]

ErrorHandler represents a function that handles an error

type ErrorResult added in v0.0.3

type ErrorResult[T any] interface {
	Result[T, error]
}

func Errorable added in v0.0.3

func Errorable[T any](val T, err error) ErrorResult[T]

type Logger added in v0.0.3

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger added in v0.0.3

func NewLogger(w io.Writer, l slog.Level) Logger

func (Logger) Debug added in v0.0.3

func (r Logger) Debug(msg string, log ...any)

func (Logger) Error added in v0.0.3

func (r Logger) Error(msg string, log ...any)

func (Logger) Info added in v0.0.3

func (r Logger) Info(msg string, log ...any)

func (*Logger) SetLevel added in v0.0.3

func (r *Logger) SetLevel(l slog.Level)

type Result added in v0.0.3

type Result[T, E any] interface {
	Error() E
	Value() T
	Failure() bool
	Success() bool
	Map(func(T) any) Result[any, E]
	FlatMap(func(T) Result[T, E]) Result[T, E]
	Or(ErrorHandler[T, E]) Result[T, E]
}

Result represents a result of an operation that can fail

func Fail added in v0.0.3

func Fail[T, E any](err E) Result[T, E]

Fail creates a failure

func FromTuple added in v0.0.3

func FromTuple[T, E any](val T, err E) Result[T, E]

FromTuple creates a Result object from a tupple value, error

func Succeed added in v0.0.3

func Succeed[T, E any](val T) Result[T, E]

Succeed creates a success

Jump to

Keyboard shortcuts

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