errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDHeader = "X-Request-ID"

RequestIDHeader the header to send the unique request id

Variables

This section is empty.

Functions

func RetrieveSample

func RetrieveSample(r io.ReadCloser) []byte

RetrieveSample retrieves a sample from a reader

Types

type Code

type Code string
const (
	CodeUnknownError Code = "unknown_error"

	CodeInvalidRequest     Code = "invalid_request"
	CodeForbidden          Code = "forbidden"
	CodeUnauthorized       Code = "unauthorized"
	CodeNotFound           Code = "not_found"
	CodeInvalidOperation   Code = "invalid_operation"
	CodeConflict           Code = "resource_conflict"
	CodeInvalidContentType Code = "invalid_content_type"
	CodeValidationError    Code = "validation_error"
	CodeRateLimitExceeded  Code = "rate_limit_exceeded"
	CodeServerError        Code = "server_error"
	CodeServerUnavailable  Code = "server_unavailable"

	CodeEncodingError   Code = "encoding_error"
	CodeRequestError    Code = "request_error"
	CodeHTTPClientError Code = "http_client_error"
)

type Error

type Error struct {
	// Code the helpers code defined
	Code Code `json:"code"`
	// StatusCode the HTTP status code (if applicable) from the API response
	StatusCode int `json:"status_code"`
	// Method is the HTTP method
	Method string `json:"method"`
	// Resource the requested resource path
	Resource string `json:"resource"`
	// RequestID the uuid of the request.
	RequestID string `json:"request_id"`
	// Request a sample of the request body.
	// this will be nil if there was either no body on the request
	// or the error is not associated with performing the API request.
	Request []byte `json:"request,omitempty"`
	// Response a sample of the response body.
	Response []byte `json:"response"`
	// Source this is the underlined error if applicable.
	Source string `json:"source,omitempty"`
}

Error a formatted API helpers

func FromRequest

func FromRequest(req *http.Request) *Error

FromRequest generates an error from a http.Request. useful when an error occurs before the Response is generated.

func FromRequestAndSource

func FromRequestAndSource(req *http.Request, code Code, src error) *Error

FromRequestAndSource generates a wrapped error from a HTTP request, overriding the code and source error.

func FromResponse

func FromResponse(req *http.Request, resp *http.Response, r io.ReadCloser) *Error

FromResponse generates an Error from a request and response. The response body is supplied separately as the stream retrieved in the response is a one-time read, providing it separately ensures we account for this and always return a usable reader.

func FromSource

func FromSource(code Code, path, method, id string, src error) *Error

FromSource generates an wrapped error from a source error.

func (*Error) Error

func (e *Error) Error() string

Error implements helpers interface.

Jump to

Keyboard shortcuts

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