middleware

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorMiddleware added in v0.1.0

func ErrorMiddleware(next http.Handler) http.Handler

ErrorMiddleware allows for Errors to be attached to a request. It uses a global cache with the request id as a key, it therefore requires chi`s RequestId middleware!

If errors are present a 500 code will be returned and the errors encoded as json.

The GetErrors function and the Errors interface can be used without this middleware (only requiring chi`s RequestId) so you can write your own error handler!

Types

type Errors

type Errors interface {
	// Id returns the request id given to the context`s request by chi`s RequestId middlware
	Id() string
	// Errors returns the attached errors
	Errors() []error
	// AddError can be used to add an error
	AddError(err error)

	// Done should only be called once the response has reached to error handler,
	// and the errors can be deleted from the cache.
	//
	// Since this clears the cache for the associated request id,
	// using Errors or AddError after this call will cause a panic!
	Done()
}

Errors can be used to read and add errors to the error cache for this request!

This requires only chi`s RequestId middleware, while you can use the ErrorMiddleware to handle the returned errors you can also write your own!

func GetErrors added in v0.0.4

func GetErrors(ctx context.Context) Errors

GetErrors retrieves the Errors interface for a give request`s context.

This requires only chi`s RequestId middleware, while you can use the ErrorMiddleware to handle the returned errors you can also write your own!

Jump to

Keyboard shortcuts

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