apierror

package
v2.15.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: BSD-3-Clause Imports: 11 Imported by: 101

Documentation ΒΆ

Overview ΒΆ

Package apierror implements a wrapper error for parsing error details from API calls. Both HTTP & gRPC status errors are supported.

For examples of how to use APIError with client libraries please reference [Inspecting errors](https://pkg.go.dev/cloud.google.com/go#hdr-Inspecting_errors) in the client library documentation.

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var ErrMessageNotFound = errors.New("message not found")

ErrMessageNotFound is used to signal ExtractProtoMessage found no matching messages.

Functions ΒΆ

This section is empty.

Types ΒΆ

type APIError ΒΆ

type APIError struct {
	// contains filtered or unexported fields
}

APIError wraps either a gRPC Status error or a HTTP googleapi.Error. It implements error and Status interfaces.

func FromError ΒΆ

func FromError(err error) (*APIError, bool)

FromError parses a Status error or a googleapi.Error and builds an APIError, wrapping the provided error in the new APIError. It returns false if neither Status nor googleapi.Error can be parsed.

func ParseError ΒΆ added in v2.7.0

func ParseError(err error, wrap bool) (*APIError, bool)

ParseError parses a Status error or a googleapi.Error and builds an APIError. If wrap is true, it wraps the error in the new APIError. It returns false if neither Status nor googleapi.Error can be parsed.

func (*APIError) Details ΒΆ

func (a *APIError) Details() ErrDetails

Details presents the error details of the APIError.

func (*APIError) Domain ΒΆ

func (a *APIError) Domain() string

Domain returns the domain in an ErrorInfo. If ErrorInfo is nil, it returns an empty string.

func (*APIError) Error ΒΆ

func (a *APIError) Error() string

Error returns a readable representation of the APIError.

func (*APIError) GRPCStatus ΒΆ

func (a *APIError) GRPCStatus() *status.Status

GRPCStatus extracts the underlying gRPC Status error. This method is necessary to fulfill the interface described in https://pkg.go.dev/google.golang.org/grpc/status#FromError.

For errors that originated as an HTTP-based googleapi.Error, GRPCStatus() returns a status that attempts to map from the original HTTP code to an equivalent gRPC status code. For use cases where you want to avoid this behavior, error unwrapping can be used.

func (*APIError) HTTPCode ΒΆ added in v2.9.0

func (a *APIError) HTTPCode() int

HTTPCode returns the underlying HTTP response status code. This method returns `-1` if the underlying error is a google.golang.org/grpc/status.Status. To check gRPC error codes use google.golang.org/grpc/status.Code.

func (*APIError) Metadata ΒΆ

func (a *APIError) Metadata() map[string]string

Metadata returns the metadata in an ErrorInfo. If ErrorInfo is nil, it returns nil.

func (*APIError) Reason ΒΆ

func (a *APIError) Reason() string

Reason returns the reason in an ErrorInfo. If ErrorInfo is nil, it returns an empty string.

func (*APIError) Unwrap ΒΆ

func (a *APIError) Unwrap() error

Unwrap extracts the original error.

type ErrDetails ΒΆ

type ErrDetails struct {
	ErrorInfo           *errdetails.ErrorInfo
	BadRequest          *errdetails.BadRequest
	PreconditionFailure *errdetails.PreconditionFailure
	QuotaFailure        *errdetails.QuotaFailure
	RetryInfo           *errdetails.RetryInfo
	ResourceInfo        *errdetails.ResourceInfo
	RequestInfo         *errdetails.RequestInfo
	DebugInfo           *errdetails.DebugInfo
	Help                *errdetails.Help
	LocalizedMessage    *errdetails.LocalizedMessage

	// Unknown stores unidentifiable error details.
	Unknown []interface{}
}

ErrDetails holds the google/rpc/error_details.proto messages.

func (ErrDetails) ExtractProtoMessage ΒΆ added in v2.5.1

func (e ErrDetails) ExtractProtoMessage(v proto.Message) error

ExtractProtoMessage provides a mechanism for extracting protobuf messages from the Unknown error details. If ExtractProtoMessage finds an unknown message of the same type, the content of the message is copied to the provided message.

ExtractProtoMessage will return ErrMessageNotFound if there are no message matching the protocol buffer type of the provided message.

func (ErrDetails) String ΒΆ

func (e ErrDetails) String() string

Directories ΒΆ

Path Synopsis
internal

Jump to

Keyboard shortcuts

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