Property 'code' does not exist on type 'Error'
Asked Answered
R

7

33

How can I access the Error.code property? I get a Typescript error because the property 'code' does not exist on type 'Error'.

this.authCtrl.login(user, {
   provider: AuthProviders.Password,
   method: AuthMethods.Password
}).then((authData) => {
    //Success
}).catch((error) => {
   console.log(error); // I see a code property
   console.log(error.code); //error
})
Reprovable answered 19/10, 2016 at 20:31 Comment(0)
D
59

The real issue is that the Node.js definition file isn't exporting a proper Error definition. It uses the following for Error (and doesn't export this):

interface Error {
    stack?: string;
}

The actual definition it exports is in the NodeJS namespace:

export interface ErrnoException extends Error {
    errno?: number;
    code?: string;
    path?: string;
    syscall?: string;
    stack?: string;
}

So the following typecast will work:

.catch((error: NodeJS.ErrnoException) => {
    console.log(error);
    console.log(error.code);
})

This seems like a flaw in Node's definition, since it doesn't line up with what an object from new Error() actually contains. TypeScript will enforce the interface Error definition.

Dutybound answered 29/3, 2018 at 18:2 Comment(2)
Not a full solution. A full solution would also show the import statement that needs to be added in order to "have" the NodeJS symbol.Chaffee
@SzczepanHołyszewski This answer worked great for me, no additional import needed.Consequent
A
5

You have to cast a type to the error param from catch i.e.

.catch((error:any) => {
    console.log(error);
    console.log(error.code);
});

or you can access the code property directly in this manner

.catch((error) => {
    console.log(error);
    console.log(error['code']);
});
Autolysis answered 19/10, 2016 at 21:57 Comment(0)
T
4

As explained by @Brent in https://stackoverflow.com/a/49562477 this problem is caused by an incomplete type definition in the Typescript package (see https://github.com/microsoft/TypeScript/blob/ed6889cd5b61b9fa5156018362d867def18e281d/lib/lib.es5.d.ts#L1039-L1043). People using ECMAScript 2015, or later, can also use module declarations to declare the missing properties. This can be done by placing the following code inside the @types/global.d.ts file.

declare interface Error {
  name: string
  message: string
  stack?: string
  code?: number | string
}
Tetramethyldiarsine answered 29/8, 2022 at 15:12 Comment(0)
J
0

I get same encountering as typescript

"Property 'code' does not exist on type 'Error'.ts(2339)," it suggests that TypeScript doesn't recognize code as a property of the standard Error object.

To handle this, i was extend the Error type with a custom interface that includes the code property.

enter image description here

Jessjessa answered 16/4 at 2:53 Comment(0)
L
0

The answer posted by rickstaa works, but it is not 100% safe. The code property could be set to another type by a third party.

I would recommend to set in the declaration file (ie: @types/global.d.ts) the following:

declare interface Error {
    code?: unknown;
}

It is enough to access code, check its value, and will rightfully require you to check its actual type before using it where a string or a number is needed.

No need to redeclare name, message and stack, the declaration will merge with the one provided by TypeScript.

About the answer posted by Brent, I would strongly advise against casting the type like this:

.catch((reason: NodeJS.ErrnoException) => {
    ...
});

I think you shouldn't assume you know all reasons a Promise could be rejected. In my opinion, you should always assume it is unknown and check its type. There's a reason TypeScript types it a an any. After declaring the code property like demonstrated above, I would write this:

.catch((reason) => {
    if (reason instanceof Error) {
        // Here we can access reason.code
    }
});
Lulita answered 13/6 at 0:3 Comment(0)
W
0

This is how in Next.js auth I got around the problem.

} catch (error) {
if (error instanceof AuthError) {
  const err = error.cause?.err as any
  switch (err.code) {
    case 'credentials':
      return 'Invalid credentials.'
    default:
      return 'Something went wrong.'
  }
}
throw error

}

Weidner answered 5/7 at 10:20 Comment(0)
U
-2
export default class ResponseError extends Error {
    code: number;
    message: string;
    response: {
        headers: { [key: string]: string; };
        body: string;
    };
}
Unmeaning answered 19/4, 2019 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.