All Projects → tc39 → Proposal Error Cause

tc39 / Proposal Error Cause

Licence: mit
TC39 proposal for accumulating errors

Programming Languages

proposal
26 projects

Labels

Projects that are alternatives of or similar to Proposal Error Cause

proposal-get-intrinsic
EcmaScript language proposal for a way to get intrinsics.
Stars: ✭ 19 (-87.82%)
Mutual labels:  tc39
Seafox
A blazing fast 100% spec compliant, self-hosted javascript parser written in Typescript
Stars: ✭ 425 (+172.44%)
Mutual labels:  tc39
Proposal Record Tuple
ECMAScript proposal for the Record and Tuple value types. | Stage 2: it will change!
Stars: ✭ 1,394 (+793.59%)
Mutual labels:  tc39
proposal-symbol-thenable
gus.host/proposal-symbol-thenable
Stars: ✭ 18 (-88.46%)
Mutual labels:  tc39
Proposal Iterator Helpers
Methods for working with iterators in ECMAScript
Stars: ✭ 313 (+100.64%)
Mutual labels:  tc39
Meriyah
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
Stars: ✭ 690 (+342.31%)
Mutual labels:  tc39
Javascript
JavaScript Style Guide
Stars: ✭ 117,286 (+75083.33%)
Mutual labels:  tc39
Test262
Official ECMAScript Conformance Test Suite
Stars: ✭ 1,770 (+1034.62%)
Mutual labels:  tc39
Proposals
✍️ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
Stars: ✭ 401 (+157.05%)
Mutual labels:  tc39
Buntis
A 100% compliant, self-hosted typescript parser that emits an ESTree-compatible AST
Stars: ✭ 90 (-42.31%)
Mutual labels:  tc39
legacy-decorators-migration-utility
🔝 A command line utility to upgrade your JavaScript files from the legacy decorators proposal to the new one.
Stars: ✭ 20 (-87.18%)
Mutual labels:  tc39
dataset
The automatic track tc39 proposals
Stars: ✭ 22 (-85.9%)
Mutual labels:  tc39
Bigint Buffer
💪🔢 bigint-buffer: Buffer Utilities for TC39 BigInt Proposal
Stars: ✭ 26 (-83.33%)
Mutual labels:  tc39
proposal-function-helpers
A withdrawn proposal for standardizing some useful, popular helper functions into JavaScript’s Function object.
Stars: ✭ 41 (-73.72%)
Mutual labels:  tc39
Js Class Fields Chinese Discussion
JavaScript的『class fields』提案中文讨论
Stars: ✭ 117 (-25%)
Mutual labels:  tc39
talk.js
🎙 A monthly meet up for all things JavaScript, Node.js, and the modern web
Stars: ✭ 75 (-51.92%)
Mutual labels:  tc39
Proposal Pipeline Operator
A proposal for adding a useful pipe operator to JavaScript.
Stars: ✭ 5,899 (+3681.41%)
Mutual labels:  tc39
Javascript Style Guide
Airbnb JavaScript 스타일 가이드
Stars: ✭ 132 (-15.38%)
Mutual labels:  tc39
Cherow
Very fast and lightweight, standards-compliant, self-hosted javascript parser with high focus on both performance and stability
Stars: ✭ 1,539 (+886.54%)
Mutual labels:  tc39
Tc39
360 Ecma-TC39 工作组
Stars: ✭ 75 (-51.92%)
Mutual labels:  tc39

Error Cause

Status: Stage 3

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 throwing it with additional 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 caught error with contextual message. 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 options parameter to the Error() constructor with a cause property, the value of which 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', { cause: err });
    });
  const jobResult = doComputationalHeavyJob(rawResource);
  await fetch('//domain/upload', { method: 'POST', body: jobResult })
    .catch(err => {
      throw new Error('Upload job result failed', { cause: 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. Since the second parameter under this proposal must be an object with a cause property, it will be distinguishable from a string.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].