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-08-17 22:35:23 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_INVALID_ARG_TYPE, TypeError)                                         \
46
  V(ERR_INVALID_MODULE_SPECIFIER, TypeError)                                 \
47
  V(ERR_INVALID_PACKAGE_CONFIG, SyntaxError)                                 \
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_MODULE_NOT_FOUND, Error)                                             \
55
  V(ERR_OUT_OF_RANGE, RangeError)                                            \
56
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error)                                 \
57
  V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error)                                     \
58
  V(ERR_STRING_TOO_LONG, Error)                                              \
59
  V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError)                              \
60
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError)              \
61
62
#define V(code, type)                                                         \
63
  inline v8::Local<v8::Value> code(v8::Isolate* isolate,                      \
64
                                   const char* message)       {               \
65
    v8::Local<v8::String> js_code = OneByteString(isolate, #code);            \
66
    v8::Local<v8::String> js_msg = OneByteString(isolate, message);           \
67
    v8::Local<v8::Object> e =                                                 \
68
        v8::Exception::type(js_msg)->ToObject(                                \
69
            isolate->GetCurrentContext()).ToLocalChecked();                   \
70
    e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"),      \
71
           js_code).Check();                                                  \
72
    return e;                                                                 \
73
  }                                                                           \
74
  inline void THROW_ ## code(v8::Isolate* isolate, const char* message) {     \
75
    isolate->ThrowException(code(isolate, message));                          \
76
  }                                                                           \
77
  inline void THROW_ ## code(Environment* env, const char* message) {         \
78
    THROW_ ## code(env->isolate(), message);                                  \
79
  }
80
6574
  ERRORS_WITH_CODE(V)
81
#undef V
82
83
// Errors with predefined static messages
84
85
#define PREDEFINED_ERROR_MESSAGES(V)                                         \
86
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE,                                        \
87
    "Buffer is not available for the current Context")                       \
88
  V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called")              \
89
  V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`")    \
90
  V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList")     \
91
  V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")               \
92
  V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST,                               \
93
    "MessagePort was found in message but not listed in transferList")       \
94
  V(ERR_MISSING_PLATFORM_FOR_WORKER,                                         \
95
    "The V8 platform used by this instance of Node does not support "        \
96
    "creating Workers")                                                      \
97
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED,                                        \
98
    "Script execution was interrupted by `SIGINT`")                          \
99
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER,                         \
100
    "Cannot serialize externalized SharedArrayBuffer")                       \
101
102
#define V(code, message)                                                     \
103
  inline v8::Local<v8::Value> code(v8::Isolate* isolate) {                   \
104
    return code(isolate, message);                                           \
105
  }                                                                          \
106
  inline void THROW_ ## code(v8::Isolate* isolate) {                         \
107
    isolate->ThrowException(code(isolate, message));                         \
108
  }                                                                          \
109
  inline void THROW_ ## code(Environment* env) {                             \
110
    THROW_ ## code(env->isolate());                                          \
111
  }
112
29
  PREDEFINED_ERROR_MESSAGES(V)
113
#undef V
114
115
// Errors with predefined non-static messages
116
511
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
117
                                               int64_t timeout) {
118
511
  std::ostringstream message;
119
511
  message << "Script execution timed out after ";
120
511
  message << timeout << "ms";
121
511
  THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
122
511
}
123
124
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
125
  char message[128];
126
  snprintf(message, sizeof(message),
127
      "Cannot create a Buffer larger than 0x%zx bytes",
128
      v8::TypedArray::kMaxLength);
129
  return ERR_BUFFER_TOO_LARGE(isolate, message);
130
}
131
132
8
inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
133
  char message[128];
134
  snprintf(message, sizeof(message),
135
      "Cannot create a string longer than 0x%x characters",
136
8
      v8::String::kMaxLength);
137
8
  return ERR_STRING_TOO_LONG(isolate, message);
138
}
139
140
#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix)                     \
141
  do {                                                                       \
142
    if (!Buffer::HasInstance(val))                                           \
143
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
144
                                              prefix " must be a buffer");   \
145
  } while (0)
146
147
#define THROW_AND_RETURN_IF_NOT_STRING(env, val, prefix)                     \
148
  do {                                                                       \
149
    if (!val->IsString())                                                    \
150
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
151
                                              prefix " must be a string");   \
152
  } while (0)
153
154
namespace errors {
155
156
class TryCatchScope : public v8::TryCatch {
157
 public:
158
  enum class CatchMode { kNormal, kFatal };
159
160
226818
  explicit TryCatchScope(Environment* env, CatchMode mode = CatchMode::kNormal)
161
226818
      : v8::TryCatch(env->isolate()), env_(env), mode_(mode) {}
162
  ~TryCatchScope();
163
164
  // Since the dtor is not virtual we need to make sure no one creates
165
  // object of it in the free store that might be held by polymorphic pointers.
166
  void* operator new(std::size_t count) = delete;
167
  void* operator new[](std::size_t count) = delete;
168
  TryCatchScope(TryCatchScope&) = delete;
169
  TryCatchScope(TryCatchScope&&) = delete;
170
  TryCatchScope operator=(TryCatchScope&) = delete;
171
  TryCatchScope operator=(TryCatchScope&&) = delete;
172
173
 private:
174
  Environment* env_;
175
  CatchMode mode_;
176
};
177
178
// Trigger the global uncaught exception handler `process._fatalException`
179
// in JS land (which emits the 'uncaughtException' event). If that returns
180
// true, continue program execution, otherwise exit the process.
181
void TriggerUncaughtException(v8::Isolate* isolate,
182
                              const v8::TryCatch& try_catch);
183
void TriggerUncaughtException(v8::Isolate* isolate,
184
                              Local<Value> error,
185
                              Local<Message> message,
186
                              bool from_promise = false);
187
188
const char* errno_string(int errorno);
189
void PerIsolateMessageListener(v8::Local<v8::Message> message,
190
                               v8::Local<v8::Value> error);
191
192
void DecorateErrorStack(Environment* env,
193
                        const errors::TryCatchScope& try_catch);
194
}  // namespace errors
195
196
}  // namespace node
197
198
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
199
200
#endif  // SRC_NODE_ERRORS_H_