Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TC39 proposal - Error.cause property #45167

Closed
sirian opened this issue Jul 23, 2021 · 10 comments · Fixed by #49639
Closed

TC39 proposal - Error.cause property #45167

sirian opened this issue Jul 23, 2021 · 10 comments · Fixed by #49639
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript ES Next New featurers for ECMAScript (a.k.a. ESNext) Help Wanted You can do this

Comments

@sirian
Copy link
Contributor

sirian commented Jul 23, 2021

lib.es2021d.ts Update Request - Error.cause property

Proposal (Stage 3)
https://github.com/tc39/proposal-error-cause

Already implemented in Chrome 93:
https://www.chromestatus.com/feature/5727099325251584

Sample Code

interface Error {
    cause?: any;
}

interface ErrorInit {
    cause?: any;
}

interface ErrorConstructor {
    new(message?: string, init?: ErrorInit): Error;
    (message?: string, init?: ErrorInit): Error;
}
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Jul 23, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jul 23, 2021
@DanielRosenwasser DanielRosenwasser added the ES Next New featurers for ECMAScript (a.k.a. ESNext) label Jul 26, 2021
@johanneswuerbach
Copy link

Error.cause is also available in Node 16.9.0 now 🎉

@majg0
Copy link

majg0 commented Oct 27, 2021

Bump

@Alexsey
Copy link

Alexsey commented Nov 4, 2021

Already Stage 4 - the feature is finalized!

@sandersn
Copy link
Member

sandersn commented Nov 5, 2021

Fixed by #46291 (and #45749, but I missed reviewing that one.)

@amakhrov
Copy link

amakhrov commented Dec 14, 2021

This seems to be partially implemented.

The constructor now accepts ErrorOptions with the cause property - which is great.
However, this property is not declared in the Error object itself.

With the full implementation this example should compile:

const error = new Error("derived", {cause: new Error("original")}); // OK
console.log(error.cause); // Property 'cause' doesn't exist

@sandersn

@sandersn sandersn reopened this Dec 16, 2021
@sandersn
Copy link
Member

Thanks for pointing that out. I re-opened the bug.

@GoudekettingRM
Copy link

Bump. Also ran into this. Fixed it local by redeclaring like this:

interface Error {
  name: string;
  message: string;
  cause?: any;
  stack?: string;
}

interface ErrorConstructor {
  new(message?: string, { cause }?: { cause: any }): Error;
  (message?: string): Error;
  readonly prototype: Error;
}

declare var Error: ErrorConstructor

@mcaskill
Copy link

mcaskill commented Feb 9, 2022

I also had this issue for cause and stack. Fixed it by redeclaring like this:

interface ErrorInit {
    cause?: unknown;
}

declare type BaseError = Error;

declare class Error implements BaseError {
    name: string;
    message: string;
    stack?: string;
    cause?: unknown;

    constructor(message?: string, init?: ErrorInit);

    // eslint-disable-next-line @typescript-eslint/ban-types -- Allow catch-all 'function' parameter type.
    static captureStackTrace(error: object, constructor?: Function): void;
}

@yume-chan
Copy link
Contributor

cause was added in #47020 and all definitions had shipped with 4.6.

@MartinJohns
Copy link
Contributor

@yume-chan Although the type is inaccurate, as per #48098.

jstasiak added a commit to lune-climate/ts-results-es that referenced this issue Mar 29, 2022
We need TypeScript 4.6+ to be able to construct Error objects with the
cause property[1]. The property is useful to chain errors and, on the
catching side, to see what error caused the error we just caught.

I want to have this in place to implement [3] which satisfies a need we
ourselves have[4].

tslib upgraded in lockstep because otherwise we get

    FAIL  test/result.test.ts
      ● Test suite failed to run

        test/result.test.ts:121:29 - error TS2807: This syntax requires an imported helper named '__spreadArray' with 3 parameters, which is not compatible with the one in 'tslib'. Consider upgrading your version of 'tslib'.

        121     const all4 = Result.all(...([] as Result<string, number>[]));
                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error
[2] microsoft/TypeScript#45167
[3] vultix#34
[4] vultix#48
jstasiak added a commit to lune-climate/ts-results-es that referenced this issue Mar 29, 2022
We need TypeScript 4.6+ to be able to construct Error objects with the
cause property[1]. The property is useful to chain errors and, on the
catching side, to see what error caused the error we just caught.

I want to have this in place to implement [3] which satisfies a need we
ourselves have[4].

tslib upgraded in lockstep because otherwise we get

    FAIL  test/result.test.ts
      ● Test suite failed to run

        test/result.test.ts:121:29 - error TS2807: This syntax requires an imported helper named '__spreadArray' with 3 parameters, which is not compatible with the one in 'tslib'. Consider upgrading your version of 'tslib'.

        121     const all4 = Result.all(...([] as Result<string, number>[]));
                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error
[2] microsoft/TypeScript#45167
[3] vultix#34
[4] vultix#48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript ES Next New featurers for ECMAScript (a.k.a. ESNext) Help Wanted You can do this
Projects
None yet