gtserror

package
v0.12.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusClientClosedRequest     = 499
	StatusTextClientClosedRequest = "Client Closed Request"
)

Custom http response codes + text that aren't included in the net/http package.

View Source
const Caller = true

Caller returns whether created errors will prepend calling function name.

Variables

This section is empty.

Functions

func New added in v0.10.0

func New(msg string) error

New returns a new error, prepended with caller function name if gtserror.Caller is enabled.

func NewFromResponse added in v0.10.0

func NewFromResponse(rsp *http.Response) error

NewResponseError crafts an error from provided HTTP response including the method, status and body (if any provided). This will also wrap the returned error using WithStatusCode() and will include the caller function name as a prefix.

func Newf added in v0.10.0

func Newf(msgf string, args ...any) error

Newf returns a new formatted error, prepended with caller function name if gtserror.Caller is enabled.

func NewfAt added in v0.11.0

func NewfAt(calldepth int, msgf string, args ...any) error

NewfAt returns a new formatted error with the given calldepth+1, useful when you want to wrap an error from within an anonymous function or utility function, but preserve the name in the error of the wrapping function that did the calling.

Provide calldepth 2 to prepend only the name of the current containing function, 3 to prepend the name of the function containing *that* function, and so on.

This function is just exposed for dry-dick optimization purposes. Most callers should just call Newf instead.

func NotFound added in v0.8.0

func NotFound(err error) bool

NotFound checks error for a stored "not found" flag. For example an error from an outgoing HTTP request due to DNS lookup.

func SetNotFound added in v0.8.0

func SetNotFound(err error) error

SetNotFound will wrap the given error to store a "not found" flag, returning wrapped error. See NotFound() for example use-cases.

func SetType added in v0.8.0

func SetType(err error, errType ErrorType) error

SetType will wrap the given error to store a "type" value, returning wrapped error. See Type() for example use-cases.

func SetUnretrievable added in v0.10.0

func SetUnretrievable(err error) error

SetUnretrievable will wrap the given error to store an "unretrievable" flag, returning wrapped error. See "Unretrievable" for example use-cases.

func SetWrongType added in v0.10.0

func SetWrongType(err error) error

SetWrongType will wrap the given error to store a "wrong type" flag, returning wrapped error. See "WrongType" for example use-cases.

func StatusCode added in v0.8.0

func StatusCode(err error) int

StatusCode checks error for a stored status code value. For example an error from an outgoing HTTP request may be stored, or an API handler expected response status code may be stored.

func Unretrievable added in v0.10.0

func Unretrievable(err error) bool

Unretrievable checks error for a stored "unretrievable" flag.

Unretrievable indicates that a call to retrieve a resource (account, status, etc) could not be fulfilled, either because it was not found locally, or because some prerequisite remote resource call failed, making it impossible to return the item.

func WithStatusCode added in v0.8.0

func WithStatusCode(err error, code int) error

WithStatusCode will wrap the given error to store provided status code, returning wrapped error. See StatusCode() for example use-cases.

func WrongType added in v0.10.0

func WrongType(err error) bool

WrongType checks error for a stored "wrong type" flag. Wrong type indicates that an ActivityPub URI returned a type we weren't expecting: Statusable instead of Accountable, or vice versa, for example.

Types

type ErrorType added in v0.8.0

type ErrorType string

ErrorType denotes the type of an error, if set.

const (

	// Types returnable from Type(...).
	TypeSMTP ErrorType = "smtp" // smtp (mail)
)

func Type added in v0.8.0

func Type(err error) ErrorType

Type checks error for a stored "type" value. For example an error from sending an email may set a value of "smtp" to indicate this was an SMTP error.

type MultiError added in v0.7.0

type MultiError []error

MultiError allows encapsulating multiple errors under a singular instance, which is useful when you only want to log on errors, not return early / bubble up.

func NewMultiError added in v0.11.0

func NewMultiError(capacity int) MultiError

NewMultiError returns a *MultiError with the capacity of its underlying error slice set to the provided value.

This capacity can be exceeded if necessary, but it saves a teeny tiny bit of memory if callers set it correctly.

If you don't know in advance what the capacity must be, just use new(MultiError) instead.

func (*MultiError) Append added in v0.7.0

func (m *MultiError) Append(err error)

Append the given error to the MultiError.

func (*MultiError) Appendf added in v0.7.0

func (m *MultiError) Appendf(format string, args ...any)

Append the given format string to the MultiError.

It is valid to use %w in the format string to wrap any other errors.

func (MultiError) Combine added in v0.7.0

func (m MultiError) Combine() error

Combine the MultiError into a single error.

Unwrap will work on the returned error as expected.

type WithCode

type WithCode interface {
	// Unwrap returns the original error.
	// This should *NEVER* be returned to a client as it may contain sensitive information.
	Unwrap() error
	// Error serializes the original internal error for debugging within the GoToSocial logs.
	// This should *NEVER* be returned to a client as it may contain sensitive information.
	Error() string
	// Safe returns the API-safe version of the error for serialization towards a client.
	// There's not much point logging this internally because it won't contain much helpful information.
	Safe() string
	//  Code returns the status code for serving to a client.
	Code() int
}

WithCode wraps an internal error with an http code, and a 'safe' version of the error that can be served to clients without revealing internal business logic.

A typical use of this error would be to first log the Original error, then return the Safe error and the StatusCode to an API caller.

func NewErrorBadRequest

func NewErrorBadRequest(original error, helpText ...string) WithCode

NewErrorBadRequest returns an ErrorWithCode 400 with the given original error and optional help text.

func NewErrorClientClosedRequest added in v0.10.0

func NewErrorClientClosedRequest(original error) WithCode

NewErrorClientClosedRequest returns an ErrorWithCode 499 with the given original error. This error type should only be used when an http caller has already hung up their request. See: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#nginx

func NewErrorConflict added in v0.2.0

func NewErrorConflict(original error, helpText ...string) WithCode

NewErrorConflict returns an ErrorWithCode 409 with the given original error and optional help text.

func NewErrorForbidden

func NewErrorForbidden(original error, helpText ...string) WithCode

NewErrorForbidden returns an ErrorWithCode 403 with the given original error and optional help text.

func NewErrorGone added in v0.6.0

func NewErrorGone(original error, helpText ...string) WithCode

NewErrorGone returns an ErrorWithCode 410 with the given original error and optional help text.

func NewErrorInternalError

func NewErrorInternalError(original error, helpText ...string) WithCode

NewErrorInternalError returns an ErrorWithCode 500 with the given original error and optional help text.

func NewErrorNotAcceptable added in v0.3.5

func NewErrorNotAcceptable(original error, helpText ...string) WithCode

NewErrorNotAcceptable returns an ErrorWithCode 406 with the given original error and optional help text.

func NewErrorNotFound

func NewErrorNotFound(original error, helpText ...string) WithCode

NewErrorNotFound returns an ErrorWithCode 404 with the given original error and optional help text.

func NewErrorUnauthorized added in v0.3.5

func NewErrorUnauthorized(original error, helpText ...string) WithCode

NewErrorUnauthorized returns an ErrorWithCode 401 with the given original error and optional help text.

func NewErrorUnprocessableEntity added in v0.3.5

func NewErrorUnprocessableEntity(original error, helpText ...string) WithCode

NewErrorUnprocessableEntity returns an ErrorWithCode 422 with the given original error and optional help text.

Jump to

Keyboard shortcuts

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