What is the best way to get typed errors?
On the server side I'm doing this to handle errors:
The
Which makes me think that the error is of type
Is there another way to handle this without having to have that utility function to satisfy TS?
export const emailPasswordSignUpForm = form(
SignUpEmailPasswordSchema,
async ({ email, _password, name }, issue) => {
try {
await auth.api.signUpEmail({
body: { name, email, password: _password, callbackURL: "/trips" },
});
return {
success: true,
};
} catch (error) {
console.log("DAS error", error);
if (hasErrorCode(error)) {
if (error.body.code === "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL") {
return invalid(issue.email("An account with this email already exists."));
}
}
invalid(issue("An unexpected error occurred during sign up."));
}
},
);export const emailPasswordSignUpForm = form(
SignUpEmailPasswordSchema,
async ({ email, _password, name }, issue) => {
try {
await auth.api.signUpEmail({
body: { name, email, password: _password, callbackURL: "/trips" },
});
return {
success: true,
};
} catch (error) {
console.log("DAS error", error);
if (hasErrorCode(error)) {
if (error.body.code === "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL") {
return invalid(issue.email("An account with this email already exists."));
}
}
invalid(issue("An unexpected error occurred during sign up."));
}
},
);The
console.log("DAS error", error);console.log("DAS error", error); will output this:DAS error [InternalAPIError: User already exists. Use another email.] {
status: 'UNPROCESSABLE_ENTITY',
body: {
code: 'USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL',
message: 'User already exists. Use another email.'
},
headers: {},
statusCode: 422
}DAS error [InternalAPIError: User already exists. Use another email.] {
status: 'UNPROCESSABLE_ENTITY',
body: {
code: 'USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL',
message: 'User already exists. Use another email.'
},
headers: {},
statusCode: 422
}Which makes me think that the error is of type
InternalAPIErrorInternalAPIError but that is not exposed. So when using TS I have to inspect the error: function hasErrorCode(error: unknown): error is { body: { code: string } } {
if (typeof error !== "object" || error === null) return false;
const body = (error as { body?: unknown }).body;
if (typeof body !== "object" || body === null) return false;
const code = (body as { code?: unknown }).code;
return typeof code === "string";
}function hasErrorCode(error: unknown): error is { body: { code: string } } {
if (typeof error !== "object" || error === null) return false;
const body = (error as { body?: unknown }).body;
if (typeof body !== "object" || body === null) return false;
const code = (body as { code?: unknown }).code;
return typeof code === "string";
}Is there another way to handle this without having to have that utility function to satisfy TS?