summaryrefslogtreecommitdiff
path: root/src/node_errors.h
blob: 28bf6a670f8ecc39d15171bc63bca7c403f98660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef SRC_NODE_ERRORS_H_
#define SRC_NODE_ERRORS_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "node.h"
#include "util-inl.h"
#include "env-inl.h"
#include "v8.h"

// Use ostringstream to print exact-width integer types
// because the format specifiers are not available on AIX.
#include <sstream>

namespace node {

enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR };
void AppendExceptionLine(Environment* env,
                         v8::Local<v8::Value> er,
                         v8::Local<v8::Message> message,
                         enum ErrorHandlingMode mode);

[[noreturn]] void FatalError(const char* location, const char* message);
void OnFatalError(const char* location, const char* message);

void PrintErrorString(const char* format, ...);

void ReportException(Environment* env, const v8::TryCatch& try_catch);

void FatalException(v8::Isolate* isolate,
                    v8::Local<v8::Value> error,
                    v8::Local<v8::Message> message);

void FatalException(const v8::FunctionCallbackInfo<v8::Value>& args);

// Helpers to construct errors similar to the ones provided by
// lib/internal/errors.js.
// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be
// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning
// a `Local<Value>` containing the TypeError with proper code and message

#define ERRORS_WITH_CODE(V)                                                  \
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error)                                 \
  V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError)                                    \
  V(ERR_BUFFER_TOO_LARGE, Error)                                             \
  V(ERR_CANNOT_TRANSFER_OBJECT, TypeError)                                   \
  V(ERR_CLOSED_MESSAGE_PORT, Error)                                          \
  V(ERR_CONSTRUCT_CALL_REQUIRED, Error)                                      \
  V(ERR_INVALID_ARG_VALUE, TypeError)                                        \
  V(ERR_INVALID_ARG_TYPE, TypeError)                                         \
  V(ERR_INVALID_TRANSFER_OBJECT, TypeError)                                  \
  V(ERR_MEMORY_ALLOCATION_FAILED, Error)                                     \
  V(ERR_MISSING_ARGS, TypeError)                                             \
  V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, TypeError)                    \
  V(ERR_MISSING_MODULE, Error)                                               \
  V(ERR_MISSING_PLATFORM_FOR_WORKER, Error)                                  \
  V(ERR_OUT_OF_RANGE, RangeError)                                            \
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error)                                 \
  V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error)                                     \
  V(ERR_STRING_TOO_LONG, Error)                                              \
  V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError)                              \
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError)              \

#define V(code, type)                                                         \
  inline v8::Local<v8::Value> code(v8::Isolate* isolate,                      \
                                   const char* message)       {               \
    v8::Local<v8::String> js_code = OneByteString(isolate, #code);            \
    v8::Local<v8::String> js_msg = OneByteString(isolate, message);           \
    v8::Local<v8::Object> e =                                                 \
        v8::Exception::type(js_msg)->ToObject(                                \
            isolate->GetCurrentContext()).ToLocalChecked();                   \
    e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"),      \
           js_code).FromJust();                                               \
    return e;                                                                 \
  }                                                                           \
  inline void THROW_ ## code(v8::Isolate* isolate, const char* message) {     \
    isolate->ThrowException(code(isolate, message));                          \
  }                                                                           \
  inline void THROW_ ## code(Environment* env, const char* message) {         \
    THROW_ ## code(env->isolate(), message);                                  \
  }
  ERRORS_WITH_CODE(V)
#undef V

// Errors with predefined static messages

#define PREDEFINED_ERROR_MESSAGES(V)                                         \
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE,                                        \
    "Buffer is not available for the current Context")                       \
  V(ERR_CANNOT_TRANSFER_OBJECT, "Cannot transfer object of unsupported type")\
  V(ERR_CLOSED_MESSAGE_PORT, "Cannot send data on closed MessagePort")       \
  V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`")    \
  V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList")     \
  V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")               \
  V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST,                               \
    "MessagePort was found in message but not listed in transferList")       \
  V(ERR_MISSING_PLATFORM_FOR_WORKER,                                         \
    "The V8 platform used by this instance of Node does not support "        \
    "creating Workers")                                                      \
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED,                                        \
    "Script execution was interrupted by `SIGINT`")                          \
  V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER,                         \
    "Cannot serialize externalized SharedArrayBuffer")                       \

#define V(code, message)                                                     \
  inline v8::Local<v8::Value> code(v8::Isolate* isolate) {                   \
    return code(isolate, message);                                           \
  }                                                                          \
  inline void THROW_ ## code(v8::Isolate* isolate) {                         \
    isolate->ThrowException(code(isolate, message));                         \
  }                                                                          \
  inline void THROW_ ## code(Environment* env) {                             \
    THROW_ ## code(env->isolate());                                          \
  }
  PREDEFINED_ERROR_MESSAGES(V)
#undef V

// Errors with predefined non-static messages
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
                                               int64_t timeout) {
  std::ostringstream message;
  message << "Script execution timed out after ";
  message << timeout << "ms";
  THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
}

inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
  char message[128];
  snprintf(message, sizeof(message),
      "Cannot create a Buffer larger than 0x%zx bytes",
      v8::TypedArray::kMaxLength);
  return ERR_BUFFER_TOO_LARGE(isolate, message);
}

inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
  char message[128];
  snprintf(message, sizeof(message),
      "Cannot create a string longer than 0x%x characters",
      v8::String::kMaxLength);
  return ERR_STRING_TOO_LONG(isolate, message);
}

#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix)                     \
  do {                                                                       \
    if (!Buffer::HasInstance(val))                                           \
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
                                              prefix " must be a buffer");   \
  } while (0)

#define THROW_AND_RETURN_IF_NOT_STRING(env, val, prefix)                     \
  do {                                                                       \
    if (!val->IsString())                                                    \
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
                                              prefix " must be a string");   \
  } while (0)

namespace errors {

class TryCatchScope : public v8::TryCatch {
 public:
  enum class CatchMode { kNormal, kFatal };

  explicit TryCatchScope(Environment* env, CatchMode mode = CatchMode::kNormal)
      : v8::TryCatch(env->isolate()), env_(env), mode_(mode) {}
  ~TryCatchScope();

  // Since the dtor is not virtual we need to make sure no one creates
  // object of it in the free store that might be held by polymorphic pointers.
  void* operator new(std::size_t count) = delete;
  void* operator new[](std::size_t count) = delete;
  TryCatchScope(TryCatchScope&) = delete;
  TryCatchScope(TryCatchScope&&) = delete;
  TryCatchScope operator=(TryCatchScope&) = delete;
  TryCatchScope operator=(TryCatchScope&&) = delete;

 private:
  Environment* env_;
  CatchMode mode_;
};

const char* errno_string(int errorno);

}  // namespace errors

void DecorateErrorStack(Environment* env,
                        const errors::TryCatchScope& try_catch);
}  // namespace node

#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif  // SRC_NODE_ERRORS_H_