From 0f7be7a9c2608cd9e35d1357c6a64bf7fa297edd Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Wed, 7 Dec 2022 10:22:52 -0500 Subject: [PATCH] namespace & shadow output --- error.go | 23 ----------------------- namespace.go | 13 +++++++++---- namespace_test.go | 1 + shadow.go | 10 ++++------ shadow_test.go | 1 + 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/error.go b/error.go index ba8a430..ceab8cf 100644 --- a/error.go +++ b/error.go @@ -2,8 +2,6 @@ package oops import ( "encoding/json" - "fmt" - "strings" ) // ErrorMarshalJSON uses e's json.Marshaler if it implements one otherwise it @@ -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 -} diff --git a/namespace.go b/namespace.go index 4c114d1..a9dadc1 100644 --- a/namespace.go +++ b/namespace.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "strings" + + "github.com/calebcase/oops/lines" ) // Namespace provides a name prefix for new errors. @@ -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. diff --git a/namespace_test.go b/namespace_test.go index 52cb517..91cc837 100644 --- a/namespace_test.go +++ b/namespace_test.go @@ -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) diff --git a/shadow.go b/shadow.go index 596685e..ae44ead 100644 --- a/shadow.go +++ b/shadow.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "strings" + + "github.com/calebcase/oops/lines" ) // ShadowError is an error with a hidden error inside. @@ -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]) @@ -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. diff --git a/shadow_test.go b/shadow_test.go index 4fe0883..acd5d19 100644 --- a/shadow_test.go +++ b/shadow_test.go @@ -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)