Categorygithub.com/ryomak/serrs
modulepackage
1.0.0
Repository: https://github.com/ryomak/serrs.git
Documentation: pkg.go.dev

# 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)
    }
}

# Functions

ErrorSurface returns the surface of err.Error().
GenerateSentryEvent is a method to generate a sentry event from an error.
GetCustomData returns the custom data attached to the error.
GetErrorCode returns the error code attached to the error.
GetErrorCodeString returns the error code string attached to the error.
Is reports whether the error's tree is the same as the target error.
New creates a new error with the given message and code.
Origin returns the original error.
ReportSentry is a method to report an error to sentry.
ReportSentryWithContext is a method to report an error to sentry with a context.
SetStackedErrorJsonFormatter sets the formatter for StackedErrorJson.
StackedErrorJson returns a slice of JSON objects representing the error stack.
Unwrap returns the cause of the error.
WithCode returns an error wrapper that adds a code to the error.
WithData returns an error wrapper that adds custom data to the error.
WithMessage returns an error wrapper that adds a message to the error.
WithSentryContexts is a function to add contexts to a sentry event.
WithSentryLevel is a function to set the level of a sentry event.
WithSentryTags is a function to add tags to a sentry event.
Wrap returns an error with a stack trace.

# Interfaces

Code is an interface that represents an error code.
CustomData is the interface for custom data attached to the error.

# Type aliases

DefaultCode is a type that represents an error code as a string.
No description provided by the author
A Frames represents a program counter inside a stack frames.
No description provided by the author