Coder Social home page Coder Social logo

exe-boss / tc39-proposal-error-cause Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tc39/proposal-error-cause

0.0 1.0 0.0 209 KB

TC39 proposal for accumulating errors

Home Page: https://tc39.es/proposal-error-cause/

License: MIT License

HTML 97.98% JavaScript 2.02%

tc39-proposal-error-cause's Introduction

Error Cause

Status: Stage 2

Author: @legendecas

Champion: @legendecas, @hemanth

Chaining Errors

Errors will be constructed to represent runtime abnormalities. To help unexpected behavior diagnosis, errors need to be augmented with contextual information like error messages, error instance properties to explain what happened at the time.

If the error were thrown from deep internal methods, the thrown error may not be straightforward to be easily conducted without proper exception design pattern. Catching an error and re-thrown it with specialized contextual data is a common approach of error handling pattern. Multiple methods are available to augment the caught error with additional contextual information:

async function getSolution() {
  const rawResource = await fetch('//domain/resource-a')
    .catch(err => {
      // How to wrap the error properly?
      // 1. throw new Error('Download raw resource failed: ' + err.message);
      // 2. const wrapErr = new Error('Download raw resource failed');
      //    wrapErr.cause = err;
      //    throw wrapErr;
      // 3. class CustomError {
      //      constructor(msg, cause) {
      //        super(msg);
      //        this.cause = cause;
      //      }
      //    }
      //    throw new CustomError('Download raw resource failed', err);
    })
  const jobResult = doComputationalHeavyJob(rawResource);
  await fetch('//domain/upload', { method: 'POST', body: jobResult });
}

await doJob(); // => TypeError: Failed to fetch

If the errors were chained with causes, it can be greatly helpful to diagnosing unexpected exceptions. As the example above shows, quite a few laboring works has to be done for a simple error handling case to augmenting the re-thrown error with the cause. Besides, no consensus on which property will representing the cause makes it not feasible for developer tools to revealing contextual information of causes.

The proposed solution is adding an additional parameter cause to the Error() constructor, and the value of the parameter will be assigned to the error instances as a property. So errors can be chained without unnecessary and overelaborate formalities on wrapping the errors in conditions.

async function doJob() {
  const rawResource = await fetch('//domain/resource-a')
    .catch(err => {
      throw new Error('Download raw resource failed', err);
    });
  const jobResult = doComputationalHeavyJob(rawResource);
  await fetch('//domain/upload', { method: 'POST', body: jobResult })
    .catch(err => {
      throw new Error('Upload job result failed', err);
    });
}

try {
  await doJob();
} catch (e) {
  console.log(e);
  console.log('Caused by', e.cause);
}
// Error: Upload job result failed
// Caused by TypeError: Failed to fetch

Compatibilities

In Firefox, Error() constructor can receive two optional additional positional parameters: fileName, lineNumber. Those parameters will be assigned to newly constructed error instances with the name fileName and lineNumber respectively.

However, no standard on either ECMAScript or Web were defined on such behavior.

FAQs

Differences with AggregateError

The key difference between those two is that the errors in the AggregateError doesn't have to be related. AggregateError are just a bunch of errors just happened to be caught and aggregated in one place, they can be totally unrelated. E.g. jobA and jobB can do nothing to each other in Promise.allSettled(jobA, jobB). However, if an error were thrown from several levels deep of jobA, the cause property can accumulate context information of those levels to help understanding what happened exactly.

With AggregateError, we get breadth. With the cause property, we get depth.

Why not custom subclasses of Error

While there are lots of ways to achieve the behavior of the proposal, if the cause property is explicitly defined by the language, debug tooling can reliably use this info rather than contracting with developers to construct an error properly.

tc39-proposal-error-cause's People

Contributors

devsnek avatar hemanth avatar legendecas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.