-
Notifications
You must be signed in to change notification settings - Fork 57
/
error-handlers.ts
197 lines (180 loc) · 6.84 KB
/
error-handlers.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @module ErrorHandlers
* This module is designed to provide utility functions for validating
* function parameters. It includes functions that throw type errors if
* the parameters do not meet specified criteria, such as being defined,
* a number, a string, a function, or an array. This module helps ensure
* that functions receive the correct types of inputs, enhancing code
* reliability and reducing runtime errors.
*/
import {
SupportedType,
isArray,
isBigInt,
isBigNumber,
isBigNumberish,
isBuffer,
isDefined,
isFunction,
isHexadecimal,
isNumber,
isObject,
isString,
isStringifiedBigInt,
isSupportedType,
isType,
isUint8Array
} from "./type-checks"
/**
* @throws Throws a type error if the parameter value has not been defined.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireDefined(parameterValue: any, parameterName: string) {
if (!isDefined(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not defined`)
}
}
/**
* @throws Throws a type error if the parameter value is not a number.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireNumber(parameterValue: number, parameterName: string) {
if (!isNumber(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a number, received type: ${typeof parameterValue}`)
}
}
/**
* @throws Throws a type error if the parameter value is not a string.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireString(parameterValue: string, parameterName: string) {
if (!isString(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a string, received type: ${typeof parameterValue}`)
}
}
/**
* @throws Throws a type error if the parameter value is not a function.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireFunction(parameterValue: Function, parameterName: string) {
if (!isFunction(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a function, received type: ${typeof parameterValue}`)
}
}
/**
* @throws Throws a type error if the parameter value is not an Array.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireArray(parameterValue: any[], parameterName: string) {
if (!isArray(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not an Array instance`)
}
}
/**
* @throws Throws a type error if the parameter value is not a Uint8Array.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireUint8Array(parameterValue: Uint8Array, parameterName: string) {
if (!isUint8Array(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a Uint8Array instance`)
}
}
/**
* @throws Throws a type error if the parameter value is not a Buffer.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireBuffer(parameterValue: Buffer, parameterName: string) {
if (!isBuffer(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a Buffer instance`)
}
}
/**
* @throws Throws a type error if the parameter value is not an object.
* Please, note that arrays are also objects in JavaScript.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireObject(parameterValue: object, parameterName: string) {
if (!isObject(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not an object, received type: ${typeof parameterValue}`)
}
}
/**
* @throws Throws a type error if the parameter value is not a bigint.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireBigInt(parameterValue: bigint, parameterName: string) {
if (!isBigInt(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a bigint, received type: ${typeof parameterValue}`)
}
}
/**
* @throws Throws a type error if the parameter value is not a stringified bigint.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireStringifiedBigInt(parameterValue: string, parameterName: string) {
if (!isStringifiedBigInt(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a stringified bigint`)
}
}
/**
* @throws Throws a type error if the parameter value is not a hexadecimal string.
* If 'prefix' is 'true', the string must start with '0x' or '0X' followed by one or more
* hexadecimal digits (0-9, a-f, A-F), otherwise no prefix is expected. 'prefix' is optional and
* if its value it is not explicitly defined it will be set to 'true' by default.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
* @param prefix A boolean to include or not a '0x' or '0X' prefix.
*/
export function requireHexadecimal(parameterValue: string, parameterName: string, prefix = true) {
if (!isHexadecimal(parameterValue, prefix)) {
throw new TypeError(`Parameter '${parameterName}' is not a hexadecimal string`)
}
}
/**
* @throws Throws a type error if the parameter value is not a bignumber.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireBigNumber(parameterValue: any, parameterName: string) {
if (!isBigNumber(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a bignumber`)
}
}
/**
* @throws Throws a type error if the parameter value is not a bignumber-ish.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireBigNumberish(parameterValue: any, parameterName: string) {
if (!isBigNumberish(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a bignumber-ish`)
}
}
/**
* @throws Throws a type error if the parameter value type is not part of the list of types.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireTypes(parameterValue: any, parameterName: string, types: SupportedType[]) {
for (const type of types) {
if (!isSupportedType(type)) {
throw new Error(`Type '${type}' is not supported`)
}
}
for (const type of types) {
if (isType(parameterValue, type)) {
return
}
}
throw new TypeError(`Parameter '${parameterName}' is none of the following types: ${types.join(", ")}`)
}