Skip to content

Commit

Permalink
Merge pull request #5 from ing-bank/feature/deprecate-v1
Browse files Browse the repository at this point in the history
Deprecate v1 of the library
  • Loading branch information
survivorbat committed Jun 5, 2024
2 parents b8aa4a2 + aa4bd4f commit 032111d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

const defaultCode = http.StatusInternalServerError

// Deprecated: Please use v2 of this library
var DefaultErrorRegistry = NewErrorRegistry()

type (
Expand All @@ -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),
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -112,13 +125,15 @@ 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)
}

// 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) {
Expand All @@ -129,13 +144,15 @@ 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)
}

// 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)
Expand Down

0 comments on commit 032111d

Please sign in to comment.