diff --git a/client.go b/client.go index fff8a8c..a0bf99e 100644 --- a/client.go +++ b/client.go @@ -33,9 +33,26 @@ type APIError struct { // // err := (&forge.APIError{StatusCode: 404, Message: "not found", URL: "/api/v1/repos/x/y"}).Error() func (e *APIError) Error() string { + if e == nil { + return "forge.APIError{}" + } return core.Concat("forge: ", e.URL, " ", strconv.Itoa(e.StatusCode), ": ", e.Message) } +// String returns a safe summary of the API error. +// +// Usage: +// +// s := err.String() +func (e *APIError) String() string { return e.Error() } + +// GoString returns a safe Go-syntax summary of the API error. +// +// Usage: +// +// s := fmt.Sprintf("%#v", err) +func (e *APIError) GoString() string { return e.Error() } + // IsNotFound returns true if the error is a 404 response. // // Usage: diff --git a/client_test.go b/client_test.go index 6267101..c8e8555 100644 --- a/client_test.go +++ b/client_test.go @@ -248,6 +248,15 @@ func TestAPIError_Error_Good(t *testing.T) { if got != want { t.Errorf("got %q, want %q", got, want) } + if got := e.String(); got != want { + t.Errorf("got String()=%q, want %q", got, want) + } + if got := fmt.Sprint(e); got != want { + t.Errorf("got fmt.Sprint=%q, want %q", got, want) + } + if got := fmt.Sprintf("%#v", e); got != want { + t.Errorf("got GoString=%q, want %q", got, want) + } } func TestIsConflict_Match_Good(t *testing.T) { diff --git a/stringer_nil_test.go b/stringer_nil_test.go index 8295a68..f5d2ea1 100644 --- a/stringer_nil_test.go +++ b/stringer_nil_test.go @@ -46,3 +46,17 @@ func TestResource_String_NilSafe(t *testing.T) { t.Fatalf("got GoString=%q, want %q", got, want) } } + +func TestAPIError_String_NilSafe(t *testing.T) { + var e *APIError + want := "forge.APIError{}" + if got := e.String(); got != want { + t.Fatalf("got String()=%q, want %q", got, want) + } + if got := fmt.Sprint(e); got != want { + t.Fatalf("got fmt.Sprint=%q, want %q", got, want) + } + if got := fmt.Sprintf("%#v", e); got != want { + t.Fatalf("got GoString=%q, want %q", got, want) + } +}