Return an error instead of panicking
diff --git a/parser_test.go b/parser_test.go index fd271ce..1531a1f 100644 --- a/parser_test.go +++ b/parser_test.go
@@ -25,12 +25,12 @@ } func TestSimpleKV(t *testing.T) { - tree := Load("a = 42") + tree, _ := Load("a = 42") assertTree(t, tree, map[string]interface{}{ "a": int64(42), }) - tree = Load("a = 42\nb = 21") + tree, _ = Load("a = 42\nb = 21") assertTree(t, tree, map[string]interface{}{ "a": int64(42), "b": int64(21), @@ -38,7 +38,7 @@ } func TestSimpleNumbers(t *testing.T) { - tree := Load("a = +42\nb = -21\nc = +4.2\nd = -2.1") + tree, _ := Load("a = +42\nb = -21\nc = +4.2\nd = -2.1") assertTree(t, tree, map[string]interface{}{ "a": int64(42), "b": int64(-21), @@ -48,21 +48,21 @@ } func TestSimpleDate(t *testing.T) { - tree := Load("a = 1979-05-27T07:32:00Z") + tree, _ := Load("a = 1979-05-27T07:32:00Z") assertTree(t, tree, map[string]interface{}{ "a": time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC), }) } func TestSimpleString(t *testing.T) { - tree := Load("a = \"hello world\"") + tree, _ := Load("a = \"hello world\"") assertTree(t, tree, map[string]interface{}{ "a": "hello world", }) } func TestBools(t *testing.T) { - tree := Load("a = true\nb = false") + tree, _ := Load("a = true\nb = false") assertTree(t, tree, map[string]interface{}{ "a": true, "b": false, @@ -70,33 +70,33 @@ } func TestNestedKeys(t *testing.T) { - tree := Load("[a.b.c]\nd = 42") + tree, _ := Load("[a.b.c]\nd = 42") assertTree(t, tree, map[string]interface{}{ "a.b.c.d": int64(42), }) } func TestArraySimple(t *testing.T) { - tree := Load("a = [42, 21, 10]") + tree, _ := Load("a = [42, 21, 10]") assertTree(t, tree, map[string]interface{}{ "a": []int64{int64(42), int64(21), int64(10)}, }) - tree = Load("a = [42, 21, 10,]") + tree, _ = Load("a = [42, 21, 10,]") assertTree(t, tree, map[string]interface{}{ "a": []int64{int64(42), int64(21), int64(10)}, }) } func TestArrayMultiline(t *testing.T) { - tree := Load("a = [42,\n21, 10,]") + tree, _ := Load("a = [42,\n21, 10,]") assertTree(t, tree, map[string]interface{}{ "a": []int64{int64(42), int64(21), int64(10)}, }) } func TestArrayNested(t *testing.T) { - tree := Load("a = [[42, 21], [10]]") + tree, _ := Load("a = [[42, 21], [10]]") assertTree(t, tree, map[string]interface{}{ "a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}}, })
diff --git a/toml.go b/toml.go index e8f5556..f27548a 100644 --- a/toml.go +++ b/toml.go
@@ -5,6 +5,7 @@ package toml import ( + "runtime" "strings" ) @@ -56,7 +57,7 @@ (*subtree)[keys[len(keys)-1]] = value } -// createSubTree takes a tree and a key andcreate the necessary intermediate +// createSubTree takes a tree and a key and create the necessary intermediate // subtrees to create a subtree at that point. In-place. // // e.g. passing a.b.c will create (assuming tree is empty) tree[a], tree[a][b] @@ -74,7 +75,16 @@ } // Create a TomlTree from a string. -func Load(content string) *TomlTree { +func Load(content string) (tree *TomlTree, err error) { + defer func() { + if r := recover(); r != nil { + if _, ok := r.(runtime.Error); ok { + panic(r) + } + err = r.(error) + } + }() _, flow := lex(content) - return parse(flow) + tree = parse(flow) + return }