1 |
|
|
#ifndef SRC_NODE_ERRORS_H_ |
2 |
|
|
#define SRC_NODE_ERRORS_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "env.h" |
7 |
|
|
#include "v8.h" |
8 |
|
|
|
9 |
|
|
// Use ostringstream to print exact-width integer types |
10 |
|
|
// because the format specifiers are not available on AIX. |
11 |
|
|
#include <sstream> |
12 |
|
|
|
13 |
|
|
namespace node { |
14 |
|
|
|
15 |
|
|
enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR }; |
16 |
|
|
void AppendExceptionLine(Environment* env, |
17 |
|
|
v8::Local<v8::Value> er, |
18 |
|
|
v8::Local<v8::Message> message, |
19 |
|
|
enum ErrorHandlingMode mode); |
20 |
|
|
|
21 |
|
|
[[noreturn]] void FatalError(const char* location, const char* message); |
22 |
|
|
void OnFatalError(const char* location, const char* message); |
23 |
|
|
|
24 |
|
|
// Helpers to construct errors similar to the ones provided by |
25 |
|
|
// lib/internal/errors.js. |
26 |
|
|
// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be |
27 |
|
|
// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning |
28 |
|
|
// a `Local<Value>` containing the TypeError with proper code and message |
29 |
|
|
|
30 |
|
|
#define ERRORS_WITH_CODE(V) \ |
31 |
|
|
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \ |
32 |
|
|
V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \ |
33 |
|
|
V(ERR_BUFFER_TOO_LARGE, Error) \ |
34 |
|
|
V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \ |
35 |
|
|
V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ |
36 |
|
|
V(ERR_CRYPTO_INITIALIZATION_FAILED, Error) \ |
37 |
|
|
V(ERR_CRYPTO_INVALID_AUTH_TAG, TypeError) \ |
38 |
|
|
V(ERR_CRYPTO_INVALID_COUNTER, TypeError) \ |
39 |
|
|
V(ERR_CRYPTO_INVALID_CURVE, TypeError) \ |
40 |
|
|
V(ERR_CRYPTO_INVALID_DIGEST, TypeError) \ |
41 |
|
|
V(ERR_CRYPTO_INVALID_IV, TypeError) \ |
42 |
|
|
V(ERR_CRYPTO_INVALID_JWK, TypeError) \ |
43 |
|
|
V(ERR_CRYPTO_INVALID_KEYLEN, RangeError) \ |
44 |
|
|
V(ERR_CRYPTO_INVALID_KEYPAIR, RangeError) \ |
45 |
|
|
V(ERR_CRYPTO_INVALID_KEYTYPE, RangeError) \ |
46 |
|
|
V(ERR_CRYPTO_INVALID_MESSAGELEN, RangeError) \ |
47 |
|
|
V(ERR_CRYPTO_INVALID_SCRYPT_PARAMS, RangeError) \ |
48 |
|
|
V(ERR_CRYPTO_INVALID_STATE, Error) \ |
49 |
|
|
V(ERR_CRYPTO_INVALID_TAG_LENGTH, RangeError) \ |
50 |
|
|
V(ERR_CRYPTO_OPERATION_FAILED, Error) \ |
51 |
|
|
V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, RangeError) \ |
52 |
|
|
V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \ |
53 |
|
|
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \ |
54 |
|
|
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, Error) \ |
55 |
|
|
V(ERR_CRYPTO_JOB_INIT_FAILED, Error) \ |
56 |
|
|
V(ERR_DLOPEN_FAILED, Error) \ |
57 |
|
|
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \ |
58 |
|
|
V(ERR_INVALID_ARG_VALUE, TypeError) \ |
59 |
|
|
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \ |
60 |
|
|
V(ERR_INVALID_ARG_TYPE, TypeError) \ |
61 |
|
|
V(ERR_INVALID_MODULE, Error) \ |
62 |
|
|
V(ERR_INVALID_THIS, TypeError) \ |
63 |
|
|
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \ |
64 |
|
|
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \ |
65 |
|
|
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \ |
66 |
|
|
V(ERR_MISSING_ARGS, TypeError) \ |
67 |
|
|
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError) \ |
68 |
|
|
V(ERR_MISSING_PASSPHRASE, TypeError) \ |
69 |
|
|
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \ |
70 |
|
|
V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \ |
71 |
|
|
V(ERR_OUT_OF_RANGE, RangeError) \ |
72 |
|
|
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \ |
73 |
|
|
V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \ |
74 |
|
|
V(ERR_STRING_TOO_LONG, Error) \ |
75 |
|
|
V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError) \ |
76 |
|
|
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \ |
77 |
|
|
V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \ |
78 |
|
|
V(ERR_WASI_NOT_STARTED, Error) \ |
79 |
|
|
V(ERR_WORKER_INIT_FAILED, Error) \ |
80 |
|
|
V(ERR_PROTO_ACCESS, Error) \ |
81 |
|
|
V(ERR_QUIC_CANNOT_SET_GROUPS, Error) \ |
82 |
|
|
V(ERR_QUIC_FAILURE_SETTING_SNI_CONTEXT, Error) |
83 |
|
|
|
84 |
|
|
#define V(code, type) \ |
85 |
|
|
inline v8::Local<v8::Value> code(v8::Isolate* isolate, \ |
86 |
|
|
const char* message) { \ |
87 |
|
|
v8::Local<v8::String> js_code = OneByteString(isolate, #code); \ |
88 |
|
|
v8::Local<v8::String> js_msg = OneByteString(isolate, message); \ |
89 |
|
|
v8::Local<v8::Object> e = \ |
90 |
|
|
v8::Exception::type(js_msg)->ToObject( \ |
91 |
|
|
isolate->GetCurrentContext()).ToLocalChecked(); \ |
92 |
|
|
e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"), \ |
93 |
|
|
js_code).Check(); \ |
94 |
|
|
return e; \ |
95 |
|
|
} \ |
96 |
|
|
inline void THROW_ ## code(v8::Isolate* isolate, const char* message) { \ |
97 |
|
|
isolate->ThrowException(code(isolate, message)); \ |
98 |
|
|
} \ |
99 |
|
|
inline void THROW_ ## code(Environment* env, const char* message) { \ |
100 |
|
|
THROW_ ## code(env->isolate(), message); \ |
101 |
|
|
} |
102 |
|
7409 |
ERRORS_WITH_CODE(V) |
103 |
|
1 |
#undef V |
104 |
|
3 |
|
105 |
|
5 |
// Errors with predefined static messages |
106 |
|
|
|
107 |
|
|
#define PREDEFINED_ERROR_MESSAGES(V) \ |
108 |
|
|
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ |
109 |
|
|
"Buffer is not available for the current Context") \ |
110 |
|
|
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ |
111 |
|
|
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ |
112 |
|
|
V(ERR_CRYPTO_INITIALIZATION_FAILED, "Initialization failed") \ |
113 |
|
|
V(ERR_CRYPTO_INVALID_AUTH_TAG, "Invalid authentication tag") \ |
114 |
|
|
V(ERR_CRYPTO_INVALID_COUNTER, "Invalid counter") \ |
115 |
|
|
V(ERR_CRYPTO_INVALID_CURVE, "Invalid EC curve name") \ |
116 |
|
|
V(ERR_CRYPTO_INVALID_DIGEST, "Invalid digest") \ |
117 |
|
|
V(ERR_CRYPTO_INVALID_IV, "Invalid initialization vector") \ |
118 |
|
|
V(ERR_CRYPTO_INVALID_JWK, "Invalid JWK format") \ |
119 |
|
|
V(ERR_CRYPTO_INVALID_KEYLEN, "Invalid key length") \ |
120 |
|
1 |
V(ERR_CRYPTO_INVALID_KEYPAIR, "Invalid key pair") \ |
121 |
|
1 |
V(ERR_CRYPTO_INVALID_KEYTYPE, "Invalid key type") \ |
122 |
|
3 |
V(ERR_CRYPTO_INVALID_MESSAGELEN, "Invalid message length") \ |
123 |
|
5 |
V(ERR_CRYPTO_INVALID_SCRYPT_PARAMS, "Invalid scrypt params") \ |
124 |
|
|
V(ERR_CRYPTO_INVALID_STATE, "Invalid state") \ |
125 |
|
|
V(ERR_CRYPTO_INVALID_TAG_LENGTH, "Invalid taglength") \ |
126 |
|
2 |
V(ERR_CRYPTO_OPERATION_FAILED, "Operation failed") \ |
127 |
|
2 |
V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, \ |
128 |
|
6 |
"Input buffers must have the same byte length") \ |
129 |
|
12 |
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \ |
130 |
|
|
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ |
131 |
|
|
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, "Unsupported crypto operation") \ |
132 |
|
|
V(ERR_CRYPTO_JOB_INIT_FAILED, "Failed to initialize crypto job config") \ |
133 |
|
|
V(ERR_DLOPEN_FAILED, "DLOpen failed") \ |
134 |
|
|
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \ |
135 |
|
|
"Context not associated with Node.js environment") \ |
136 |
|
|
V(ERR_INVALID_MODULE, "No such module") \ |
137 |
|
|
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \ |
138 |
|
|
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ |
139 |
|
|
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ |
140 |
|
|
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \ |
141 |
|
|
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \ |
142 |
|
|
"A message object could not be deserialized successfully in the target " \ |
143 |
|
|
"vm.Context") \ |
144 |
|
|
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, \ |
145 |
|
|
"Object that needs transfer was found in message but not listed " \ |
146 |
|
|
"in transferList") \ |
147 |
|
|
V(ERR_MISSING_PLATFORM_FOR_WORKER, \ |
148 |
|
|
"The V8 platform used by this instance of Node does not support " \ |
149 |
|
|
"creating Workers") \ |
150 |
|
|
V(ERR_NON_CONTEXT_AWARE_DISABLED, \ |
151 |
|
|
"Loading non context-aware native modules has been disabled") \ |
152 |
|
|
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \ |
153 |
|
|
"Script execution was interrupted by `SIGINT`") \ |
154 |
|
|
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") \ |
155 |
|
|
V(ERR_WASI_NOT_STARTED, "wasi.start() has not been called") \ |
156 |
|
|
V(ERR_WORKER_INIT_FAILED, "Worker initialization failure") \ |
157 |
|
|
V(ERR_PROTO_ACCESS, \ |
158 |
|
|
"Accessing Object.prototype.__proto__ has been " \ |
159 |
|
|
"disallowed with --disable-proto=throw") \ |
160 |
|
|
V(ERR_QUIC_CANNOT_SET_GROUPS, "Cannot set groups") \ |
161 |
|
|
V(ERR_QUIC_FAILURE_SETTING_SNI_CONTEXT, "Failure setting SNI context") |
162 |
|
|
|
163 |
|
|
#define V(code, message) \ |
164 |
|
|
inline v8::Local<v8::Value> code(v8::Isolate* isolate) { \ |
165 |
|
|
return code(isolate, message); \ |
166 |
|
|
} \ |
167 |
|
|
inline void THROW_ ## code(v8::Isolate* isolate) { \ |
168 |
|
|
isolate->ThrowException(code(isolate, message)); \ |
169 |
|
|
} \ |
170 |
|
|
inline void THROW_ ## code(Environment* env) { \ |
171 |
|
|
THROW_ ## code(env->isolate()); \ |
172 |
|
|
} |
173 |
|
1376 |
PREDEFINED_ERROR_MESSAGES(V) |
174 |
|
|
#undef V |
175 |
|
|
|
176 |
|
|
// Errors with predefined non-static messages |
177 |
|
13 |
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env, |
178 |
|
|
int64_t timeout) { |
179 |
|
26 |
std::ostringstream message; |
180 |
|
13 |
message << "Script execution timed out after "; |
181 |
|
13 |
message << timeout << "ms"; |
182 |
|
13 |
THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str()); |
183 |
|
13 |
} |
184 |
|
|
|
185 |
|
|
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) { |
186 |
|
|
char message[128]; |
187 |
|
|
snprintf(message, sizeof(message), |
188 |
|
|
"Cannot create a Buffer larger than 0x%zx bytes", |
189 |
|
|
v8::TypedArray::kMaxLength); |
190 |
|
|
return ERR_BUFFER_TOO_LARGE(isolate, message); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
7 |
inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) { |
194 |
|
|
char message[128]; |
195 |
|
|
snprintf(message, sizeof(message), |
196 |
|
|
"Cannot create a string longer than 0x%x characters", |
197 |
|
7 |
v8::String::kMaxLength); |
198 |
|
7 |
return ERR_STRING_TOO_LONG(isolate, message); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix) \ |
202 |
|
|
do { \ |
203 |
|
|
if (!Buffer::HasInstance(val)) \ |
204 |
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \ |
205 |
|
|
prefix " must be a buffer"); \ |
206 |
|
|
} while (0) |
207 |
|
|
|
208 |
|
|
#define THROW_AND_RETURN_IF_NOT_STRING(env, val, prefix) \ |
209 |
|
|
do { \ |
210 |
|
|
if (!val->IsString()) \ |
211 |
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \ |
212 |
|
|
prefix " must be a string"); \ |
213 |
|
|
} while (0) |
214 |
|
|
|
215 |
|
|
namespace errors { |
216 |
|
|
|
217 |
|
|
class TryCatchScope : public v8::TryCatch { |
218 |
|
|
public: |
219 |
|
|
enum class CatchMode { kNormal, kFatal }; |
220 |
|
|
|
221 |
|
1394918 |
explicit TryCatchScope(Environment* env, CatchMode mode = CatchMode::kNormal) |
222 |
|
1394918 |
: v8::TryCatch(env->isolate()), env_(env), mode_(mode) {} |
223 |
|
|
~TryCatchScope(); |
224 |
|
|
|
225 |
|
|
// Since the dtor is not virtual we need to make sure no one creates |
226 |
|
|
// object of it in the free store that might be held by polymorphic pointers. |
227 |
|
|
void* operator new(std::size_t count) = delete; |
228 |
|
|
void* operator new[](std::size_t count) = delete; |
229 |
|
|
TryCatchScope(TryCatchScope&) = delete; |
230 |
|
|
TryCatchScope(TryCatchScope&&) = delete; |
231 |
|
|
TryCatchScope operator=(TryCatchScope&) = delete; |
232 |
|
|
TryCatchScope operator=(TryCatchScope&&) = delete; |
233 |
|
|
|
234 |
|
|
private: |
235 |
|
|
Environment* env_; |
236 |
|
|
CatchMode mode_; |
237 |
|
|
}; |
238 |
|
|
|
239 |
|
|
// Trigger the global uncaught exception handler `process._fatalException` |
240 |
|
|
// in JS land (which emits the 'uncaughtException' event). If that returns |
241 |
|
|
// true, continue program execution, otherwise exit the process. |
242 |
|
|
void TriggerUncaughtException(v8::Isolate* isolate, |
243 |
|
|
const v8::TryCatch& try_catch); |
244 |
|
|
void TriggerUncaughtException(v8::Isolate* isolate, |
245 |
|
|
v8::Local<v8::Value> error, |
246 |
|
|
v8::Local<v8::Message> message, |
247 |
|
|
bool from_promise = false); |
248 |
|
|
|
249 |
|
|
const char* errno_string(int errorno); |
250 |
|
|
void PerIsolateMessageListener(v8::Local<v8::Message> message, |
251 |
|
|
v8::Local<v8::Value> error); |
252 |
|
|
|
253 |
|
|
void DecorateErrorStack(Environment* env, |
254 |
|
|
const errors::TryCatchScope& try_catch); |
255 |
|
|
} // namespace errors |
256 |
|
|
|
257 |
|
|
} // namespace node |
258 |
|
|
|
259 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
260 |
|
|
|
261 |
|
|
#endif // SRC_NODE_ERRORS_H_ |