Tengo un componente funcional donde llamo a un método desde otro archivo .js. El método dentro del archivo .js arroja un error (que está bien, para mis propósitos de prueba), pero quiero que este error llegue al método dentro del componente funcional. ¿Cómo puedo hacer eso?
import { pickImageAsync, } from "./someFile"; export default class Component1 extends React.Component<any> { render() { const { sendError } = this.props; function pickImage() { try { //calling from another file pickImageAsync(onSend); } catch (error) { //I dont reach this part of the file console.log("I am catching error: ", error); sendImageError(true); } } En mi archivo someFile.js :
export async function pickImageAsync(onSend) { if (await getImagePickerPermissionAsync()) { let result; try { //with the way I test it this method throws the error, which I want to bubble up result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images, quality: 1, }); //with the way I test it, I dont see any result, which is OK for my testing purposes console.log(result); // I dont reach this peace of code which is fine if (!result.cancelled) { onSend([{ image: result.uri }]); } } catch (error) { //I am catching the error and I see it on the console, but how do I bubble it up? console.log("could not select image: ", error); } } }Debe throw este error ( throw error ). Esto hará que aparezca en la pila de llamadas hasta que alguien lo atrape (o no, y verá un error en la consola).
Mira la última línea que agregué:
export async function pickImageAsync(onSend) { if (await getImagePickerPermissionAsync()) { let result; try { //with the way I test it this method throws the error, which I want to bubble up result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images, quality: 1, }); //with the way I test it, I dont see any result, which is OK for my testing purposes console.log(result); // I dont reach this peace of code which is fine if (!result.cancelled) { onSend([{ image: result.uri }]); } } catch (error) { //I am catching the error and I see it on the console, but how do I bubble it up? console.log("could not select image: ", error); throw error; } } }