Estoy atascado con el siguiente problema:
function upperFn(){ FetchSomeThing() .catch(err => { console.log(err) }) } function lowerFn(){ try { upperFn() } catch (err){ //Here its not working anymore console.log(err) // Catch is never used } }Así que intenté devolver el error e incluso volver a lanzarlo, pero nada funcionó. Sería muy feliz si alguien me puede explicar cómo detectar este error en mi función inferior.
Gracias
Solo descubrí cómo manejar este problema, simplemente devolviendo la función completa.
function upperFn(){ return FetchSomeThing() } function lowerFn(){ try { upperFn() } catch (err){ //Here its working well console.log(err) } }Simplemente throw el error en la upperFunction e intenta atraparlo en la lowerFunction
function FetchSomeThing() { return Promise.reject("Something goes wrong."); } function upperFn(){ FetchSomeThing().catch(error => { throw error; }) } function lowerFn(){ try { upperFn() } catch (error) { //Here its not working anymore console.log(error) // Catch is never used } } lowerFn()Ejemplo de trabajo: https://codesandbox.io/s/agitated-thunder-3s9qj
Producción:
¿Tal vez es porque te perdiste el parámetro de error después de la captura?
try { doSomeThing(); } catch (error) { console.error(error); }