Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement the Unwrap method for custom error types #2857

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2857.txt
Original file line number Diff line number Diff line change
@@ -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
```
24 changes: 24 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading