fail

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists returns true if the underlying HTTP status code of 'err' is 409. This will be true for any error you created using the AlreadyExists() function.

func IsBadCredentials

func IsBadCredentials(err error) bool

IsBadCredentials returns true if the underlying HTTP status code of 'err' is 401. This will be true for any error you created using the BadCredentials() function.

func IsBadGateway

func IsBadGateway(err error) bool

IsBadGateway returns true if the underlying HTTP status code of 'err' is 502. This will be true for any error you created using the BadGateway() function.

func IsBadRequest

func IsBadRequest(err error) bool

IsBadRequest returns true if the underlying HTTP status code of 'err' is 400. This will be true for any error you created using the BadRequest() function.

func IsGone

func IsGone(err error) bool

IsGone returns true if the underlying HTTP status code of 'err' is 410. This will be true for any error you created using the Gone() function.

func IsInternalServiceError

func IsInternalServiceError(err error) bool

IsInternalServiceError returns true if the underlying HTTP status code of 'err' is 500. This will be true for any error you created using the InternalServiceError() function.

func IsMethodNotAllowed

func IsMethodNotAllowed(err error) bool

IsMethodNotAllowed returns true if the underlying HTTP status code of 'err' is 405. This will be true for any error you created using the MethodNotAllowed() function.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the underlying HTTP status code of 'err' is 404. This will be true for any error you created using the NotFound() function.

func IsNotImplemented

func IsNotImplemented(err error) bool

IsNotImplemented returns true if the underlying HTTP status code of 'err' is 501. This will be true for any error you created using the Throttled() function.

func IsPaymentRequired

func IsPaymentRequired(err error) bool

IsPaymentRequired returns true if the underlying HTTP status code of 'err' is 402. This will be tru for any error you created using the PaymentRequired() function.

func IsPermissionDenied

func IsPermissionDenied(err error) bool

IsPermissionDenied returns true if the underlying HTTP status code of 'err' is 403. This will be true for any error you created using the PermissionDenied() function.

func IsThrottled

func IsThrottled(err error) bool

IsThrottled returns true if the underlying HTTP status code of 'err' is 429. This will be true for any error you created using the Throttled() function.

func IsTimeout

func IsTimeout(err error) bool

IsTimeout returns true if the underlying HTTP status code of 'err' is 408. This will be true for any error you created using the Timeout() function.

func IsTooLarge

func IsTooLarge(err error) bool

IsTooLarge returns true if the underlying HTTP status code of 'err' is 413. This will be true for any error you created using the TooLarge() function.

func IsUnavailable

func IsUnavailable(err error) bool

IsUnavailable returns true if the underlying HTTP status code of 'err' is 503. This will be true for any error you created using the Unavailable() function.

func IsUnexpected

func IsUnexpected(err error) bool

IsUnexpected returns true if the underlying HTTP status code of 'err' is 500. This will be true for any error you created using the Unexpected() function.

func IsUnsupportedFormat

func IsUnsupportedFormat(err error) bool

IsUnsupportedFormat returns true if the underlying HTTP status code of 'err' is 415. This will be true for any error you created using the UnsupportedFormat() function.

func Status

func Status(err error) int

Status looks for either a Status(), StatusCode(), or Code() method on the error to figure out the most appropriate HTTP status code for it. If the error doesn't have any of those methods then we'll just assume that it is a 500 error.

Types

type Error added in v0.1.4

type Error StatusError

Error is simply a shorthand for the fail package's StatusError that includes an HTTP status code w/ your error message.

type ErrorHandler

type ErrorHandler func(err error)

ErrorHandler is the generic function signature for something that accepts errors that occur in the bowels of the framework. It gives you a chance to log or deal with them as you see fit. These are typically asynchronous things where you don't have any handle over the control flow, but you don't want to lose these events.

type Group

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

A Group is a collection of goroutines working on subtasks that are part of the same overall task.

A zero Group is valid, has no limit on the number of active goroutines, and does not cancel on error.

func NewGroup

func NewGroup(ctx context.Context) (*Group, context.Context)

NewGroup returns a new Group and an associated Context derived from ctx.

The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.

func (*Group) Go

func (g *Group) Go(f func() error)

Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.

The first call to return a non-nil error cancels the group's context, if the group was created by calling WithContext. The error will be returned by Wait.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

type StatusError

type StatusError struct {
	// Status is the HTTP status code that most closely describes this error.
	Status int `json:"Status"`
	// Message is the human-readable error message.
	Message string `json:"Message"`
}

StatusError is an error that maintains not just an error message but an HTTP compatible status code indicating the type/class of error. It's useful for helping you figure out downstream if an occurred because the user didn't have rights or if some record did not exist.

func AlreadyExists

func AlreadyExists(messageFormat string, args ...any) StatusError

AlreadyExists is a 409-style error that is used when attempting to create some record/resource, but there is already a duplicate instance in existence.

func BadCredentials

func BadCredentials(messageFormat string, args ...any) StatusError

BadCredentials is a 401-style error that indicates that the caller either didn't provide credentials when necessary or they did, but the credentials were invalid for some reason. This corresponds to the HTTP "unauthorized" status, but we prefer this name because this type of failure has nothing to do with authorization, and it's more clear what aspect of the request has failed.

func BadGateway

func BadGateway(messageFormat string, args ...any) StatusError

BadGateway is a 502-style error that indicates that some upstream resource was not available. Your code is working fine, but another service you're dependent on is not.

func BadRequest

func BadRequest(messageFormat string, args ...any) StatusError

BadRequest is a 400-style error that indicates that some aspect of the request was either ill-formed or failed validation. This could be an ill-formed function parameter, a bad HTTP body, etc.

func Gone

func Gone(messageFormat string, args ...any) StatusError

Gone is a 410-style error that is used to indicate that something used to exist, but doesn't anymore.

func InternalServerError

func InternalServerError(messageFormat string, args ...any) StatusError

InternalServerError is a generic 500-style catch-all error for failures you don't know what to do with.

func MethodNotAllowed

func MethodNotAllowed(messageFormat string, args ...any) StatusError

MethodNotAllowed is a 405-style error that indicates that the wrong HTTP method was used.

func New

func New(status int, messageFormat string, args ...any) StatusError

New creates an error that maps directly to an HTTP status so if your method results in this error, it will result in the same 'status' in your HTTP response. While you can do this for more obscure HTTP failure statuses like "payment required", it's typically a better idea to use the error functions BadRequest(), PermissionDenied(), etc. as it provides proper status codes and results in more readable code.

func NotFound

func NotFound(messageFormat string, args ...any) StatusError

NotFound is a 404-style error that indicates that some record/resource could not be located.

func NotImplemented

func NotImplemented(messageFormat string, args ...any) StatusError

NotImplemented is a 501-style error that indicates that the resource/logic required to fulfill the request has not been added yet.

func PaymentRequired

func PaymentRequired(messageFormat string, args ...any) StatusError

PaymentRequired is a 402-style error that indicates that you must provide payment to access the given resource or perform the given task. Greedy bastard :)

func PermissionDenied

func PermissionDenied(messageFormat string, args ...any) StatusError

PermissionDenied is a 403-style error that indicates that the caller does not have rights/clearance to perform any part of the operation.

func Throttled

func Throttled(messageFormat string, args ...any) StatusError

Throttled is a 429-style error that indicates that the caller has exceeded the number of requests, amount of resources, etc allowed over some time period. The failure should indicated to the caller that the failure is due to some throttle that prevented the operation from even occurring.

func Timeout

func Timeout(messageFormat string, args ...any) StatusError

Timeout is a 408-style error that indicates that some operation exceeded its allotted time/deadline.

func TooLarge

func TooLarge(messageFormat string, args ...any) StatusError

TooLarge is a 413-style error that is used to indicate that some resource/entity is too big.

func Unavailable

func Unavailable(messageFormat string, args ...any) StatusError

Unavailable is a 503-style error that indicates that some aspect of the server/service is unavailable. This could be something like DB connection failures, some third party service being down, etc.

func Unexpected

func Unexpected(messageFormat string, args ...any) StatusError

Unexpected is a generic 500-style catch-all error for failures you don't know what to do with. This is exactly the same as calling InternalServerError(), just more concise in your code.

func UnsupportedFormat

func UnsupportedFormat(messageFormat string, args ...any) StatusError

UnsupportedFormat is a 415-style error that is used to indicate that the media/content type of some input is not valid. For instance, the user uploads an "image/bmp" but you only support PNG and JPG files.

func (StatusError) Error

func (r StatusError) Error() string

Error returns the underlying error message.

func (StatusError) StatusCode

func (r StatusError) StatusCode() int

StatusCode returns the most relevant HTTP-style status code describing this type of error.

Jump to

Keyboard shortcuts

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