Files
react-native/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js
T
Krystof Woldrich 12a102b926 Add error reporting examples to rn-tester turbo modules (#36729)
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
2023-04-03 08:34:59 -07:00

250 lines
6.9 KiB
JavaScript

/**
* 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.
*
* @format
* @flow strict-local
*/
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
import {
DeviceEventEmitter,
Text,
View,
FlatList,
TouchableOpacity,
RootTagContext,
} from 'react-native';
import * as React from 'react';
import NativeCxxModuleExample, {
EnumInt,
EnumNone,
} from '../../../NativeCxxModuleExample/NativeCxxModuleExample';
import styles from './TurboModuleExampleCommon';
type State = {|
testResults: {
[string]: {
type: string,
value: mixed,
...
},
...
},
|};
type Examples =
| 'callback'
| 'getArray'
| 'getBool'
| 'getConstants'
| 'getCustomEnum'
| 'getNumEnum'
| 'getStrEnum'
| 'getMap'
| 'getNumber'
| 'getObject'
| 'getSet'
| 'getString'
| 'getUnion'
| 'getValue'
| 'promise'
| 'rejectPromise'
| 'voidFunc'
| 'optionalArgs'
| 'emitDeviceEvent'
| 'voidFuncThrows'
| 'getObjectThrows'
| 'promiseThrows'
| 'voidFuncAssert'
| 'getObjectAssert'
| 'promiseAssert';
class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
static contextType: React$Context<RootTag> = RootTagContext;
state: State = {
testResults: {},
};
// Add calls to methods in TurboModule here
// $FlowFixMe[missing-local-annot]
_tests = {
callback: () =>
NativeCxxModuleExample?.getValueWithCallback(callbackValue =>
this._setResult('callback', callbackValue),
),
getArray: () =>
NativeCxxModuleExample?.getArray([
{a: 1, b: 'foo'},
{a: 2, b: 'bar'},
null,
]),
getBool: () => NativeCxxModuleExample?.getBool(true),
getConstants: () => NativeCxxModuleExample?.getConstants(),
getCustomEnum: () => NativeCxxModuleExample?.getCustomEnum(EnumInt.IB),
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
getObject: () =>
NativeCxxModuleExample?.getObject({a: 1, b: 'foo', c: null}),
getSet: () => NativeCxxModuleExample?.getSet([1, 1.1, 1.1, 1.1, 2]),
getString: () => NativeCxxModuleExample?.getString('Hello'),
getUnion: () => NativeCxxModuleExample?.getUnion(1.44, 'Two', {low: '12'}),
getValue: () =>
NativeCxxModuleExample?.getValue(5, 'test', {a: 1, b: 'foo'}),
promise: () =>
NativeCxxModuleExample?.getValueWithPromise(false).then(valuePromise =>
this._setResult('promise', valuePromise),
),
rejectPromise: () =>
NativeCxxModuleExample?.getValueWithPromise(true)
.then(() => {})
.catch(e => this._setResult('rejectPromise', e.message)),
voidFunc: () => NativeCxxModuleExample?.voidFunc(),
optionalArgs: () => NativeCxxModuleExample?.getWithWithOptionalArgs(),
emitDeviceEvent: () => {
const CUSTOM_EVENT_TYPE = 'myCustomDeviceEvent';
DeviceEventEmitter.removeAllListeners(CUSTOM_EVENT_TYPE);
DeviceEventEmitter.addListener(CUSTOM_EVENT_TYPE, (...args) => {
this._setResult(
'emitDeviceEvent',
`${CUSTOM_EVENT_TYPE}(${args.map(s => `${s}`).join(', ')})`,
);
});
NativeCxxModuleExample?.emitCustomDeviceEvent(CUSTOM_EVENT_TYPE);
},
voidFuncThrows: () => {
try {
NativeCxxModuleExample?.voidFuncThrows();
} catch (e) {
return e.message;
}
},
getObjectThrows: () => {
try {
NativeCxxModuleExample?.getObjectThrows({a: 1, b: 'foo', c: null});
} catch (e) {
return e.message;
}
},
promiseThrows: () => {
try {
// $FlowFixMe[unused-promise]
NativeCxxModuleExample?.promiseThrows();
} catch (e) {
return e.message;
}
},
voidFuncAssert: () => {
try {
NativeCxxModuleExample?.voidFuncAssert();
} catch (e) {
return e.message;
}
},
getObjectAssert: () => {
try {
NativeCxxModuleExample?.getObjectAssert({a: 1, b: 'foo', c: null});
} catch (e) {
return e.message;
}
},
promiseAssert: () => {
try {
// $FlowFixMe[unused-promise]
NativeCxxModuleExample?.promiseAssert();
} catch (e) {
return e.message;
}
},
};
_setResult(
name: string | Examples,
result:
| $FlowFixMe
| void
| Array<$FlowFixMe>
| boolean
| {const1: boolean, const2: number, const3: string}
| number
| {[key: string]: ?number}
| Promise<mixed>
| number
| string,
) {
this.setState(({testResults}) => ({
testResults: {
...testResults,
/* $FlowFixMe[invalid-computed-prop] (>=0.111.0 site=react_native_fb)
* This comment suppresses an error found when Flow v0.111 was
* deployed. To see the error, delete this comment and run Flow. */
[name]: {value: result, type: typeof result},
},
}));
}
_renderResult(name: Examples): React.Node {
const result = this.state.testResults[name] || {};
return (
<View style={styles.result}>
<Text style={[styles.value]}>{JSON.stringify(result.value)}</Text>
<Text style={[styles.type]}>{result.type}</Text>
</View>
);
}
componentDidMount(): void {
if (global.__turboModuleProxy == null) {
throw new Error(
'Cannot load this example because TurboModule is not configured.',
);
}
}
render(): React.Node {
return (
<View style={styles.container}>
<View style={styles.item}>
<TouchableOpacity
style={[styles.column, styles.button]}
onPress={() =>
Object.keys(this._tests).forEach(item =>
this._setResult(item, this._tests[item]()),
)
}>
<Text style={styles.buttonTextLarge}>Run all tests</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.setState({testResults: {}})}
style={[styles.column, styles.button]}>
<Text style={styles.buttonTextLarge}>Clear results</Text>
</TouchableOpacity>
</View>
<FlatList
// $FlowFixMe[incompatible-type]
data={Object.keys(this._tests)}
keyExtractor={item => item}
renderItem={({item}: {item: Examples, ...}) => (
<View style={styles.item}>
<TouchableOpacity
style={[styles.column, styles.button]}
onPress={e => this._setResult(item, this._tests[item]())}>
<Text style={styles.buttonText}>{item}</Text>
</TouchableOpacity>
<View style={[styles.column]}>{this._renderResult(item)}</View>
</View>
)}
/>
</View>
);
}
}
module.exports = NativeCxxModuleExampleExample;