Skip to content

Commit 43f53ca

Browse files
committed
refactor(errors): improve Google error handling type safety
- Updated error handling functions to accept `unknown` type instead of specific error types, enhancing type safety and flexibility. - Adjusted the `handleExpressError` and `isFullSyncRequired` functions to work with the new type definitions, ensuring consistent error management across the application. - This change improves code robustness and prepares the error handling system for a wider range of error scenarios.
1 parent 546a81b commit 43f53ca

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

packages/backend/src/common/errors/handlers/error.express.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const handleExpressError = async (
9393
}
9494

9595
if (isGoogleError(e)) {
96-
await handleGoogleError(req, res, userId, e);
96+
await handleGoogleError(req, res, userId, e as GaxiosError);
9797
} else {
9898
const errInfo = assembleErrorInfo(e);
9999
res.status(e.status || Status.INTERNAL_SERVER).send(errInfo);

packages/backend/src/common/services/gcal/gcal.utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const getEmailFromUrl = (url: string) => {
4242
};
4343

4444
// occurs when token expired or revoked
45-
export const isInvalidGoogleToken = (e: GaxiosError | Error) => {
45+
export const isInvalidGoogleToken = (e: unknown) => {
4646
if (!isGoogleError(e)) return false;
4747

4848
const err = e as GaxiosError;
@@ -57,9 +57,12 @@ export const isGoogleError = (e: unknown) => {
5757
return e instanceof GaxiosError || (e as any)?.name === "GaxiosError";
5858
};
5959

60-
export const isFullSyncRequired = (e: GaxiosError | Error) => {
61-
if (isGoogleError(e) && e.code) {
62-
const codeStr = typeof e.code === "string" ? e.code : String(e.code);
60+
export const isFullSyncRequired = (e: unknown) => {
61+
if (!isGoogleError(e)) return false;
62+
63+
const err = e as GaxiosError;
64+
if (err.code) {
65+
const codeStr = typeof err.code === "string" ? err.code : String(err.code);
6366
if (parseInt(codeStr) === 410) {
6467
return true;
6568
}

packages/backend/src/sync/controllers/sync.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class SyncController {
107107
res.status(Status.GONE).send(msg);
108108

109109
return;
110-
} else if (isFullSyncRequired(e as Error) && userId) {
110+
} else if (isFullSyncRequired(e) && userId) {
111111
// do not await this call
112112
userService
113113
.restartGoogleCalendarSync(userId, { force: true })

0 commit comments

Comments
 (0)