Skip to content

Commit

Permalink
refactor to NewError to not log fatal or panic, add tests (#394)
Browse files Browse the repository at this point in the history
Co-authored-by: Luther Monson <[email protected]>
  • Loading branch information
luthermonson and luthermonson authored Oct 6, 2023
1 parent cb9a279 commit f71d70d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
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

0 comments on commit f71d70d

Please sign in to comment.