...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package logger
16
17 import (
18 "fmt"
19 "path/filepath"
20 "runtime"
21 )
22
23
24 const (
25 RuntimeErrorSkipCall = 1
26 )
27
28
29
30 type RuntimeError struct {
31 line int
32 file string
33 message string
34 function string
35 arguments []interface{}
36 }
37
38
39 func NewRuntimeError(message string, arguments ...interface{}) *RuntimeError {
40 return NewRuntimeErrorBase(RuntimeErrorSkipCall, message, arguments...)
41 }
42
43
44 func NewRuntimeErrorBase(skipCall int, message string, arguments ...interface{}) *RuntimeError {
45 pc, path, line, _ := runtime.Caller(skipCall + 1)
46
47 return &RuntimeError{
48 line: line,
49 file: filepath.Base(path),
50 message: message,
51 function: filepath.Base(runtime.FuncForPC(pc).Name()),
52 arguments: arguments,
53 }
54 }
55
56
57
58 func (r *RuntimeError) Error() string {
59 var formatted string
60
61 var err error
62
63 record := &Record{
64 Message: r.message,
65 Arguments: r.arguments,
66 }
67
68 if formatted, err = NewFormatter().FormatMessage(record); err != nil {
69 formatted = r.message
70 }
71
72 return fmt.Sprintf("%s:%d:%s(): %s",
73 r.file,
74 r.line,
75 r.function,
76 formatted,
77 )
78 }
79
80
81 func (r *RuntimeError) Unwrap() error {
82 for _, argument := range r.arguments {
83 if err, ok := argument.(error); ok {
84 return err
85 }
86 }
87
88 return nil
89 }
90
View as plain text