GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_errors.h Lines: 13 16 81.3 %
Date: 2020-02-19 22:14:06 Branches: 0 0 - %

Line Branch Exec Source
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
using v8::Local;
16
using v8::Message;
17
using v8::Value;
18
19
enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR };
20
void AppendExceptionLine(Environment* env,
21
                         Local<Value> er,
22
                         Local<Message> message,
23
                         enum ErrorHandlingMode mode);
24
25
[[noreturn]] void FatalError(const char* location, const char* message);
26
void OnFatalError(const char* location, const char* message);
27
28
// Helpers to construct errors similar to the ones provided by
29
// lib/internal/errors.js.
30
// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be
31
// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning
32
// a `Local<Value>` containing the TypeError with proper code and message
33
34
#define ERRORS_WITH_CODE(V)                                                  \
35
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error)                                 \
36
  V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError)                                    \
37
  V(ERR_BUFFER_TOO_LARGE, Error)                                             \
38
  V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError)                                  \
39
  V(ERR_CONSTRUCT_CALL_INVALID, TypeError)                                   \
40
  V(ERR_CRYPTO_UNKNOWN_CIPHER, Error)                                        \
41
  V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error)                                      \
42
  V(ERR_INVALID_ARG_VALUE, TypeError)                                        \
43
  V(ERR_OSSL_EVP_INVALID_DIGEST, Error)                                      \
44
  V(ERR_INVALID_ARG_TYPE, TypeError)                                         \
45
  V(ERR_INVALID_MODULE_SPECIFIER, TypeError)                                 \
46
  V(ERR_INVALID_PACKAGE_CONFIG, Error)                                       \
47
  V(ERR_INVALID_PACKAGE_TARGET, Error)                                       \
48
  V(ERR_INVALID_TRANSFER_OBJECT, TypeError)                                  \
49
  V(ERR_MEMORY_ALLOCATION_FAILED, Error)                                     \
50
  V(ERR_MISSING_ARGS, TypeError)                                             \
51
  V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, TypeError)                    \
52
  V(ERR_MISSING_PASSPHRASE, TypeError)                                       \
53
  V(ERR_MISSING_PLATFORM_FOR_WORKER, Error)                                  \
54
  V(ERR_NON_CONTEXT_AWARE_DISABLED, Error)                                   \
55
  V(ERR_MODULE_NOT_FOUND, Error)                                             \
56
  V(ERR_OUT_OF_RANGE, RangeError)                                            \
57
  V(ERR_PACKAGE_PATH_NOT_EXPORTED, Error)                                    \
58
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error)                                 \
59
  V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error)                                     \
60
  V(ERR_STRING_TOO_LONG, Error)                                              \
61
  V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError)                              \
62
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError)              \
63
  V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error)                              \
64
  V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error)                               \
65
66
#define V(code, type)                                                         \
67
  inline v8::Local<v8::Value> code(v8::Isolate* isolate,                      \
68
                                   const char* message)       {               \
69
    v8::Local<v8::String> js_code = OneByteString(isolate, #code);            \
70
    v8::Local<v8::String> js_msg = OneByteString(isolate, message);           \
71
    v8::Local<v8::Object> e =                                                 \
72
        v8::Exception::type(js_msg)->ToObject(                                \
73
            isolate->GetCurrentContext()).ToLocalChecked();                   \
74
    e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"),      \
75
           js_code).Check();                                                  \
76
    return e;                                                                 \
77
  }                                                                           \
78
  inline void THROW_ ## code(v8::Isolate* isolate, const char* message) {     \
79
    isolate->ThrowException(code(isolate, message));                          \
80
  }                                                                           \
81
  inline void THROW_ ## code(Environment* env, const char* message) {         \
82
    THROW_ ## code(env->isolate(), message);                                  \
83
  }
84
1754
  ERRORS_WITH_CODE(V)
85
#undef V
86
87
// Errors with predefined static messages
88
89
#define PREDEFINED_ERROR_MESSAGES(V)                                         \
90
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE,                                        \
91
    "Buffer is not available for the current Context")                       \
