gtserror

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 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 IsMalformed added in v0.13.0

func IsMalformed(err error) bool

IsMalformed checks error for a stored "malformed" flag. For example an error from an incoming ActivityStreams type conversion.

func IsNotFound added in v0.13.0

func IsNotFound(err error) bool

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

func IsSMTP added in v0.13.0

func IsSMTP(err error) bool

IsSMTP checks error for a stored "smtp" flag. For example an error from outgoing SMTP email attempt.

func IsUnretrievable added in v0.13.0

func IsUnretrievable(err error) bool

IsUnretrievable indicates that a call to retrieve a resource (account, status, attachment, 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 IsWrongType added in v0.13.0

func IsWrongType(err error) bool

IsWrongType 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.

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 SetMalformed added in v0.13.0

func SetMalformed(err error) error

SetMalformed will wrap the given error to store a "malformed" flag, returning wrapped error. See IsMalformed() for example use-cases.

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 IsNotFound() for example use-cases.

func SetSMTP added in v0.13.0

func SetSMTP(err error) error

SetSMTP will wrap the given error to store an "smtp" flag, returning wrapped error. See IsSMTP() 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 IsWrongType() 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 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.

Types

type ErrorType added in v0.8.0

type ErrorType string

ErrorType denotes the type of an error, if set.

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 NewErrorRequestTimeout added in v0.13.0

func NewErrorRequestTimeout(original error) WithCode

NewErrorRequestTimeout returns an ErrorWithCode 408 with the given original error. This error type should only be used when the server has decided to hang up a client request after x amount of time, to avoid keeping extremely slow client requests open.

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