[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/joyeecheung/node/cpp-bash/src/node_errors.h [Back]  [Original]

#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 

namespace node {

using v8::Local;
using v8::Message;
using v8::Value;

enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR };
void AppendExceptionLine(Environment* env,
                         Local er,
                         Local 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 ReportException(Environment* env,
                     Local er,
                     Local message);

void FatalException(v8::Isolate* isolate,
                    Local error,
                    Local message);

void FatalException(v8::Isolate* isolate,
                    Local error,
                    Local message,
                    bool from_promise);

// 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` 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_CONSTRUCT_CALL_REQUIRED, Error)                                      \
  V(ERR_INVALID_ARG_VALUE, TypeError)                                        \
  V(ERR_INVALID_ARG_TYPE, TypeError)                                         \
  V(ERR_INVALID_MODULE_SPECIFIER, TypeError)                                 \
  V(ERR_INVALID_PACKAGE_CONFIG, SyntaxError)                                 \
  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_PASSPHRASE, TypeError)                                       \
  V(ERR_MISSING_PLATFORM_FOR_WORKER, Error)                                  \
  V(ERR_MODULE_NOT_FOUND, 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 code(v8::Isolate* isolate,                      \
                                   const char* message)       {               \
    v8::Local js_code = OneByteString(isolate, #code);            \
    v8::Local js_msg = OneByteString(isolate, message);           \
    v8::Local e =                                                 \
        v8::Exception::type(js_msg)->ToObject(                                \
            isolate->GetCurrentContext()).ToLocalChecked();                   \
    e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"),      \
           js_code).Check();                                                  \
    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_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 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 

Web Proxy Viewer  |  New URL  |  Original Page