Repository navigation
Expand file tree
/
Copy patherror.go
More file actions
63 lines (52 loc) · 1.63 KB
/
Copy patherror.go
File metadata and controls
63 lines (52 loc) · 1.63 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
63
package goa
import (
"fmt"
"runtime"
)
// Error with stack trace
type Error interface {
Error() string // Error message
Stack() string // Error stack trace
}
// Argument error
type ArgumentError interface {
Error() string // Error message
Stack() string // Error stack trace
ArgName() string // Name of invalid argument
ArgValue() interface{} // Value of invalid argument
}
// Max size of stack trace string in bytes
var maxStackBytes = 4096
// Build error with stack trace information
func NewError(msg string) Error {
stack := make([]byte, maxStackBytes)
runtime.Stack(stack, false)
return &errorInfo{msg, string(stack)}
}
// Helper method with fmt.Errorf like behavior
func NewErrorf(format string, a ...interface{}) Error {
stack := make([]byte, maxStackBytes)
runtime.Stack(stack, false)
return &errorInfo{fmt.Sprintf(format, a...), string(stack)}
}
// Build argument error from message, argument name and value
func NewArgumentError(msg string, argName string, argValue interface{}) ArgumentError {
stack := make([]byte, maxStackBytes)
runtime.Stack(stack, false)
return &argumentErrorInfo{&errorInfo{msg, string(stack)}, argName, argValue}
}
// Error implementation
type errorInfo struct {
message string
stack string
}
func (err *errorInfo) Error() string { return err.message }
func (err *errorInfo) Stack() string { return err.stack }
// ArgumentError implementation
type argumentErrorInfo struct {
*errorInfo
argName string
argValue interface{}
}
func (err *argumentErrorInfo) ArgName() string { return err.argName }
func (err *argumentErrorInfo) ArgValue() interface{} { return err.argValue }