mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
12a102b926
Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
145 lines
4.0 KiB
C++
145 lines
4.0 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#if __has_include(<React-Codegen/AppSpecsJSI.h>) // CocoaPod headers on Apple
|
|
#include <React-Codegen/AppSpecsJSI.h>
|
|
#elif __has_include("AppSpecsJSI.h") // Cmake headers on Android
|
|
#include "AppSpecsJSI.h"
|
|
#else // BUCK headers
|
|
#include <AppSpecs/AppSpecsJSI.h>
|
|
#endif
|
|
#include <memory>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace facebook::react {
|
|
|
|
#pragma mark - Structs
|
|
using ConstantsStruct =
|
|
NativeCxxModuleExampleCxxBaseConstantsStruct<bool, int32_t, std::string>;
|
|
|
|
template <>
|
|
struct Bridging<ConstantsStruct>
|
|
: NativeCxxModuleExampleCxxBaseConstantsStructBridging<
|
|
bool,
|
|
int32_t,
|
|
std::string> {};
|
|
|
|
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
|
|
int32_t,
|
|
std::string,
|
|
std::optional<std::string>>;
|
|
|
|
template <>
|
|
struct Bridging<ObjectStruct>
|
|
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
|
|
int32_t,
|
|
std::string,
|
|
std::optional<std::string>> {};
|
|
|
|
using ValueStruct =
|
|
NativeCxxModuleExampleCxxBaseValueStruct<double, std::string, ObjectStruct>;
|
|
|
|
template <>
|
|
struct Bridging<ValueStruct> : NativeCxxModuleExampleCxxBaseValueStructBridging<
|
|
double,
|
|
std::string,
|
|
ObjectStruct> {};
|
|
|
|
#pragma mark - enums
|
|
enum CustomEnumInt { A = 23, B = 42 };
|
|
|
|
template <>
|
|
struct Bridging<CustomEnumInt> {
|
|
static CustomEnumInt fromJs(jsi::Runtime &rt, int32_t value) {
|
|
if (value == 23) {
|
|
return CustomEnumInt::A;
|
|
} else if (value == 42) {
|
|
return CustomEnumInt::B;
|
|
} else {
|
|
throw jsi::JSError(rt, "Invalid enum value");
|
|
}
|
|
}
|
|
|
|
static jsi::Value toJs(jsi::Runtime &rt, CustomEnumInt value) {
|
|
return bridging::toJs(rt, static_cast<int32_t>(value));
|
|
}
|
|
};
|
|
|
|
#pragma mark - implementation
|
|
class NativeCxxModuleExample
|
|
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
|
|
public:
|
|
NativeCxxModuleExample(std::shared_ptr<CallInvoker> jsInvoker);
|
|
|
|
void getValueWithCallback(
|
|
jsi::Runtime &rt,
|
|
AsyncCallback<std::string> callback);
|
|
|
|
std::vector<std::optional<ObjectStruct>> getArray(
|
|
jsi::Runtime &rt,
|
|
std::vector<std::optional<ObjectStruct>> arg);
|
|
|
|
bool getBool(jsi::Runtime &rt, bool arg);
|
|
|
|
ConstantsStruct getConstants(jsi::Runtime &rt);
|
|
|
|
CustomEnumInt getCustomEnum(jsi::Runtime &rt, CustomEnumInt arg);
|
|
|
|
NativeCxxModuleExampleCxxEnumFloat getNumEnum(
|
|
jsi::Runtime &rt,
|
|
NativeCxxModuleExampleCxxEnumInt arg);
|
|
|
|
NativeCxxModuleExampleCxxEnumStr getStrEnum(
|
|
jsi::Runtime &rt,
|
|
NativeCxxModuleExampleCxxEnumNone arg);
|
|
|
|
std::map<std::string, std::optional<int32_t>> getMap(
|
|
jsi::Runtime &rt,
|
|
std::map<std::string, std::optional<int32_t>> arg);
|
|
|
|
double getNumber(jsi::Runtime &rt, double arg);
|
|
|
|
ObjectStruct getObject(jsi::Runtime &rt, ObjectStruct arg);
|
|
|
|
std::set<float> getSet(jsi::Runtime &rt, std::set<float> arg);
|
|
|
|
std::string getString(jsi::Runtime &rt, std::string arg);
|
|
|
|
std::string getUnion(jsi::Runtime &rt, float x, std::string y, jsi::Object z);
|
|
|
|
ValueStruct
|
|
getValue(jsi::Runtime &rt, double x, std::string y, ObjectStruct z);
|
|
|
|
AsyncPromise<std::string> getValueWithPromise(jsi::Runtime &rt, bool error);
|
|
|
|
std::optional<bool> getWithWithOptionalArgs(
|
|
jsi::Runtime &rt,
|
|
std::optional<bool> optionalArg);
|
|
|
|
void voidFunc(jsi::Runtime &rt);
|
|
|
|
void emitCustomDeviceEvent(jsi::Runtime &rt, jsi::String eventName);
|
|
|
|
void voidFuncThrows(jsi::Runtime &rt);
|
|
|
|
ObjectStruct getObjectThrows(jsi::Runtime &rt, ObjectStruct arg);
|
|
|
|
AsyncPromise<jsi::Value> promiseThrows(jsi::Runtime &rt);
|
|
|
|
void voidFuncAssert(jsi::Runtime &rt);
|
|
|
|
ObjectStruct getObjectAssert(jsi::Runtime &rt, ObjectStruct arg);
|
|
|
|
AsyncPromise<jsi::Value> promiseAssert(jsi::Runtime &rt);
|
|
};
|
|
|
|
} // namespace facebook::react
|