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