Categorygithub.com/memsql/errors
modulepackage
0.1.0
Repository: https://github.com/memsql/errors.git
Documentation: pkg.go.dev

# README

errors - API for creating exceptions and alerting

GoDoc unit tests report card codecov

Install:

go get github.com/memsql/errors

Package errors provides an API for creating exceptions and alerting.

This is intended to be a replacement for Go's standard library errors package. You can import "github.com/memsql/errors" instead of "errors".

Verbose Messages

Use New or Errorf to produce an error that can be formatted with verbose details, including a stack trace. To see the verbose details, format using %+v instead of %v, %s or calling err.Error().

Alert and Capture

Applications can call RegisterCapture to persist errors to logs, a backend database, or a third-party service. When Alert or Alertf is called, the error will be passed to each registered handler, which is responsible for peristing. The capture handler returns an identifier which becomes part of the error message, as a suffix enclosed in square brackets.

Expand and Expunge

The Expand helper adds information to an error, while Expunge is intended to remove information that end users don't need to see. Both are intended to be deferred, using a pattern like this,

func FindThing(id string) (err error) {
  // include the id, and verbose stack, with all errors
  defer errors.Expand(&err, "thing (%q) not found", id)

  // ...
}

When returning errors which wrap other errors, Expunge can be used to hide underlying details, to make a more user-friendly message. It assumes that error message text follows specific conventions, described below.

Message Conventions

Error messages include static parts and dynamic parts. If the error is “/tmp/foo.txt not found”, then “/tmp/foo.txt” is the dynamic part and “not found” is the static part. The dynamic parts present detail to the reader. The static part is useful as well. For example, to determine the line of code where it originated, or how frequently the error is triggered. This package follows a convention to easily work with both static and dynamic parts of error messages:

  • Dynamic text SHOULD appear in parenthesis.

  • Static text SHOULD be grammatically correct, with or without the dynamic parts.

Following these guidelines, the backend might produce an error: “file ("/tmp/foo.txt”) not found”.

The static text should make sense even when the dynamic parts are removed. Imagine running a whole log file through a regular expression that removes the parentheticals. This stripped log file should still make sense to the reader. In our example, the stripped text would be “file not found”, which makes sense (while just “not found” would not make sense).

The colon character “:” has special significance in error message. It implies that an error message has been “wrapped” with another message, providing additional context.

// avoid this!
return errors.Errorf("invalid widget id: %s", id)

// do this
return errors.Errorf("failed to parse widget id (%q): %w", id, err)
  • Error messages SHOULD use the colon “:” only when wrapping another error.

# Functions

Alert sends an error to all registered capture handlers.
Alertf produces an error and alerts.
Errorf produces an error with a formatted message including dynamic arguments.
Expand rewites an error message, when an error is non-nil.
Expunge rewrites an error message, when an error is non-nil.
ExpungeOnce behaves like Expunge(), except that it leaves an exception as-is if the it has already been expunged.
FromPanic produces an error when passed non-nil input.
LogCapture is a simple capture handler that writes exception to log.
New emulates the behavior of stdlib's errors.New(), and includes a stack trace with the error.
Redact removes potential sensitive details from an error, making the message safe to display to an unprivileged user.
RegisterCapture adds a handler to the set that will be invoked each time an error is captured.
No description provided by the author
Walk visits each error in a tree of errors wrapping other errors.
WithStack produces an error that includes a stack trace.
Wrap returns nil when the exception passed in is nil; otherwise, it returns an error with message text that wraps exception.
Wrapf returns nil when the exception passed in is nil; otherwise, it produces text based on the format string and arguments, and returns an error with that text that wraps the exception.

# Variables

No description provided by the author
CaptureTimeout limits how long to wait for a capture ID to be returned from a capture handler.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Captured marks and wraps an error that has been "captured", meaning it has been logged verbosely or stored in a way that can be looked up later.
Error implements Go's error interface; and can format verbose messages, including stack traces.
No description provided by the author
A Throttle will alert, until threshold is reached.

# Interfaces

StackTracer is exported so that external packages can detect whether a err has stack trace associated.

# Type aliases

CaptureFunc is a handler invoked to capture an error.
No description provided by the author
No description provided by the author
No description provided by the author
String provides a simple mechanism to define const errors.