diff --git a/internal/handlers/common_test.go b/internal/handlers/common_test.go index 15293ad..e5dc3d2 100644 --- a/internal/handlers/common_test.go +++ b/internal/handlers/common_test.go @@ -3,7 +3,6 @@ package handlers_test import ( "database/sql" "encoding/json" - "fmt" "github.com/aclindsa/moneygo/internal/config" "github.com/aclindsa/moneygo/internal/db" "github.com/aclindsa/moneygo/internal/handlers" @@ -63,7 +62,7 @@ func create(client *http.Client, input, output TransactType, urlsuffix, key stri return err } if e.ErrorId != 0 || len(e.ErrorString) != 0 { - return fmt.Errorf("Error when creating %s: %+v", urlsuffix, e) + return &e } err = output.Read(string(body)) @@ -92,7 +91,7 @@ func read(client *http.Client, output TransactType, urlsuffix, key string) error return err } if e.ErrorId != 0 || len(e.ErrorString) != 0 { - return fmt.Errorf("Error when updating %s: %+v", urlsuffix, e) + return &e } err = output.Read(string(body)) @@ -125,7 +124,7 @@ func update(client *http.Client, input, output TransactType, urlsuffix, key stri return err } if e.ErrorId != 0 || len(e.ErrorString) != 0 { - return fmt.Errorf("Error when updating %s: %+v", urlsuffix, e) + return &e } err = output.Read(string(body)) @@ -154,7 +153,7 @@ func remove(client *http.Client, urlsuffix, key string) error { return err } if e.ErrorId != 0 || len(e.ErrorString) != 0 { - return fmt.Errorf("Error when removing %s: %+v", urlsuffix, e) + return &e } return nil diff --git a/internal/handlers/errors.go b/internal/handlers/errors.go index fd05742..1c0928f 100644 --- a/internal/handlers/errors.go +++ b/internal/handlers/errors.go @@ -2,6 +2,7 @@ package handlers import ( "encoding/json" + "fmt" "log" "net/http" "strings" @@ -12,6 +13,10 @@ type Error struct { ErrorString string } +func (e *Error) Error() string { + return fmt.Sprintf("Error %d: %s", e.ErrorId, e.ErrorString) +} + func (e *Error) Read(json_str string) error { dec := json.NewDecoder(strings.NewReader(json_str)) return dec.Decode(e)