https://github.com/legendecas https://github.com/tc39/proposal-error-cause

    1. {
    2. async function doJob() {
    3. const rawResource = await fetch('//domain/resource-a')
    4. .catch(err => {
    5. throw new Error('Download raw resource failed', { cause: err })
    6. })
    7. const jobResult = doComputationalHeavyJob(rawResource)
    8. await fetch('//domain/upload', { method: 'POST', body: jobResult })
    9. .catch(err => {
    10. throw new Error('Upload job result failed', { cause: err })
    11. })
    12. }
    13. try {
    14. await doJob()
    15. } catch (e) {
    16. const error = e as unknown as Error
    17. console.log(error) // Error: Download raw resource failed
    18. console.log('Caused by', error.cause) // Caused by TypeError: Failed to fetch
    19. }
    20. }
    21. export {}