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 (
"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

View File

@ -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)