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-02-26 22:23:30 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
enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR };
18
void AppendExceptionLine(Environment* env,
19
                         v8::Local<v8::Value> er,
20
                         v8::Local<v8::Message> message,
21
                         enum ErrorHandlingMode mode);
22
23
[[noreturn]] void FatalError(const char* location, const char* message);
24
void OnFatalError(const char* location, const char* message);
25
26
void PrintErrorString(const char* format, ...);
27
28
void ReportException(Environment* env, const v8::TryCatch& try_catch);
29
30
void FatalException(v8::Isolate* isolate,
31
                    v8::Local<v8::Value> error,
32
                    v8::Local<v8::Message> message);
33
34
void FatalException(const v8::FunctionCallbackInfo<v8::Value>& args);
35
36
// Helpers to construct errors similar to the ones provided by
37
// lib/internal/errors.js.
38
// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be
39
// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning
40
// a `Local<Value>` containing the TypeError with proper code and message
41
42
#define ERRORS_WITH_CODE(V)                                                  \
43
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error)                                 \
44
  V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError)                                    \
45
  V(ERR_BUFFER_TOO_LARGE, Error)                                             \
46
  V(ERR_CANNOT_TRANSFER_OBJECT, TypeError)                                   \
47
  V(ERR_CLOSED_MESSAGE_PORT, Error)                                          \
48
  V(ERR_CONSTRUCT_CALL_REQUIRED, Error)                                      \
49
  V(ERR_INVALID_ARG_VALUE, TypeError)                                        \
50
  V(ERR_INVALID_ARG_TYPE, TypeError)                                         \
51
  V(ERR_INVALID_TRANSFER_OBJECT, TypeError)                                  \
52
  V(ERR_MEMORY_ALLOCATION_FAILED, Error)                                     \
53
  V(ERR_MISSING_ARGS, TypeError)                                             \
54
  V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, TypeError)                    \
55
  V(ERR_MISSING_MODULE, Error)                                               \
56
  V(ERR_MISSING_PLATFORM_FOR_WORKER, 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).FromJust();                                               \
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
5830
  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_CANNOT_TRANSFER_OBJECT, "Cannot transfer object of unsupported type")\
91
  V(ERR_CLOSED_MESSAGE_PORT, "Cannot send data on closed MessagePort")       \
92
  V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`")    \
93
  V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList")     \
94
  V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")               \
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_SCRIPT_EXECUTION_INTERRUPTED,                                        \
101
    "Script execution was interrupted by `SIGINT`")                          \
102
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER,                         \
103
    "Cannot serialize externalized SharedArrayBuffer")                       \
104
105
#define V(code, message)                                                     \
106
  inline v8::Local<v8::Value> code(v8::Isolate* isolate) {                   \
107
    return code(isolate, message);                                           \
108
  }                                                                          \
109
  inline void THROW_ ## code(v8::Isolate* isolate) {                         \
110
    isolate->ThrowException(code(isolate, message));                         \
111
  }                                                                          \
112
  inline void THROW_ ## code(Environment* env) {                             \
113
    THROW_ ## code(env->isolate());                                          \
114
  }
115
23
  PREDEFINED_ERROR_MESSAGES(V)
116
#undef V
117
118
// Errors with predefined non-static messages
119
508
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
120
                                               int64_t timeout) {
121
508
  std::ostringstream message;
122
508
  message << "Script execution timed out after ";
123
508
  message << timeout << "ms";
124
508
  THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
125
508
}
126
127
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
128
  char message[128];
129
  snprintf(message, sizeof(message),
130
      "Cannot create a Buffer larger than 0x%zx bytes",
131
      v8::TypedArray::kMaxLength);
132
  return ERR_BUFFER_TOO_LARGE(isolate, message);
133
}
134
135
8
inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
136
  char message[128];
137
  snprintf(message, sizeof(message),
138
      "Cannot create a string longer than 0x%x characters",
139
8
      v8::String::kMaxLength);
140
8
  return ERR_STRING_TOO_LONG(isolate, message);
141
}
142
143
#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix)                     \
144
  do {                                                                       \
145
    if (!Buffer::HasInstance(val))                                           \
146
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
147
                                              prefix " must be a buffer");   \
148
  } while (0)
149
150
#define THROW_AND_RETURN_IF_NOT_STRING(env, val, prefix)                     \
151
  do {                                                                       \
152
    if (!val->IsString())                                                    \
153
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
154
                                              prefix " must be a string");   \
155
  } while (0)
156
157
namespace errors {
158
159
class TryCatchScope : public v8::TryCatch {
160
 public:
161
  enum class CatchMode { kNormal, kFatal };
162
163
164273
  explicit TryCatchScope(Environment* env, CatchMode mode = CatchMode::kNormal)
164
164273
      : v8::TryCatch(env->isolate()), env_(env), mode_(mode) {}
165
  ~TryCatchScope();
166
167
  // Since the dtor is not virtual we need to make sure no one creates
168
  // object of it in the free store that might be held by polymorphic pointers.
169
  void* operator new(std::size_t count) = delete;
170
  void* operator new[](std::size_t count) = delete;
171
  TryCatchScope(TryCatchScope&) = delete;
172
  TryCatchScope(TryCatchScope&&) = delete;
173
  TryCatchScope operator=(TryCatchScope&) = delete;
174
  TryCatchScope operator=(TryCatchScope&&) = delete;
175
176
 private:
177
  Environment* env_;
178
  CatchMode mode_;
179
};
180
181
const char* errno_string(int errorno);
182
183
}  // namespace errors
184
185
void DecorateErrorStack(Environment* env,
186
                        const errors::TryCatchScope& try_catch);
187
}  // namespace node
188
189
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
190
191
#endif  // SRC_NODE_ERRORS_H_