Skip to content

Commit

Permalink
Refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwuzw committed Jan 9, 2024
1 parent 8dc0647 commit 2dcf769
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s
return runtime.global().getPropertyAsFunction(runtime, "Error").call(runtime, message);
}

static jsi::Value createJSRuntimeError(jsi::Runtime &runtime, const std::string &message, jsi::Object &options)
{
return runtime.global().getPropertyAsFunction(runtime, "Error").call(runtime, message, options);
}

/**
* Creates JSError with current JS runtime and NSException stack trace.
*/
Expand All @@ -211,6 +216,24 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s
return {runtime, std::move(error)};
}

/**
* Creates JS error value with current JS runtime and error details.
*/
static jsi::Value convertJSErrorDetailsToJSValue(jsi::Runtime &runtime, NSDictionary *jsErrorDetails)
{
// From JS documentation:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#cause an
// Error can be created with `new Error(message, option);`. the `option` param is a JS object with the
// `cause` property. Create a valid `option` object
jsi::Object jsErrorOptions = jsi::Object(runtime);
jsErrorOptions.setProperty(runtime, "cause", convertObjCObjectToJSIValue(runtime, jsErrorDetails));
NSString *message = jsErrorDetails[@"message"];

auto jsError = createJSRuntimeError(runtime, [message UTF8String], jsErrorOptions);

return jsError;
}

}

jsi::Value ObjCTurboModule::createPromise(jsi::Runtime &runtime, std::string methodName, PromiseInvocationBlock invoke)
Expand Down Expand Up @@ -281,16 +304,7 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s

NSDictionary *jsErrorDetails = RCTJSErrorFromCodeMessageAndNSError(code, message, error);
reject->call([jsErrorDetails](jsi::Runtime &rt, jsi::Function &jsFunction) {
// From JS documentation:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#cause an
// Error can be created with `new Error(message, option);`. the `option` param is a JS object with the
// `cause` property. Create a valid `option` object
NSDictionary<NSString *, id> *jsErrorOptions = @{@"cause" : jsErrorDetails};
auto jsiObjCError = convertObjCObjectToJSIValue(rt, jsErrorOptions);
NSString *message =
jsErrorDetails[@"message"] ? jsErrorDetails[@"message"] : @"Unknown error from a native module";
auto jsError =
rt.global().getPropertyAsFunction(rt, "Error").call(rt, [message UTF8String], jsiObjCError);
auto jsError = convertJSErrorDetailsToJSValue(rt, jsErrorDetails);
jsFunction.call(rt, jsError);
});
resolveWasCalled = NO;
Expand Down

0 comments on commit 2dcf769

Please sign in to comment.