httperr

package
v0.0.70 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 5 Imported by: 5

Documentation

Index

Constants

View Source
const (
	UnknownID               = "http.response.status.unknown"                  // ID for Unknown status
	BadRequestID            = "http.response.status.bad_request"              // ID for Bad Request status
	UnauthorizedID          = "http.response.status.unauthorized"             // ID for Unauthorized status
	ForbiddenID             = "http.response.status.forbidden"                // ID for Forbidden status
	NotFoundID              = "http.response.status.not_found"                // ID for Not Found status
	MethodNotAllowedID      = "http.response.status.method_not_allowed"       // ID for Method Not Allowed status
	TooManyRequestsID       = "http.response.status.too_many_requests"        // ID for Too Many Requests status
	RequestEntityTooLargeID = "http.response.status.request_entity_too_large" // ID for Request Entity Too Large status
	InternalServerErrorID   = "http.response.status.internal_server_error"    // ID for Internal Server Error status
	ConflictID              = "http.response.status.conflict"                 // ID for Conflict status
	RequestTimeoutID        = "http.response.status.request_timeout"          // ID for Request Timeout status
)

Define constant IDs for various Error status codes

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(format string, obj ...any) error

BadRequest generates a 400 error.

func Code

func Code(code int32, detail string) error

Code generates an error for a given code

func CodeFormat

func CodeFormat(code int32, format string, args ...any) error

CodeFormat generates an error for a given code

func Conflict

func Conflict(format string, obj ...any) error

Conflict generates a 409 error.

func Equal

func Equal(err1 error, err2 error) bool

Equal tries to compare errors, which are equal if they have the same Code

func Forbidden

func Forbidden(format string, obj ...any) error

Forbidden generates a 403 error.

func InternalServerError

func InternalServerError(format string, obj ...any) error

InternalServerError generates a 500 error.

func LookupID

func LookupID(code int32) (string, bool)

LookupID looks up the identifier corresponding to the given code. This function uses a read lock to ensure safe concurrent access to shared resources. Parameters: code - The integer value of the code to look up.

Returns: The string identifier corresponding to the code.

func MethodNotAllowed

func MethodNotAllowed(format string, obj ...any) error

MethodNotAllowed generates a 405 error.

func New

func New(id string, code int32, detail string) error

New generates an Error error

func NewFormat

func NewFormat(id string, code int32, format string, args ...any) error

NewFormat generates an Error error

func NotFound

func NotFound(format string, obj ...any) error

NotFound generates a 404 error.

func RegisterCode

func RegisterCode(code int32, id string)

RegisterCode register a new error code with the given ID,or overwriting any existing one

func RequestEntityTooLarge

func RequestEntityTooLarge(format string, obj ...any) error

RequestEntityTooLarge generates a 413 error.

func Timeout

func Timeout(format string, obj ...any) error

Timeout generates a 408 error.

func TooManyRequests

func TooManyRequests(format string, obj ...any) error

TooManyRequests generates a 429 error.

func Unauthorized

func Unauthorized(format string, obj ...any) error

Unauthorized generates a 401 error.

Types

type Error

type Error struct {
	ID     string `json:"id,omitempty"`
	Code   int32  `json:"code,omitempty"`
	Detail string `json:"detail,omitempty"`
}

Error customize the error structure for implementation errors.Error interface

func FromError

func FromError(err error) *Error

FromError tries to convert a Go error to *Error

func Parse

func Parse(err string) *Error

Parse tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.

func (*Error) Error

func (obj *Error) Error() string

Error returns the JSON representation of the error

func (*Error) Is

func (obj *Error) Is(err error) bool

func (*Error) Status

func (obj *Error) Status() string

func (*Error) String

func (obj *Error) String() string

Jump to

Keyboard shortcuts

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