-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
62 lines (50 loc) · 1.21 KB
/
Copy patherrors.go
File metadata and controls
62 lines (50 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package liberrors
import (
"fmt"
"io"
libescapes "github.com/bbfh-dev/lib-ansi-escapes"
)
// Common error names
const (
ERROR_INTERNAL = "Internal Error"
ERROR_ASSERT = "Assertion Error"
ERROR_IO = "I/O Error"
ERROR_READING = "Reading Error"
ERROR_WRITING = "Writing Error"
ERROR_SYNTAX = "Syntax Error"
ERROR_FORMATTING = "Formatting Error"
ERROR_VALIDATION = "Validation Error"
)
type DetailedError struct {
Name string
Details string
Trace []TraceItem
Context Context
}
// It is recommended to use [.Print()] and [.GetPrinted()] for printing.
func (err *DetailedError) Error() string {
return fmt.Sprintf("(%s) %s", err.Name, err.Details)
}
func (err *DetailedError) Print(writer io.Writer) {
fmt.Fprintf(writer, "[!] %s\n", err.Name)
if len(err.Trace) > 0 {
err.Trace[0].PrintRoot(writer)
for _, item := range err.Trace[1:] {
item.PrintNested(writer)
}
}
if !err.Context.IsEmpty() {
err.Context.Print(writer)
}
fmt.Fprintf(
writer,
"\n[?] Details\n %s%s%s\n",
libescapes.TextColorBrightRed,
err.Details,
libescapes.ColorReset,
)
}
func (err *DetailedError) AddTraceItem(item TraceItem) *DetailedError {
err.Trace = append(err.Trace, item)
return err
}