I have the following try catch
try {
user = await admin.auth().getUserByEmail(inputEmail);
} catch (error) {
if(error.code !== "auth/user-not-found") throw new Error("authentication failed");
}
But I get an an error saying
Object is of type 'unknown'.
On error.code
This code was working perfectly fine before. How can this be solved?
I found the this
https://firebase.google.com/docs/reference/js/v8/firebase.FirebaseError
but I do not know where I can import it from.

I tried to asign any as type

And I tried to check if the error was a instance of Error which says
Property 'code' does not exist on type 'Error'.

The error simply says that type of error is unknown.
try {
// ...
} catch (error: unknown) {
// unknown --> ^^^
}
If you are using Typescript 4.4 then you can use --useUnknownInCatchVariables flag which changes the default type of catch clause variables from any to unknown.
Then you set up User defined type guards to specify type for the error that is being thrown. You can import FirebaseError from @firebase/util as in this issue.
import { FirebaseError } from '@firebase/util';
try {
// ...
} catch (error: unknown) {
if (error instanceof FirebaseError) {
console.error(error.code)
}
}
Can you try it with this:
try {
user = await admin.auth().getUserByEmail(inputEmail);
} catch (error:unknown) {
if (error instanceof Error) {
if(error.code !== "auth/user-not-found") throw new Error("authentication failed");
}
}
Pls check this docs about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With