Skip to content

Commit

Permalink
namespace & shadow output
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcase committed Dec 7, 2022
1 parent cbc5c04 commit 0f7be7a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 33 deletions.
23 changes: 0 additions & 23 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package oops

import (
"encoding/json"
"fmt"
"strings"
)

// ErrorMarshalJSON uses e's json.Marshaler if it implements one otherwise it
Expand All @@ -21,24 +19,3 @@ func ErrorMarshalJSON(e error) (bs []byte, err error) {

return bs, nil
}

// ErrorLines returns the error's string in the given format as an array of
// lines.
func ErrorLines(err error, format string) []string {
return strings.Split(fmt.Sprintf(format, err), "\n")
}

// ErrorIndent returns the error's string prefixed by indent. The first line is
// not indented.
func ErrorIndent(err error, format, indent string) []string {
lines := ErrorLines(err, format)
for i, line := range lines {
if i == 0 {
continue
}

lines[i] = indent + line
}

return lines
}
13 changes: 9 additions & 4 deletions namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"strings"

"github.com/calebcase/oops/lines"
)

// Namespace provides a name prefix for new errors.
Expand Down Expand Up @@ -74,13 +76,16 @@ func (ne *NamespaceError) Format(f fmt.State, verb rune) {
return
}

lines := ErrorLines(ne.Err, "%"+flag+string(verb))
output := []string{}
ls := lines.Sprintf("%"+flag+string(verb), ne.Err)

fmt.Fprintf(f, "%s: %s\n", ne.Name, lines[0])
output = append(output, fmt.Sprintf("%s: %s", ne.Name, ls[0]))

if len(lines) > 1 {
f.Write([]byte(strings.Join(lines[1:], "\n")))
if len(ls) > 1 {
output = append(output, ls[1:]...)
}

f.Write([]byte(strings.Join(output, "\n")))
}

// MarshalJSON implements json.Marshaler.
Expand Down
1 change: 1 addition & 0 deletions namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestNamespace(t *testing.T) {
err := namespaceFunc()
require.Error(t, err)

t.Logf("%v\n", err)
t.Logf("%+v\n", err)

ne := err.(*oops.NamespaceError)
Expand Down
10 changes: 4 additions & 6 deletions shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"strings"

"github.com/calebcase/oops/lines"
)

// ShadowError is an error with a hidden error inside.
Expand Down Expand Up @@ -48,9 +50,8 @@ func (se *ShadowError) Format(f fmt.State, verb rune) {
return
}

output := ErrorIndent(se.Err, "%"+flag+string(verb), "··")

hidden := ErrorIndent(se.Hidden, "%"+flag+string(verb), "··")
output := lines.Indent(lines.Sprintf("%"+flag+string(verb), se.Err), "··", 1)
hidden := lines.Indent(lines.Sprintf("%"+flag+string(verb), se.Hidden), "··", 1)

output = append(output, "··hidden: "+hidden[0])

Expand All @@ -59,9 +60,6 @@ func (se *ShadowError) Format(f fmt.State, verb rune) {
}

fmt.Fprintf(f, strings.Join(output, "\n"))

//fmt.Fprintf(f, "%"+flag+string(verb)+":\n", se.Err)
//fmt.Fprintf(f, " %"+flag+string(verb)+"\n", se.Hidden)
}

// MarshalJSON implements json.Marshaler.
Expand Down
1 change: 1 addition & 0 deletions shadow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestShadow(t *testing.T) {
require.Error(t, err)

t.Logf("err: %v\n", err)
t.Logf("err: %+v\n", err)

se := err.(*oops.ShadowError)
require.Equal(t, ErrShadow, se.Err)
Expand Down

0 comments on commit 0f7be7a

Please sign in to comment.