-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patherrors.go
More file actions
142 lines (114 loc) · 4.41 KB
/
errors.go
File metadata and controls
142 lines (114 loc) · 4.41 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
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
132
133
134
135
136
137
138
139
140
141
142
package qjs
import (
"errors"
"fmt"
"reflect"
"runtime/debug"
)
var (
ErrRType = reflect.TypeOf((*error)(nil)).Elem()
ErrZeroRValue = reflect.Zero(ErrRType)
ErrCallFuncOnNonObject = errors.New("cannot call function on non-object")
ErrNotAnObject = errors.New("value is not an object")
ErrObjectNotAConstructor = errors.New("object not a constructor")
ErrInvalidFileName = errors.New("file name is required")
ErrMissingProperties = errors.New("value has no properties")
ErrInvalidPointer = errors.New("null pointer dereference")
ErrIndexOutOfRange = errors.New("index out of range")
ErrNoNullTerminator = errors.New("no NUL terminator")
ErrInvalidContext = errors.New("invalid context")
ErrNotANumber = errors.New("js value is not a number")
ErrAsyncFuncRequirePromise = errors.New("jsFunctionProxy: async function requires a promise")
ErrEmptyStringToNumber = errors.New("empty string cannot be converted to number")
ErrJsFuncDeallocated = errors.New("js function context has been deallocated")
ErrNotByteArray = errors.New("invalid TypedArray: buffer is not a byte array")
ErrNotArrayBuffer = errors.New("input is not an ArrayBuffer")
ErrMissingBufferProperty = errors.New("invalid TypedArray: missing buffer property")
ErrRuntimeClosed = errors.New("runtime is closed")
ErrNilModule = errors.New("WASM module is nil")
ErrNilHandle = errors.New("handle is nil")
ErrChanClosed = errors.New("channel is closed")
ErrChanSend = errors.New("channel send would block: buffer full or no receiver ready")
ErrChanReceive = errors.New("channel receive would block: buffer empty or no sender ready")
ErrChanCloseReceiveOnly = errors.New("cannot close receive-only channel")
)
func combineErrors(errs ...error) error {
if len(errs) == 0 {
return nil
}
var errStr string
for _, err := range errs {
if err != nil {
errStr += err.Error() + "\n"
}
}
return errors.New(errStr)
}
func newMaxLengthExceededErr(request uint, maxLen int64, index int) error {
return fmt.Errorf("length %d exceeds max %d at index %d", request, maxLen, index)
}
func newOverflowErr(value any, targetType string) error {
return fmt.Errorf("value %v overflows %s", value, targetType)
}
func newGoToJsErr(kind string, err error, details ...string) error {
detail := ""
if len(details) > 0 {
detail = " " + details[0]
}
if err == nil {
return fmt.Errorf("cannot convert Go%s '%s' to JS", detail, kind)
}
return fmt.Errorf("cannot convert Go%s '%s' to JS: %w", detail, kind, err)
}
func newJsToGoErr(kind *Value, err error, details ...string) error {
detail := ""
if len(details) > 0 {
detail = " " + details[0]
}
kindStr := ""
var kindErr error
if kind != nil {
kindStr, kindErr = kind.JSONStringify()
if kindErr != nil {
kindStr = fmt.Errorf("(%w), %s", kindErr, kind.String()).Error()
}
}
if kindStr == "undefined" || kindStr == "null" {
kindStr = kind.Type()
}
if kindStr != "" {
kindStr = " " + kindStr
}
if err == nil {
return fmt.Errorf("cannot convert JS%s%s to Go", detail, kindStr)
}
return fmt.Errorf("cannot convert JS%s%s to Go: %w", detail, kindStr, err)
}
func newArgConversionErr(index int, err error) error {
return fmt.Errorf("cannot convert JS function argument at index %d: %w", index, err)
}
func newInvalidGoTypeErr(expect string, got any) error {
return fmt.Errorf("expected GO type %s, got %T", expect, got)
}
func newInvalidJsInputErr(kind string, input *Value) (err error) {
var detail string
if detail, err = input.JSONStringify(); err != nil {
detail = fmt.Sprintf("(JSONStringify failed: %v), (.String()) %s", err, input.String())
}
return fmt.Errorf("expected JS %s, got %s=%s", kind, input.Type(), detail)
}
func newJsStringifyErr(kind string, err error) error {
return fmt.Errorf("js %s: %w", kind, err)
}
func newProxyErr(id uint64, r any) error {
if err, ok := r.(error); ok {
return fmt.Errorf("functionProxy [%d]: %w\n%s", id, err, debug.Stack())
}
if str, ok := r.(string); ok {
return fmt.Errorf("functionProxy [%d]: %s\n%s", id, str, debug.Stack())
}
return fmt.Errorf("functionProxy [%d]: %v\n%s", id, r, debug.Stack())
}
func newInvokeErr(input *Value, err error) error {
return fmt.Errorf("cannot call getTime on JS value '%s', err=%w", input.String(), err)
}