exception

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package exception provides helper functions to facilitate work with errors, e traces or handling panics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregatedf added in v0.3.0

func Aggregatedf(errors ...error) error

Aggregatedf returns formatted array of errors as single error.

func ConvertToError

func ConvertToError(i any) error

ConvertToError returns i if it's error or new error string.

func HandlePanicError

func HandlePanicError(onPanic func(error))

HandlePanicError defers function which check for recover() and convert it to error preserving error type or creating new string error. If recover() is nil onPanic will not be called.

Example
package main

import (
	"fmt"

	"github.com/Prastiwar/Go-flow/exception"
)

func main() {
	defer exception.HandlePanicError(func(err error) {
		fmt.Println(err)
	})

	panic("string error")

}
Output:

string error

func StackTrace

func StackTrace() string

StackTrace returns a formatted string e trace of the goroutine that calls it.

Types

type AggregatedError added in v0.3.0

type AggregatedError []error

AggregatedError is an array of errors. It implements error interface and aggregates multiple errors.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/Prastiwar/Go-flow/exception"
)

func main() {
	aggErr := exception.Aggregate(
		errors.New("'' is not valid value for title"),
		errors.New("title is required"),
	)
	err := fmt.Errorf("(%v) validation errors: %w", len(aggErr), aggErr)
	fmt.Println(err)

}
Output:

(2) validation errors: ["'' is not valid value for title", "title is required"]

func Aggregate added in v0.3.0

func Aggregate(errs ...error) AggregatedError

Aggregate returns AggregatedError filled with specified errors. This is helper function to use variadic errors as cast to AggregatedError.

func (AggregatedError) Error added in v0.3.0

func (err AggregatedError) Error() string

func (AggregatedError) Flat added in v0.3.0

func (err AggregatedError) Flat() AggregatedError

Flat returns AggregatedError with flat one-dimensional array. Any nested AggregatedError will be unwrapped.

Jump to

Keyboard shortcuts

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