Skip to content

Commit

Permalink
Fix usage of deprecated function
Browse files Browse the repository at this point in the history
Replace usage of `wrap.Error`.
  • Loading branch information
HeavyWombat committed Oct 25, 2023
1 parent 9105e62 commit 0cd7591
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
21 changes: 10 additions & 11 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@ func SprintError(err error) string {
)

default:
if parts := strings.SplitN(err.Error(), ":", 2); len(parts) == 2 {
message, cause := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
return ContentBox(
fmt.Sprintf("Error: %s", message),
cause,
HeadlineColor(bunt.OrangeRed),
ContentColor(bunt.Red),
)
}
return unpack(err.Error())
}
}

func unpack(content string) string {
if parts := strings.SplitN(content, ":", 2); len(parts) == 2 {
message, cause := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
return ContentBox(
"Error",
err.Error(),
fmt.Sprintf("Error: %s", message),
unpack(cause),
HeadlineColor(bunt.OrangeRed),
ContentColor(bunt.Red),
)
}

return content
}
14 changes: 6 additions & 8 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ var _ = Describe("error rendering", func() {
It("should render to a writer", func() {
buf := bytes.Buffer{}
out := bufio.NewWriter(&buf)
FprintError(out, fmt.Errorf("failed to do X"))
FprintError(out, fmt.Errorf("foo: %w", fmt.Errorf("failed to do X")))
out.Flush()

Expect(buf.String()).To(
BeEquivalentTo(ContentBox(
"Error",
"Error: foo",
"failed to do X",
HeadlineColor(OrangeRed),
ContentColor(Red),
Expand Down Expand Up @@ -97,11 +97,9 @@ var _ = Describe("error rendering", func() {
return buf.String()
}

Expect(captureStdout(func() {
PrintError(fmt.Errorf("failed to do X"))
})).To(
Expect(captureStdout(func() { PrintError(fmt.Errorf("foo: %w", fmt.Errorf("failed to do X"))) })).To(
BeEquivalentTo(ContentBox(
"Error",
"Error: foo",
"failed to do X",
HeadlineColor(OrangeRed),
ContentColor(Red),
Expand All @@ -110,10 +108,10 @@ var _ = Describe("error rendering", func() {

It("should render a context error inside a context error", func() {
root := fmt.Errorf("unable to load X")
cause := wrap.Errorf(root, "failed to start Y")
cause := fmt.Errorf("failed to start Y: %w", root)
context := "cannot process Z"

err := wrap.Error(cause, context)
err := fmt.Errorf("cannot process Z: %w", cause)
Expect(SprintError(err)).To(
BeEquivalentTo(ContentBox(
"Error: "+context,
Expand Down

0 comments on commit 0cd7591

Please sign in to comment.