92
  V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called")              \
93
  V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`")    \
94
  V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher")                             \
95
  V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group")                         \
96
  V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList")     \
97
  V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")               \
98
  V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used")                      \
99
  V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST,                               \
100
    "MessagePort was found in message but not listed in transferList")       \
101
  V(ERR_MISSING_PLATFORM_FOR_WORKER,                                         \
102
    "The V8 platform used by this instance of Node does not support "        \
103
    "creating Workers")                                                      \
104
  V(ERR_NON_CONTEXT_AWARE_DISABLED,                                          \
105
    "Loading non context-aware native modules has been disabled")            \
106
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED,                                        \
107
    "Script execution was interrupted by `SIGINT`")                          \
108
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER,                         \
109
    "Cannot serialize externalized SharedArrayBuffer")                       \
110
  V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint")  \
111
112
#define V(code, message)                                                     \
113
  inline v8::Local<v8::Value> code(v8::Isolate* isolate) {                   \
114
    return code(isolate, message);                                           \
115
  }                                                                          \
116
  inline void THROW_ ## code(v8::Isolate* isolate) {                         \
117
    isolate->ThrowException(code(isolate, message));                         \
118
  }                                                                          \
119
  inline void THROW_ ## code(Environment* env) {                             \
120
    THROW_ ## code(env->isolate());                                          \
121
  }
122
40
  PREDEFINED_ERROR_MESSAGES(V)
123
#undef V
124
125
// Errors with predefined non-static messages
126
11
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
127
                                               int64_t timeout) {
128
22
  std::ostringstream message;
129
11
  message << "Script execution timed out after ";
130
11
  message << timeout << "ms";
131
11
  THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
132
11
}
133
134
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
135
  char message[128];
136
  snprintf(message, sizeof(message),
137
      "Cannot create a Buffer larger than 0x%zx bytes",
138
      v8::TypedArray::kMaxLength);
139
  return ERR_BUFFER_TOO_LARGE(isolate, message);
140
}
141
142
7
inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
143
  char message[128];
144
  snprintf(message, sizeof(message),
145
      "Cannot create a string longer than 0x%x characters",
146
7
      v8::String::kMaxLength);
147
7
  return ERR_STRING_TOO_LONG(isolate, message);
148
}
149
150
#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix)                     \
151
  do {                                                                       \
152
    if (!Buffer::HasInstance(val))                                           \
153
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
154
                                              prefix " must be a buffer");   \
155
  } while (0)
156
157
#define THROW_AND_RETURN_IF_NOT_STRING(env, val, prefix)                     \
158
  do {                                                                       \
159
    if (!val->IsString())                                                    \
160
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
161
                                              prefix " must be a string");   \
162
  } while (0)
163
164
namespace errors {
165
166
class TryCatchScope : public v8::TryCatch {
167
 public:
168
  enum class CatchMode { kNormal, kFatal };
169
170
696283
  explicit TryCatchScope(Environment* env, CatchMode mode = CatchMode::kNormal)
171
696283
      : v8::TryCatch(env->isolate()), env_(env), mode_(mode) {}
172
  ~TryCatchScope();
173
174
  // Since the dtor is not virtual we need to make sure no one creates
175
  // object of it in the free store that might be held by polymorphic pointers.
176
  void* operator new(std::size_t count) = delete;
177
  void* operator new[](std::size_t count) = delete;
178
  TryCatchScope(TryCatchScope&) = delete;
179
  TryCatchScope(TryCatchScope&&) = delete;
180
  TryCatchScope operator=(TryCatchScope&) = delete;
181
  TryCatchScope operator=(TryCatchScope&&) = delete;
182
183
 private:
184
  Environment* env_;
185
  CatchMode mode_;
186
};
187
188
// Trigger the global uncaught exception handler `process._fatalException`
189
// in JS land (which emits the 'uncaughtException' event). If that returns
190
// true, continue program execution, otherwise exit the process.
191
void TriggerUncaughtException(v8::Isolate* isolate,
192
                              const v8::TryCatch& try_catch);
193
void TriggerUncaughtException(v8::Isolate* isolate,
194
                              Local<Value> error,
195
                              Local<Message> message,
196
                              bool from_promise = false);
197
198
const char* errno_string(int errorno);
199
void PerIsolateMessageListener(v8::Local<v8::Message> message,
200
                               v8::Local<v8::Value> error);
201
202
void DecorateErrorStack(Environment* env,
203
                        const errors::TryCatchScope& try_catch);
204
}  // namespace errors
205
206
}  // namespace node
207
208
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
209
210
#endif  // SRC_NODE_ERRORS_H_