Categorygithub.com/vortahq/rich-error
modulepackage
0.6.7
Repository: https://github.com/vortahq/rich-error.git
Documentation: pkg.go.dev

# README

Rich Error

Rich Error is an error library that:

  • allows nested errors
  • makes it easy to store extra information along errors
  • provides an easy to use API
  • follows go1.13 errors conventions

Installation

As we're publishing this module in our private repo, you'll need to do following steps before running go get:

  1. Add repo address to GOPRIVATE env, to prevent go from using the proxy server:

    go env -w GOPRIVATE=github.com/vortahq/rich-error
    

    This is a shortcut for GONOPROXY and GONOSUMDB.

  2. Add your Gitlab credentials to allow git to access to your gitlab account using http API

    After creating a new Personal Access Token with write_repository permission from "Profile" -> "Access Tokens", create a ~/.netrc file with the following content

    machine gitlab.com
    login oauth2
    password <your access token>
    
    

    ~/.netrc is the file that curl uses for authentication purposes. As git uses curl internally this will allow git to access to your repository by git-over-http

Public API

For more information about public API checkout contract.go

Usage

Using rich error is easy, you can create a new RichError using richerror.New("error message"). It automatically adds runtime information to your error (like line number, file name, etc.). If you wish to add extra information to your error you can use following methods. You can chain them together and except NilIfNoError their ordering is not important.

WithFields & WithField

These methods add a metadata (or a number of metadata) to your error. You can access them using RichError.Metadata() method. These metadata are a good place to store information like request ID, user ID, etc. and allow you to debug errors more efficiently.

WithKind & WithLevel

These methods allow you to assign a kind and level to your errors. Kind and Level are predefined enums and you have to choose from provided options. This allows you to hint to caller functions that the error is recoverable or not, or what kind of issue caused the error.

WithOperation

Operation is a hint that you can store in error to make debugging and grouping of errors easier.

WithType

WithType lets you assign a type to your error. The type stores information that you're going to show to the user. Because each service has it's own error types, You have to define your type struct yourself, it just have to implements Type interface. You'll probably end up with something like:

type MyErrorType string

func (t *MyErrorType) string {
    return string(t)
}

const (
    ErrX = MyErrorType("x")
)

WithError

WithError allows you to wrap another error inside your error. It follows go 1.13 conventions and supports Unwrap, Is, and As methods.

WithError tries to fill type, level, type, operation, etc. if they haven't been filled explicitly.

NilIfNoError

NilIfNoError returns nil if the underling error is not present. It helps you avoid if err != nil check as much as possible.

JsonMode

JsonMode is a flag controlling the format of generated output, the default format is string. You can change the output format by setting the JsonMode to true.

Helpers

This package provides a set of helper function and structs to help users to utilize the full power of the RichError. Currently, there are following constructs:

  • gRPC interception which uses RichError's Kind to determine gRPC's status code.
  • Logger which tries to log RichErrors in the most complete way (based on the logger given to it).
  • Sentry which reports errors to sentry using sentry-go and uses RichErrors metadata to enrich the reported errors.

# Functions

No description provided by the author
No description provided by the author
New creates a new richError.

# Constants

grpc: ALREADY_EXISTS - http 409 Conflict.
grpc: CANCELLED - http 500 Internal Server Error.
No description provided by the author
No description provided by the author
No description provided by the author
grpc: INTERNAL - http 500 Internal Server Error.
No description provided by the author
grpc: INVALID_ARGUMENT - http 400 Bad Request.
grpc: NOT_FOUND - http 404 Not Found.
grpc: PERMISSION_DENIED - http 403 Forbidden.
grpc: DEADLINE_EXCEEDED - http 500 Internal Server Error.
grpc: RESOURCE_EXHAUSTED - http 429 Too Many Requests.
grpc: UNAUTHENTICATED - http 401 Unauthorized.
No description provided by the author
grpc: UNAVAILABLE - http 503 Service Unavailable.
No description provided by the author
grpc: UNIMPLEMENTED - http 501 Not Implemented.
grpc: UNKNOWN - http 500 Internal Server Error.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

ChainLogger is an ErrorLogger that upon being called will call a set of loggers one after the other.
GRPCInterceptors is a helper that provides unary and stream grpc interceptors that will catch and log errors of your grpc server.
Logger is a struct that provides an ErrorLogger.
RuntimeInfo stores runtime information about the code.
SentryLogger is a ErrorLogger that logs to sentry.

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RichError is a richer type of error that holds runtime information with itself.
Type stores information that we want to show to user.

# Type aliases

Deprecated: CodeInfo has been renamed to RuntimeInfo and will be removed in V2.
Kind hints about underlying cause of error.
Level identifies severity of the error.
Metadata stores metadata of error.
Operation can be used to group or organize error.
No description provided by the author