1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 07:33:21 -05:00

testing: Make handlers.Error obey the standard 'error' interface

And return these types from test helper functions
This commit is contained in:
Aaron Lindsay 2017-10-09 21:10:59 -04:00
parent 32ac18647b
commit 4953063286
2 changed files with 9 additions and 5 deletions

View File

@ -3,7 +3,6 @@ package handlers_test
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"github.com/aclindsa/moneygo/internal/config" "github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/db" "github.com/aclindsa/moneygo/internal/db"
"github.com/aclindsa/moneygo/internal/handlers" "github.com/aclindsa/moneygo/internal/handlers"
@ -63,7 +62,7 @@ func create(client *http.Client, input, output TransactType, urlsuffix, key stri
return err return err
} }
if e.ErrorId != 0 || len(e.ErrorString) != 0 { 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)) err = output.Read(string(body))
@ -92,7 +91,7 @@ func read(client *http.Client, output TransactType, urlsuffix, key string) error
return err return err
} }
if e.ErrorId != 0 || len(e.ErrorString) != 0 { 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)) err = output.Read(string(body))
@ -125,7 +124,7 @@ func update(client *http.Client, input, output TransactType, urlsuffix, key stri
return err return err
} }
if e.ErrorId != 0 || len(e.ErrorString) != 0 { 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)) err = output.Read(string(body))
@ -154,7 +153,7 @@ func remove(client *http.Client, urlsuffix, key string) error {
return err return err
} }
if e.ErrorId != 0 || len(e.ErrorString) != 0 { if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return fmt.Errorf("Error when removing %s: %+v", urlsuffix, e) return &e
} }
return nil return nil

View File

@ -2,6 +2,7 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -12,6 +13,10 @@ type Error struct {
ErrorString string 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 { func (e *Error) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str)) dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(e) return dec.Decode(e)