Skip to content

Commit 74d9713

Browse files
akerl-unprivakerl
authored andcommitted
return specific error for spec version mismatch (#14)
1 parent e160ad9 commit 74d9713

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

cartogram/cartogram.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cartogram
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
76
"time"
87
)
@@ -14,6 +13,12 @@ type Cartogram struct {
1413
AccountSet AccountSet `json:"accounts"`
1514
}
1615

16+
// dummyCartogram just parses the Version
17+
// this is used to test for schema version mismatch
18+
type dummyCartogram struct {
19+
Version int `json:"version"`
20+
}
21+
1722
// NewCartogram creates a new cartogram from an account set
1823
func NewCartogram(as AccountSet) Cartogram {
1924
return Cartogram{
@@ -42,11 +47,19 @@ func (c *Cartogram) loadFromFile(filePath string) error {
4247
}
4348

4449
func (c *Cartogram) loadFromString(data []byte) error {
50+
if err := schemaVersionCheck(data); err != nil {
51+
return err
52+
}
53+
return json.Unmarshal(data, &c)
54+
}
55+
56+
func schemaVersionCheck(data []byte) error {
57+
var c Cartogram
4558
if err := json.Unmarshal(data, &c); err != nil {
4659
return err
4760
}
4861
if c.Version != specVersion {
49-
return fmt.Errorf("spec version mismatch: expected %d, got %d", specVersion, c.Version)
62+
return SpecVersionError{ActualVersion: c.Version, ExpectedVersion: specVersion}
5063
}
5164
return nil
5265
}

cartogram/errors.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cartogram
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// SpecVersionError indicates the cartogram version doesn't match voyager version
8+
type SpecVersionError struct {
9+
ActualVersion, ExpectedVersion int
10+
}
11+
12+
func (s SpecVersionError) Error() string {
13+
return fmt.Sprintf(
14+
"spec version mismatch: expected %d, got %d",
15+
s.ExpectedVersion,
16+
s.ActualVersion,
17+
)
18+
}

0 commit comments

Comments
 (0)