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

Add some error type convenience functions for mocking and inspection #1047

Merged
merged 3 commits into from
Aug 18, 2022
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/1047.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
errors: add some error type convenience functions for mocking and inspection
```
60 changes: 60 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (e RequestError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}

func (e RequestError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e RequestError) RayID() string {
return e.cloudflareError.RayID
}
Expand All @@ -121,6 +125,12 @@ func (e RequestError) Type() ErrorType {
return e.cloudflareError.Type
}

func NewRequestError(e *Error) RequestError {
return RequestError{
cloudflareError: e,
}
}

// RatelimitError is for HTTP 429s where the service is telling the client to
// slow down.
type RatelimitError struct {
Expand All @@ -143,6 +153,10 @@ func (e RatelimitError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}

func (e RatelimitError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e RatelimitError) RayID() string {
return e.cloudflareError.RayID
}
Expand All @@ -151,6 +165,12 @@ func (e RatelimitError) Type() ErrorType {
return e.cloudflareError.Type
}

func NewRatelimitError(e *Error) RatelimitError {
return RatelimitError{
cloudflareError: e,
}
}

// ServiceError is a handler for 5xx errors returned to the client.
type ServiceError struct {
cloudflareError *Error
Expand All @@ -172,6 +192,10 @@ func (e ServiceError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}

func (e ServiceError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e ServiceError) RayID() string {
return e.cloudflareError.RayID
}
Expand All @@ -180,6 +204,12 @@ func (e ServiceError) Type() ErrorType {
return e.cloudflareError.Type
}

func NewServiceError(e *Error) ServiceError {
return ServiceError{
cloudflareError: e,
}
}

// AuthenticationError is for HTTP 401 responses.
type AuthenticationError struct {
cloudflareError *Error
Expand All @@ -201,6 +231,10 @@ func (e AuthenticationError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}

func (e AuthenticationError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e AuthenticationError) RayID() string {
return e.cloudflareError.RayID
}
Expand All @@ -209,6 +243,12 @@ func (e AuthenticationError) Type() ErrorType {
return e.cloudflareError.Type
}

func NewAuthenticationError(e *Error) AuthenticationError {
return AuthenticationError{
cloudflareError: e,
}
}

// AuthorizationError is for HTTP 403 responses.
type AuthorizationError struct {
cloudflareError *Error
Expand All @@ -230,6 +270,10 @@ func (e AuthorizationError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}

func (e AuthorizationError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e AuthorizationError) RayID() string {
return e.cloudflareError.RayID
}
Expand All @@ -238,6 +282,12 @@ func (e AuthorizationError) Type() ErrorType {
return e.cloudflareError.Type
}

func NewAuthorizationError(e *Error) AuthorizationError {
return AuthorizationError{
cloudflareError: e,
}
}

// NotFoundError is for HTTP 404 responses.
type NotFoundError struct {
cloudflareError *Error
Expand All @@ -259,6 +309,10 @@ func (e NotFoundError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}

func (e NotFoundError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}

func (e NotFoundError) RayID() string {
return e.cloudflareError.RayID
}
Expand All @@ -267,6 +321,12 @@ func (e NotFoundError) Type() ErrorType {
return e.cloudflareError.Type
}

func NewNotFoundError(e *Error) NotFoundError {
return NotFoundError{
cloudflareError: e,
}
}

// ClientError returns a boolean whether or not the raised error was caused by
// something client side.
func (e *Error) ClientError() bool {
Expand Down
28 changes: 28 additions & 0 deletions errors_external_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cloudflare_test

import (
"testing"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/stretchr/testify/assert"
)

func TestError_CreateErrors(t *testing.T) {
baseErr := &cloudflare.Error{
StatusCode: 400,
ErrorCodes: []int{10000},
}

requestErr := cloudflare.NewRequestError(baseErr)
assert.True(t, requestErr.InternalErrorCodeIs(10000))
limitError := cloudflare.NewRatelimitError(baseErr)
assert.True(t, limitError.InternalErrorCodeIs(10000))
svcErr := cloudflare.NewServiceError(baseErr)
assert.True(t, svcErr.InternalErrorCodeIs(10000))
authErr := cloudflare.NewAuthenticationError(baseErr)
assert.True(t, authErr.InternalErrorCodeIs(10000))
authzErr := cloudflare.NewAuthorizationError(baseErr)
assert.True(t, authzErr.InternalErrorCodeIs(10000))
notFoundErr := cloudflare.NewNotFoundError(baseErr)
assert.True(t, notFoundErr.InternalErrorCodeIs(10000))
}