Skip to content

Commit

Permalink
feat: improve default formatters and add custom format support (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
sum2000 authored Jan 12, 2020
1 parent c742c91 commit 5a86ea6
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 254 deletions.
4 changes: 1 addition & 3 deletions eris.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ func printError(err error, s fmt.State, verb rune) {
withTrace = true
}
}
format := NewDefaultFormat(withTrace)
uErr := Unpack(err)
str := uErr.ToString(format)
str := ToString(err, withTrace)
_, _ = io.WriteString(s, str)
}
97 changes: 42 additions & 55 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@ import (

// Demonstrates JSON formatting of wrapped errors that originate from
// external (non-eris) error types.
func ExampleUnpackedError_ToJSON_external() {
func ExampleToJSON_external() {
// example func that returns an IO error
readFile := func(fname string) error {
return io.ErrUnexpectedEOF
}

// unpack and print the error
err := readFile("example.json")
uerr := eris.Unpack(err)
format := eris.NewDefaultFormat(false) // false: omit stack trace
u, _ := json.Marshal(uerr.ToJSON(format))
u, _ := json.Marshal(eris.ToJSON(err, false)) // false: omit stack trace
fmt.Println(string(u))
// Output:
// {"external":"unexpected EOF"}
}

// Demonstrates JSON formatting of wrapped errors that originate from
// global root errors (created via eris.NewGlobal).
func ExampleUnpackedError_ToJSON_global() {
func ExampleToJSON_global() {
// declare a "global" error type
ErrUnexpectedEOF := eris.NewGlobal("unexpected EOF")

Expand All @@ -49,43 +47,41 @@ func ExampleUnpackedError_ToJSON_global() {
}

// unpack and print the error via uerr.ToJSON(...)
err := parseFile("example.json") // line 20
uerr := eris.Unpack(err)
format := eris.NewDefaultFormat(true) // true: include stack trace
u, _ := json.MarshalIndent(uerr.ToJSON(format), "", "\t")
err := parseFile("example.json") // line 20
u, _ := json.MarshalIndent(eris.ToJSON(err, true), "", "\t") // true: include stack trace
fmt.Printf("%v\n", string(u))

// example output:
// {
// "root": {
// "message": "unexpected EOF",
// "stack": [
// "main.readFile: .../example/main.go: 6",
// "main.parseFile: .../example/main.go: 12",
// "main.main: .../example/main.go: 20",
// "main.readFile:.../example/main.go:6",
// "main.parseFile:.../example/main.go:12",
// "main.main:.../example/main.go:20",
// ]
// },
// "wrap": [
// {
// "message": "error reading file 'example.json'",
// "stack": "main.readFile: .../example/main.go: 6"
// "stack": "main.readFile:.../example/main.go:6"
// }
// ]
// }
}

// Hack to run examples that don't have a predictable output (i.e. all
// examples that involve printing stack traces).
func TestExampleUnpackedError_ToJSON_global(t *testing.T) {
func TestExampleToJSON_global(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleUnpackedError_ToJSON_global()
ExampleToJSON_global()
}

// Demonstrates JSON formatting of wrapped errors that originate from
// local root errors (created at the source of the error via eris.New).
func ExampleUnpackedError_ToJSON_local() {
func ExampleToJSON_local() {
// example func that returns an eris error
readFile := func(fname string) error {
return eris.New("unexpected EOF") // line 3
Expand Down Expand Up @@ -123,23 +119,21 @@ func ExampleUnpackedError_ToJSON_local() {

// unpack and print the raw error
err := printFile("example.json") // line 37
uerr := eris.Unpack(err)
format := eris.NewDefaultFormat(true) // true: include stack trace
u, _ := json.MarshalIndent(uerr.ToJSON(format), "", "\t")
u, _ := json.MarshalIndent(eris.ToJSON(err, true), "", "\t")
fmt.Printf("%v\n", string(u))

// example output:
// {
// "root": {
// "message": "unexpected EOF",
// "stack": [
// "main.readFile: .../example/main.go: 3",
// "main.parseFile: .../example/main.go: 9",
// "main.parseFile: .../example/main.go: 11",
// "main.processFile: .../example/main.go: 19",
// "main.printFile: .../example/main.go: 29",
// "main.printFile: .../example/main.go: 31",
// "main.main: .../example/main.go: 37",
// "main.readFile:.../example/main.go:3",
// "main.parseFile:.../example/main.go:9",
// "main.parseFile:.../example/main.go:11",
// "main.processFile:.../example/main.go:19",
// "main.printFile:.../example/main.go:29",
// "main.printFile:.../example/main.go:31",
// "main.main:.../example/main.go:37",
// ]
// },
// "wrap": [
Expand All @@ -149,39 +143,36 @@ func ExampleUnpackedError_ToJSON_local() {
// },
// {
// "message": "error printing file 'example.json'",
// "stack": "main.printFile: .../example/main.go: 31"
// "stack": "main.printFile:.../example/main.go:31"
// }
// ]
// }
}

func TestExampleUnpackedError_ToJSON_local(t *testing.T) {
func TestExampleToJSON_local(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleUnpackedError_ToJSON_local()
ExampleToJSON_local()
}

// Demonstrates string formatting of wrapped errors that originate from
// external (non-eris) error types.
func ExampleUnpackedError_ToString_external() {
func ExampleToString_external() {
// example func that returns an IO error
readFile := func(fname string) error {
return io.ErrUnexpectedEOF
}

// unpack and print the error
err := readFile("example.json")
uerr := eris.Unpack(err)
format := eris.NewDefaultFormat(false) // false: omit stack trace
fmt.Println(uerr.ToString(format))
// Output:
fmt.Println(eris.ToString(err, false)) // false: omit stack trace
// unexpected EOF
}

// Demonstrates string formatting of wrapped errors that originate from
// global root errors (created via eris.NewGlobal).
func ExampleUnpackedError_ToString_global() {
func ExampleToString_global() {
// declare a "global" error type
ErrUnexpectedEOF := eris.NewGlobal("unexpected EOF")

Expand Down Expand Up @@ -210,29 +201,27 @@ func ExampleUnpackedError_ToString_global() {
// unexpected EOF: error reading file 'example.json'

// unpack and print the error via uerr.ToString(...)
uerr := eris.Unpack(err)
format := eris.NewDefaultFormat(true) // true: include stack trace
fmt.Printf("%v\n", uerr.ToString(format))
fmt.Printf("%v\n", eris.ToString(err, true)) // true: include stack trace

// example output:
// unexpected EOF
// main.readFile: .../example/main.go: 6
// main.parseFile: .../example/main.go: 12
// main.main: .../example/main.go: 20
// main.readFile:.../example/main.go:6
// main.parseFile:.../example/main.go:12
// main.main:.../example/main.go:20
// error reading file 'example.json'
// main.readFile: .../example/main.go: 6
// main.readFile:.../example/main.go:6
}

func TestExampleUnpackedError_ToString_global(t *testing.T) {
func TestExampleToString_global(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleUnpackedError_ToString_global()
ExampleToString_global()
}

// Demonstrates string formatting of wrapped errors that originate from
// local root errors (created at the source of the error via eris.New).
func ExampleUnpackedError_ToString_local() {
func ExampleToString_local() {
// example func that returns an eris error
readFile := func(fname string) error {
return eris.New("unexpected EOF") // line 3
Expand All @@ -258,23 +247,21 @@ func ExampleUnpackedError_ToString_local() {
// unexpected EOF: error reading file 'example.json'

// unpack and print the error via uerr.ToString(...)
uerr := eris.Unpack(err)
format := eris.NewDefaultFormat(true) // true: include stack trace
fmt.Println(uerr.ToString(format))
fmt.Println(eris.ToString(err, true)) // true: include stack trace

// example output:
// unexpected EOF
// main.readFile: .../example/main.go: 3
// main.parseFile: .../example/main.go: 9
// main.parseFile: .../example/main.go: 11
// main.main: .../example/main.go: 17
// main.readFile:.../example/main.go:3
// main.parseFile:.../example/main.go:9
// main.parseFile:.../example/main.go:11
// main.main:.../example/main.go:17
// error reading file 'example.json'
// main.parseFile: .../example/main.go: 11
// main.parseFile:.../example/main.go:11
}

func TestExampleUnpackedError_ToString_local(t *testing.T) {
func TestExampleToString_local(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleUnpackedError_ToString_local()
ExampleToString_local()
}
Loading

0 comments on commit 5a86ea6

Please sign in to comment.