-
Notifications
You must be signed in to change notification settings - Fork 6
/
errors.go
65 lines (53 loc) · 1.42 KB
/
errors.go
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
63
64
65
package console
import (
"fmt"
"os"
)
type (
// ErrorHandler is a function that handles errors.
//
// The handler can choose not to bubble up the error by returning nil.
ErrorHandler func(err error) error
// Err is the Console base error type.
//
// All errors that bubble up to the error handler should be
// wrapped in this error type.
//
// There are more concrete error types that wrap this one defined below
// this allow for easy use of errors.As.
Err struct {
err error
message string
}
// PreReadError is an error that occurs during the pre-read phase.
PreReadError struct{ Err }
// ParseError is an error that occurs during the parsing phase.
ParseError struct{ Err }
// LineHookError is an error that occurs during the line hook phase.
LineHookError struct{ Err }
// ExecutionError is an error that occurs during the execution phase.
ExecutionError struct{ Err }
)
func defaultErrorHandler(err error) error {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return nil
}
// newError creates a new Err.
func newError(err error, message string) Err {
return Err{
err: err,
message: message,
}
}
// Error returns the error message with an optional
// message prefix.
func (e Err) Error() string {
if len(e.message) > 0 {
return fmt.Sprintf("%s: %s", e.message, e.err.Error())
}
return e.err.Error()
}
// Unwrap implements the errors Unwrap interface.
func (e Err) Unwrap() error {
return e.err
}