serrs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 12 Imported by: 0

README

serrs

Go Reference GitHub Actions codecov Go Report Card

description

serrs is a library designed to simplify error handling in your applications. By using serrs, developers can effortlessly manage stack traces and integrate with monitoring tools like Sentry.

Installation

go get -u github.com/ryomak/serrs

Usage

Create an error


var HogeError = serrs.Wrap(err)

or

var InvalidParameterError = serrs.New(serrs.DefaultCode("invalid_parameter"),"invalid parameter error")

Wrap an error and add a stack trace


if err := DoSomething(); err != nil {
    // This point is recorded
    return serrs.Wrap(err)
}

fmt.Printf("%+v",err)

// Output Example:
// - file: ./serrs/format_test.go:22
//   function: github.com/ryomak/serrs_test.TestSerrs_Format
//   msg: 
// - file: ./serrs/format_test.go:14
//   function: github.com/ryomak/serrs_test.TestSerrs_Format
//   code: demo
//   data: {key1:value1,key2:value2}
// - error1
Parameters

The parameters that can be managed with serrs are three: code, message, and data. WithXXX functions can be used to add additional information to the error.

  • code: error code
  • message: err text
  • data: custom data

data
data is a custom data that can be added to the error. The data is output to the log.
If the type satisfies the CustomData interface, any type can be added.

if err := DoSomething(); err != nil {
    return serrs.Wrap(err, serrs.WithData(serrs.DefaultCustomData{
        "key": "value",
    }))
}
Get Additional Data Functions
  • GetCustomData(err error): Get custom data from error
  • GetErrorCode(err error): Get error code from error
  • GetErrorCodeString(err error): Get error code string from error
  • ErrorSurface(err error): Get top level error message
  • Origin(err error): Get original error

check error match

var HogeError = serrs.New(serrs.DefaultCode("unexpected"),"unexpected error")

if serrs.Is(HogeError) {
    
}

Send Sentry

supports sending reports to Sentry. The location where serrs.Wrap is executed is saved as a stack trace and displayed cleanly on Sentry. In addition, any added custom data or messages are also displayed as additional data on Sentry.


serrs.ReportSentry(
    err, 
    // Customize the contexts 
    serrs.WithSentryContexts(map[string]sentry.Context{
        "custom": map[string]any{
            "key": "value",
        },
    }), 
    // Customize the Sentry tags 
    serrs.WithSentryTags(map[string]string{
        "code": serrs.GetErrorCodeString(err),
    }), 
    // Customize the Sentry Level 
    serrs.WithSentryLevel(sentry.LevelInfo),
)

or


import (
    "github.com/getsentry/sentry-go"
)

func main() {
    sentry.Init(sentry.ClientOptions{
        Dsn: "your-dsn",
    })
    defer sentry.Flush(2 * time.Second)
	
    if err := DoSomething(); err != nil {
        event := serrs.GenerateSentryEvent(err)
        sentry.CaptureEvent(event)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorSurface

func ErrorSurface(err error) string

ErrorSurface returns the surface of err.Error()

func GenerateSentryEvent

func GenerateSentryEvent(err error, ws ...sentryWrapper) *sentry.Event

GenerateSentryEvent is a method to generate a sentry event from an error

func GetErrorCodeString

func GetErrorCodeString(err error) string

GetErrorCodeString returns the error code string attached to the error.

func Is

func Is(err error, target error) bool

Is reports whether the error's tree is the same as the target error.

func New

func New(code Code, msg string) error

New creates a new error with the given message and code.

func Origin

func Origin(err error) error

Origin returns the original error.

func ReportSentry

func ReportSentry(err error, ws ...sentryWrapper)

ReportSentry is a method to report an error to sentry

func ReportSentryWithContext added in v0.0.2

func ReportSentryWithContext(ctx context.Context, err error, ws ...sentryWrapper)

ReportSentryWithContext is a method to report an error to sentry with a context

func SetStackedErrorJsonFormatter

func SetStackedErrorJsonFormatter(f StackedErrorJsonFormatter)

SetStackedErrorJsonFormatter sets the formatter for StackedErrorJson. change the format of the automatically set error

func StackedErrorJson

func StackedErrorJson(err error) []any

StackedErrorJson returns a slice of JSON objects representing the error stack.

func Unwrap

func Unwrap(e error) error

Unwrap returns the cause of the error.

func WithCode

func WithCode(code Code) errWrapper

WithCode returns an error wrapper that adds a code to the error.

func WithData

func WithData(data CustomData) errWrapper

WithData returns an error wrapper that adds custom data to the error.

func WithMessage

func WithMessage(msg string) errWrapper

WithMessage returns an error wrapper that adds a message to the error.

func WithSentryContexts

func WithSentryContexts(m map[string]sentry.Context) sentryWrapper

WithSentryContexts is a function to add contexts to a sentry event

func WithSentryLevel

func WithSentryLevel(l sentry.Level) sentryWrapper

WithSentryLevel is a function to set the level of a sentry event

func WithSentryTags

func WithSentryTags(m map[string]string) sentryWrapper

WithSentryTags is a function to add tags to a sentry event

func Wrap

func Wrap(err error, ws ...errWrapper) error

Wrap returns an error with a stack trace. errWrapper is a function that adds information to the error.

Types

type Code

type Code interface {
	ErrorCode() string
}

Code is an interface that represents an error code.

func GetErrorCode

func GetErrorCode(err error) (Code, bool)

GetErrorCode returns the error code attached to the error.

type CustomData

type CustomData interface {
	String() string
	Clone() CustomData
}

CustomData is the interface for custom data attached to the error.

func GetCustomData

func GetCustomData(err error) []CustomData

GetCustomData returns the custom data attached to the error.

type DefaultCode added in v0.0.2

type DefaultCode string

DefaultCode is a type that represents an error code as a string.

func (DefaultCode) ErrorCode added in v0.0.2

func (s DefaultCode) ErrorCode() string

type DefaultCustomData

type DefaultCustomData map[string]any

func (DefaultCustomData) Clone

func (d DefaultCustomData) Clone() CustomData

func (DefaultCustomData) String

func (d DefaultCustomData) String() string

type Frames added in v0.0.2

type Frames []uintptr

A Frames represents a program counter inside a stack frames.

type StackedErrorJsonFormatter

type StackedErrorJsonFormatter func(err error) any

Jump to

Keyboard shortcuts

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