feat(forge): add APIError stringers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
36f9619fc4
commit
b8bc948fc0
3 changed files with 40 additions and 0 deletions
17
client.go
17
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{<nil>}"
|
||||
}
|
||||
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:
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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{<nil>}"
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue