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

refactor to NewError to not log fatal or panic, add tests #394

Merged
merged 3 commits into from
Oct 6, 2023
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
14 changes: 7 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package linodego

import (
"fmt"
"log"
"net/http"
"reflect"
"strings"

"github.com/go-resty/resty/v2"
)

const (
ErrorUnsupported = iota
// ErrorFromString is the Code identifying Errors created by string types
ErrorFromString = 1
ErrorFromString
// ErrorFromError is the Code identifying Errors created by error types
ErrorFromError = 2
ErrorFromError
// ErrorFromStringer is the Code identifying Errors created by fmt.Stringer types
ErrorFromStringer = 3
ErrorFromStringer
)

// Error wraps the LinodeGo error with the relevant http.Response
Expand Down Expand Up @@ -113,7 +114,7 @@ func NewError(err any) *Error {
apiError, ok := e.Error().(*APIError)

if !ok {
log.Fatalln("Unexpected Resty Error Response")
return &Error{Code: ErrorUnsupported, Message: "Unexpected Resty Error Response, no error"}
}

return &Error{
Expand All @@ -128,7 +129,6 @@ func NewError(err any) *Error {
case fmt.Stringer:
return &Error{Code: ErrorFromStringer, Message: e.String()}
default:
log.Fatalln("Unsupported type to linodego.NewError")
panic(err)
return &Error{Code: ErrorUnsupported, Message: fmt.Sprintf("Unsupported type to linodego.NewError: %s", reflect.TypeOf(e))}
}
}
52 changes: 52 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,58 @@ import (
"github.com/google/go-cmp/cmp"
)

type tstringer string

func (t tstringer) String() string {
return string(t)
}

func TestNewError(t *testing.T) {
if NewError(nil) != nil {
t.Errorf("nil error should return nil")
}
if NewError(struct{}{}).Code != ErrorUnsupported {
t.Error("empty struct should return unsupported error type")
}
err := errors.New("test")
newErr := NewError(&err)
if newErr.Message == err.Error() && newErr.Code == ErrorFromError {
t.Error("nil error should return nil")
}

if err := NewError(&resty.Response{Request: &resty.Request{}}); err.Message != "Unexpected Resty Error Response, no error" {
t.Error("Unexpected Resty Error Response, no error")
}

rerr := &resty.Response{
RawResponse: &http.Response{
StatusCode: 500,
},
Request: &resty.Request{
Error: &APIError{
[]APIErrorReason{
{
Reason: "testreason",
Field: "testfield",
},
},
},
},
}

if err := NewError(rerr); err.Message != "[testfield] testreason" {
t.Error("rest response error should should be set")
}

if err := NewError("stringerror"); err.Message != "stringerror" || err.Code != ErrorFromString {
t.Errorf("string error should be set")
}

if err := NewError(tstringer("teststringer")); err.Message != "teststringer" || err.Code != ErrorFromStringer {
t.Errorf("stringer error should be set")
}
}

func createTestServer(method, route, contentType, body string, statusCode int) (*httptest.Server, *Client) {
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method == method && r.URL.Path == route {
Expand Down