-
Notifications
You must be signed in to change notification settings - Fork 14
/
errors.go
131 lines (104 loc) · 2.79 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
}
type ErrInternalHTTPWithContentType struct {
ErrInternalHTTP
contentType string
}
func (e ErrInternalHTTPWithContentType) Encoding() string {
return e.contentType
}
const separator = " || "
func ToError(e error, source *SourceMap) error {
if e == nil {
return nil
}
binderError, ok := e.(*binder.Error)
if !ok {
return e
}
originalMsg := binderError.Error()
msgSplitIndex := strings.Index(originalMsg, ":")
errMsgParts := strings.Split(originalMsg[msgSplitIndex+2:], separator)
if len(errMsgParts) == 0 {
return binderError
}
// Internal errors not coming from a LUA custom_error()
if len(errMsgParts) == 1 {
if source == nil {
return ErrInternal(errMsgParts[0])
}
luaLine, convErr := strconv.Atoi(originalMsg[5:msgSplitIndex])
if convErr != nil {
luaLine = 0
}
if affectedScript, relativeLine, err := source.AffectedSource(luaLine); err == nil {
return ErrInternal(fmt.Sprintf("%s (%s:L%d)", errMsgParts[0], affectedScript, relativeLine))
}
return ErrInternal(errMsgParts[0])
}
// If the error was correctly splitted by separator means we're dealing with a LUA custom_error()
code, err := strconv.Atoi(errMsgParts[1])
if err != nil {
code = 500
}
if code == -1 {
return ErrInternal(errMsgParts[0])
}
errHTTP := ErrInternalHTTP{msg: errMsgParts[0], code: code}
if len(errMsgParts) == 2 {
return errHTTP
}
return ErrInternalHTTPWithContentType{
ErrInternalHTTP: errHTTP,
contentType: errMsgParts[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 fmt.Errorf("%s%s%d", c.Arg(1).String(), separator, -1)
case 2:
return fmt.Errorf("%s%s%d", c.Arg(1).String(), separator, int(c.Arg(2).Number()))
default:
return fmt.Errorf("%s%s%d%s%s", c.Arg(1).String(), separator, int(c.Arg(2).Number()), separator, c.Arg(3).String())
}
})
}