From 4d8403ac29031d067a4c9e744be0fa569fb3b3df Mon Sep 17 00:00:00 2001 From: favonia Date: Sun, 11 Aug 2024 08:01:16 -0500 Subject: [PATCH] implement the Unwrap method for custom error types --- .changelog/2857.txt | 3 +++ errors.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 .changelog/2857.txt diff --git a/.changelog/2857.txt b/.changelog/2857.txt new file mode 100644 index 00000000000..f27aee94771 --- /dev/null +++ b/.changelog/2857.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +errors: implement the Unwrap method for custom error types to access the wrapped errors via errors.Is and errors.As +``` diff --git a/errors.go b/errors.go index 85fdc3efacb..93faab0dbd6 100644 --- a/errors.go +++ b/errors.go @@ -152,6 +152,10 @@ func (e RequestError) Type() ErrorType { return e.cloudflareError.Type } +func (e RequestError) Unwrap() error { + return e.cloudflareError +} + func NewRequestError(e *Error) RequestError { return RequestError{ cloudflareError: e, @@ -192,6 +196,10 @@ func (e RatelimitError) Type() ErrorType { return e.cloudflareError.Type } +func (e RatelimitError) Unwrap() error { + return e.cloudflareError +} + func NewRatelimitError(e *Error) RatelimitError { return RatelimitError{ cloudflareError: e, @@ -231,6 +239,10 @@ func (e ServiceError) Type() ErrorType { return e.cloudflareError.Type } +func (e ServiceError) Unwrap() error { + return e.cloudflareError +} + func NewServiceError(e *Error) ServiceError { return ServiceError{ cloudflareError: e, @@ -270,6 +282,10 @@ func (e AuthenticationError) Type() ErrorType { return e.cloudflareError.Type } +func (e AuthenticationError) Unwrap() error { + return e.cloudflareError +} + func NewAuthenticationError(e *Error) AuthenticationError { return AuthenticationError{ cloudflareError: e, @@ -309,6 +325,10 @@ func (e AuthorizationError) Type() ErrorType { return e.cloudflareError.Type } +func (e AuthorizationError) Unwrap() error { + return e.cloudflareError +} + func NewAuthorizationError(e *Error) AuthorizationError { return AuthorizationError{ cloudflareError: e, @@ -348,6 +368,10 @@ func (e NotFoundError) Type() ErrorType { return e.cloudflareError.Type } +func (e NotFoundError) Unwrap() error { + return e.cloudflareError +} + func NewNotFoundError(e *Error) NotFoundError { return NotFoundError{ cloudflareError: e,