Skip to content

Commit 286ad9f

Browse files
committed
more error improvement
1 parent bef7c4f commit 286ad9f

12 files changed

+94
-15
lines changed

cmd_error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (ce CmdError) Error() string {
4747
}
4848

4949
if ce.Err != nil {
50-
lines = append(lines, fmt.Sprintf("error: %s", ce.Err))
50+
lines = append(lines, fmt.Sprintf("cmd error: %s", ce.Err))
5151
}
5252

5353
sb.WriteString(strings.Join(lines, "\n"))

cmd_error_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func Test_CmdError_Error(t *testing.T) {
7878
act := ce.Error()
7979
act = strings.TrimSpace(act)
8080

81-
exp := "filepath: /tmp/hype.md\ncmd: $ echo hello\nexit: 1\nerror: EOF"
81+
exp := "filepath: /tmp/hype.md\ncmd: $ echo hello\nexit: 1\ncmd error: EOF"
8282
exp = strings.TrimSpace(exp)
8383

8484
r.Equal(exp, act)

execute_error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (pe ExecuteError) Error() string {
4444
}
4545

4646
if pe.Err != nil {
47-
lines = append(lines, fmt.Sprintf("error: %s", pe.Err))
47+
lines = append(lines, fmt.Sprintf("execute error: %s", pe.Err))
4848
}
4949

5050
sb.WriteString(strings.Join(lines, "\n"))

execute_error_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ func Test_ExecuteError_String(t *testing.T) {
146146
{
147147
name: "detailed",
148148
in: detailed,
149-
exp: "filepath: testdata/parser/errors/execute/hype.md\ndocument: My Title\nerror: EOF",
149+
exp: "filepath: testdata/parser/errors/execute/hype.md\ndocument: My Title\nexecute error: EOF",
150150
},
151151
{
152152
name: "basic",
153153
in: ExecuteError{Err: io.EOF},
154-
exp: "error: EOF",
154+
exp: "execute error: EOF",
155155
},
156156
}
157157

figure.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (f *Figure) MD() string {
4747
bb := &strings.Builder{}
4848
bb.WriteString(f.StartTag())
4949
fmt.Fprintln(bb)
50-
bb.WriteString(f.Nodes.MD())
50+
bb.WriteString(f.Nodes.String())
5151
bb.WriteString(f.EndTag())
5252

5353
return bb.String()

parse_error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (pe ParseError) Error() string {
3838
}
3939

4040
if pe.Err != nil {
41-
lines = append(lines, fmt.Sprintf("error: %s", pe.Err))
41+
lines = append(lines, fmt.Sprintf("parse error: %s", pe.Err))
4242
}
4343

4444
sb.WriteString(strings.Join(lines, "\n"))

parse_error_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Test_ParseError_Error(t *testing.T) {
6464

6565
fmt.Println(act)
6666

67-
exp := "filepath: root/test.md\ncontents: contents\nerror: EOF"
67+
exp := "filepath: root/test.md\nparse error: EOF"
6868

6969
r.Equal(exp, act)
7070
}

post_execute_error.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package hype
33
import (
44
"encoding/json"
55
"errors"
6+
"fmt"
7+
"path/filepath"
8+
"strings"
69
)
710

811
type PostExecuteError struct {
@@ -29,7 +32,32 @@ func (pee PostExecuteError) MarshalJSON() ([]byte, error) {
2932
}
3033

3134
func (e PostExecuteError) Error() string {
32-
return toError(e)
35+
sb := &strings.Builder{}
36+
37+
var lines []string
38+
39+
fp := filepath.Join(e.Root, e.Filename)
40+
if len(fp) > 0 {
41+
lines = append(lines, fmt.Sprintf("filepath: %s", fp))
42+
}
43+
44+
if e.Document != nil && len(e.Document.Title) > 0 {
45+
lines = append(lines, fmt.Sprintf("document: %s", e.Document.Title))
46+
}
47+
48+
if e.Err != nil {
49+
lines = append(lines, fmt.Sprintf("post execute error: %s", e.Err))
50+
}
51+
52+
if e.OrigErr != nil {
53+
lines = append(lines, fmt.Sprintf("original error: %s", e.OrigErr))
54+
}
55+
56+
sb.WriteString(strings.Join(lines, "\n"))
57+
58+
s := sb.String()
59+
60+
return strings.TrimSpace(s)
3361
}
3462

3563
func (e PostExecuteError) Unwrap() error {

post_parse_error.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package hype
33
import (
44
"encoding/json"
55
"errors"
6+
"fmt"
7+
"path/filepath"
8+
"strings"
69
)
710

811
type PostParseError struct {
@@ -29,8 +32,32 @@ func (ppe PostParseError) MarshalJSON() ([]byte, error) {
2932
}
3033

3134
func (ppe PostParseError) Error() string {
32-
b, _ := ppe.MarshalJSON()
33-
return string(b)
35+
sb := &strings.Builder{}
36+
37+
var lines []string
38+
39+
fp := filepath.Join(ppe.Root, ppe.Filename)
40+
if len(fp) > 0 {
41+
lines = append(lines, fmt.Sprintf("filepath: %s", fp))
42+
}
43+
44+
if ppe.Document != nil && len(ppe.Document.Title) > 0 {
45+
lines = append(lines, fmt.Sprintf("document: %s", ppe.Document.Title))
46+
}
47+
48+
if ppe.Err != nil {
49+
lines = append(lines, fmt.Sprintf("post parse error: %s", ppe.Err))
50+
}
51+
52+
if ppe.OrigErr != nil {
53+
lines = append(lines, fmt.Sprintf("original error: %s", ppe.OrigErr))
54+
}
55+
56+
sb.WriteString(strings.Join(lines, "\n"))
57+
58+
s := sb.String()
59+
60+
return strings.TrimSpace(s)
3461
}
3562

3663
func (ppe PostParseError) Unwrap() error {

pre_execute_error.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package hype
33
import (
44
"encoding/json"
55
"errors"
6+
"fmt"
7+
"path/filepath"
8+
"strings"
69
)
710

811
type PreExecuteError struct {
@@ -27,7 +30,28 @@ func (pee PreExecuteError) MarshalJSON() ([]byte, error) {
2730
}
2831

2932
func (pee PreExecuteError) Error() string {
30-
return toError(pee)
33+
sb := &strings.Builder{}
34+
35+
var lines []string
36+
37+
fp := filepath.Join(pee.Root, pee.Filename)
38+
if len(fp) > 0 {
39+
lines = append(lines, fmt.Sprintf("filepath: %s", fp))
40+
}
41+
42+
if pee.Document != nil && len(pee.Document.Title) > 0 {
43+
lines = append(lines, fmt.Sprintf("document: %s", pee.Document.Title))
44+
}
45+
46+
if pee.Err != nil {
47+
lines = append(lines, fmt.Sprintf("pre execute error: %s", pee.Err))
48+
}
49+
50+
sb.WriteString(strings.Join(lines, "\n"))
51+
52+
s := sb.String()
53+
54+
return strings.TrimSpace(s)
3155
}
3256

3357
func (pee PreExecuteError) Unwrap() error {

pre_parse_error.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func (e PreParseError) Error() string {
4040
}
4141

4242
if e.PreParser != nil {
43-
lines = append(lines, fmt.Sprintf("pre_parser: %s", toType(e.PreParser)))
43+
lines = append(lines, fmt.Sprintf("pre parser: %s", toType(e.PreParser)))
4444
}
4545

4646
if e.Err != nil {
47-
lines = append(lines, fmt.Sprintf("error: %s", e.Err))
47+
lines = append(lines, fmt.Sprintf("pre-parse error: %s", e.Err))
4848
}
4949

5050
sb.WriteString(strings.Join(lines, "\n"))

pre_parse_error_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func Test_PreParseError_Error(t *testing.T) {
145145
act = strings.TrimSpace(act)
146146

147147
// fmt.Println(act)
148-
exp := "filepath: root/hype.md\nerror: EOF"
148+
exp := "filepath: root/hype.md\npre-parse error: EOF"
149149

150150
r.Equal(exp, act)
151151
}

0 commit comments

Comments
 (0)