diff --git a/errors.go b/errors.go index a8a9b45..95fc04b 100644 --- a/errors.go +++ b/errors.go @@ -7,6 +7,7 @@ import ( const defaultCode = http.StatusInternalServerError +// Deprecated: Please use v2 of this library var DefaultErrorRegistry = NewErrorRegistry() type ( @@ -16,16 +17,20 @@ type ( // CustomErrorHandler is the template for unexported errors. For example binding.SliceValidationError // or uuid.invalidLengthError +// Deprecated: Please use v2 of this library type CustomErrorHandler[R any] func(err error) (int, R) // ErrorStringHandler is the template for string errors that don't have their own object available. For example // "record not found" or "invalid input" +// Deprecated: Please use v2 of this library type ErrorStringHandler[R any] func(err string) (int, R) // ErrorHandler is the template of an error handler in the ErrorRegistry. The E type is the error type that // the handler is registered for. The R type is the type of the response body. +// Deprecated: Please use v2 of this library type ErrorHandler[E error, R any] func(E) (int, R) +// Deprecated: Please use v2 of this library func NewErrorRegistry() *ErrorRegistry { registry := &ErrorRegistry{ handlers: make(map[string]internalHandler), @@ -46,6 +51,7 @@ func NewErrorRegistry() *ErrorRegistry { return registry } +// Deprecated: Please use v2 of this library type ErrorRegistry struct { // handlers are used when we know the type of the error handlers map[string]internalHandler @@ -60,6 +66,7 @@ type ErrorRegistry struct { DefaultResponse any } +// Deprecated: Please use v2 of this library func (e *ErrorRegistry) SetDefaultResponse(code int, response any) { e.DefaultCode = code e.DefaultResponse = response @@ -68,6 +75,8 @@ func (e *ErrorRegistry) SetDefaultResponse(code int, response any) { // NewErrorResponse Returns an error response using the DefaultErrorRegistry. If no specific handler could be found, // it will return the defaults. It returns an HTTP status code and a response object. // +// Deprecated: Please use v2 of this library +// //nolint:gocritic // Unnamed return arguments are described func NewErrorResponse(err error) (int, any) { return NewErrorResponseFrom(DefaultErrorRegistry, err) @@ -76,6 +85,8 @@ func NewErrorResponse(err error) (int, any) { // NewErrorResponseFrom Returns an error response using the given registry. If no specific handler could be found, // it will return the defaults. It returns an HTTP status code and a response object. // +// Deprecated: Please use v2 of this library +// //nolint:gocritic // Unnamed return arguments are described func NewErrorResponseFrom(registry *ErrorRegistry, err error) (int, any) { errorType := fmt.Sprintf("%T", err) @@ -91,11 +102,13 @@ func NewErrorResponseFrom(registry *ErrorRegistry, err error) (int, any) { } // RegisterErrorHandler registers an error handler in DefaultErrorRegistry. The R type is the type of the response body. +// Deprecated: Please use v2 of this library func RegisterErrorHandler[E error, R any](handler ErrorHandler[E, R]) { - RegisterErrorHandlerOn[E](DefaultErrorRegistry, handler) + RegisterErrorHandlerOn(DefaultErrorRegistry, handler) } // RegisterErrorHandlerOn registers an error handler in the given registry. The R type is the type of the response body. +// Deprecated: Please use v2 of this library func RegisterErrorHandlerOn[E error, R any](registry *ErrorRegistry, handler ErrorHandler[E, R]) { // Name of the type errorType := fmt.Sprintf("%T", *new(E)) @@ -112,6 +125,7 @@ func RegisterErrorHandlerOn[E error, R any](registry *ErrorRegistry, handler Err // RegisterCustomErrorTypeHandler registers an error handler in DefaultErrorRegistry. Same as RegisterErrorHandler, // but you can set the fmt.Sprint("%T", err) error yourself. Allows you to register error types that aren't exported // from their respective packages such as the uuid error or *errors.errorString. The R type is the type of the response body. +// Deprecated: Please use v2 of this library func RegisterCustomErrorTypeHandler[R any](errorType string, handler CustomErrorHandler[R]) { RegisterCustomErrorTypeHandlerOn(DefaultErrorRegistry, errorType, handler) } @@ -119,6 +133,7 @@ func RegisterCustomErrorTypeHandler[R any](errorType string, handler CustomError // RegisterCustomErrorTypeHandlerOn registers an error handler in the given registry. Same as RegisterErrorHandlerOn, // but you can set the fmt.Sprint("%T", err) error yourself. Allows you to register error types that aren't exported // from their respective packages such as the uuid error or *errors.errorString. The R type is the type of the response body. +// Deprecated: Please use v2 of this library func RegisterCustomErrorTypeHandlerOn[R any](registry *ErrorRegistry, errorType string, handler CustomErrorHandler[R]) { // Wrap it in a closure, we can't save it directly registry.handlers[errorType] = func(err error) (int, any) { @@ -129,6 +144,7 @@ func RegisterCustomErrorTypeHandlerOn[R any](registry *ErrorRegistry, errorType // RegisterStringErrorHandler allows you to register an error handler for a simple errorString created with // errors.New() or fmt.Errorf(). Can be used in case you are dealing with libraries that don't have exported // error objects. Uses the DefaultErrorRegistry. The R type is the type of the response body. +// Deprecated: Please use v2 of this library func RegisterStringErrorHandler[R any](errorString string, handler ErrorStringHandler[R]) { RegisterStringErrorHandlerOn(DefaultErrorRegistry, errorString, handler) } @@ -136,6 +152,7 @@ func RegisterStringErrorHandler[R any](errorString string, handler ErrorStringHa // RegisterStringErrorHandlerOn allows you to register an error handler for a simple errorString created with // errors.New() or fmt.Errorf(). Can be used in case you are dealing with libraries that don't have exported // error objects. The R type is the type of the response body. +// Deprecated: Please use v2 of this library func RegisterStringErrorHandlerOn[R any](registry *ErrorRegistry, errorString string, handler ErrorStringHandler[R]) { registry.stringHandlers[errorString] = func(err string) (int, any) { return handler(err)