-
Notifications
You must be signed in to change notification settings - Fork 14
/
errors.go
87 lines (67 loc) · 1.64 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package lua
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/krakendio/binder"
)
type ErrWrongChecksumType string
func (e ErrWrongChecksumType) Error() string {
return "lua: wrong cheksum type for source " + string(e)
}
type ErrWrongChecksum struct {
Source, Actual, Expected string
}
func (e ErrWrongChecksum) Error() string {
return fmt.Sprintf("lua: wrong cheksum for source %s. have: %v, want: %v", e.Source, e.Actual, e.Expected)
}
type ErrUnknownSource string
func (e ErrUnknownSource) Error() string {
return "lua: unable to load required source " + string(e)
}
var errNeedsArguments = errors.New("need arguments")
type ErrInternal string
func (e ErrInternal) Error() string {
return string(e)
}
type ErrInternalHTTP struct {
msg string
code int
}
func (e ErrInternalHTTP) StatusCode() int {
return e.code
}
func (e ErrInternalHTTP) Error() string {
return e.msg
}
func ToError(e error) error {
if e == nil {
return nil
}
if _, ok := e.(*binder.Error); !ok {
return e
}
originalMsg := e.Error()
start := strings.Index(originalMsg, ":")
if l := len(originalMsg); originalMsg[l-1] == ')' && originalMsg[l-5] == '(' {
code, err := strconv.Atoi(originalMsg[l-4 : l-1])
if err != nil {
code = 500
}
return ErrInternalHTTP{msg: originalMsg[start+2 : l-6], code: code}
}
return ErrInternal(originalMsg[start+2:])
}
func RegisterErrors(b *binder.Binder) {
b.Func("custom_error", func(c *binder.Context) error {
switch c.Top() {
case 0:
return errNeedsArguments
case 1:
return errors.New(c.Arg(1).String())
default:
return fmt.Errorf("%s (%d)", c.Arg(1).String(), int(c.Arg(2).Number()))
}
})
}