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