Skip to content

Commit

Permalink
implement the Unwrap method for custom error types
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Aug 11, 2024
1 parent 5dc300a commit 4d8403a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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

0 comments on commit 4d8403a

Please sign in to comment.