errors

package
v0.0.0-...-2cbf6af Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package errors provides simple error handling primitives.

The traditional error handling idiom in Go is roughly akin to

if err != nil {
        return err
}

which when applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.

Adding context to an error

The errors.Wrap function returns a new error that adds context to the original error by recording a stack trace at the point Wrap is called, together with the supplied message. For example

_, err := ioutil.ReadAll(r)
if err != nil {
        return errors.Wrap(err, "read failed")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func New

func New(message string) error

New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.

func Newf

func Newf(format string, args ...interface{}) error

Newf returns an error that formats according to a format specifier. Each call to Newf returns a distinct error value even if the text is identical.

func Status

func Status(code codes.Code, message string, details ...*errdetails.ErrorInfo) error

Status represents an RPC status code, message, and details. It is immutable. Code represent a set of Status code tht will be translated from gRPC to HTTP if the error is meant to be consumed by an HTTP client.

| HTTP | gRPC | Description | |------|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 200 | OK | No error. | | 400 | INVALID_ARGUMENT | Client specified an invalid argument. Check error message and error details for more information. | | 400 | FAILED_PRECONDITION | Request can not be executed in the current system state, such as deleting a non-empty directory. | | 400 | OUT_OF_RANGE | Client specified an invalid range. | | 401 | UNAUTHENTICATED | Request not authenticated due to missing, invalid, or expired OAuth token. | | 403 | PERMISSION_DENIED | Client does not have sufficient permission. This can happen because the OAuth token does not have the right scopes, the client doesn't have permission. | | 404 | NOT_FOUND | A specified resource is not found. | | 409 | ABORTED | Concurrency conflict, such as read-modify-write conflict. | | 409 | ALREADY_EXISTS | The resource that a client tried to create already exists. | | 429 | RESOURCE_EXHAUSTED | Either out of resource quota or reaching rate limiting. | | 499 | CANCELLED | Request cancelled by the client. | | 500 | DATA_LOSS | Unrecoverable data loss or data corruption. The client should report the error to the user. | | 500 | UNKNOWN | Unknown server error. Typically a server bug. | | 500 | INTERNAL | Internal server error. Typically a server bug. | | 501 | NOT_IMPLEMENTED | API method not implemented by the server. | | 502 | N/A | Network error occurred before reaching the server. Typically a network outage or misconfiguration. | | 503 | UNAVAILABLE | Service unavailable. Typically the server is down. | | 504 | DEADLINE_EXCEEDED | Request deadline exceeded. This will happen only if the caller sets a deadline that is shorter than the method's default deadline. |

details provide details error information appended to the status. It is optional but always good to add detail when applicable.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. If err is nil, Wrapf returns nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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