Exemplo n.º 1
0
Value Value::makeError(JSContextRef ctx, const char *error)
{
  JSValueRef exn;
  JSValueRef args[] = { Value(ctx, String(ctx, error)) };
  JSObjectRef errorObj = JSC_JSObjectMakeError(ctx, 1, args, &exn);
  if (!errorObj) {
    throw JSException(ctx, exn, "Exception making error");
  }
  return Value(ctx, errorObj);
}
Exemplo n.º 2
0
Value Value::makeError(JSContextRef ctx, const char *error, const char *stack)
{
  auto errorMsg = Value(ctx, String(ctx, error));
  JSValueRef args[] = {errorMsg};
  if (stack) {
    // Using this instead of JSObjectMakeError to actually get a stack property.
    // MakeError only sets it stack when returning from the invoked function, so we
    // can't extend it here.
    auto errorConstructor = Object::getGlobalObject(ctx).getProperty("Error").asObject();
    auto jsError = errorConstructor.callAsConstructor({errorMsg});
    auto fullStack = std::string(stack) + jsError.getProperty("stack").toString().str();
    jsError.setProperty("stack", String(ctx, fullStack.c_str()));
    return jsError;
  } else {
    JSValueRef exn;
    JSObjectRef errorObj = JSC_JSObjectMakeError(ctx, 1, args, &exn);
    if (!errorObj) {
      throw JSException(ctx, exn, "Exception making error");
    }
    return Value(ctx, errorObj);
  }
}