-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfundamental.go
More file actions
65 lines (51 loc) · 1.15 KB
/
fundamental.go
File metadata and controls
65 lines (51 loc) · 1.15 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
64
65
package errors
import (
"fmt"
"regexp"
"strconv"
"github.com/golage/errors/stacktrace"
)
var (
regex = regexp.MustCompile("error ([0-9]*): (.*)")
)
// Fundamental interface of fundamental error
type Fundamental interface {
error
// Code returns error code
Code() Code
// Message returns error message
Message() string
// Stacktrace returns error stacktrace
StackTrace() stacktrace.Stacktrace
}
type fundamental struct {
code Code
message string
stackTrace stacktrace.Stacktrace
}
func (err fundamental) Marshal() string {
return fmt.Sprintf("error %d: %v", err.code, err.message)
}
func (err *fundamental) Unmarshal(message string) {
err.code = CodeUnknown
err.message = message
matches := regex.FindStringSubmatch(message)
if len(matches) == 3 {
if code, e := strconv.Atoi(matches[1]); e == nil {
err.code = Code(code)
err.message = matches[2]
}
}
}
func (err fundamental) Error() string {
return err.Marshal()
}
func (err fundamental) Code() Code {
return err.code
}
func (err fundamental) Message() string {
return err.message
}
func (err fundamental) StackTrace() stacktrace.Stacktrace {
return err.stackTrace
}