Skip to content

Commit

Permalink
Refactor notification controller error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PooyaRaki committed Dec 4, 2024
1 parent 285a17e commit 85796c5
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/routes/notifications/v1/notifications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Delete,
HttpCode,
Inject,
NotFoundException,
Param,
Post,
} from '@nestjs/common';
Expand Down Expand Up @@ -155,14 +156,28 @@ export class NotificationsController {
): Promise<void> {
if (this.isPushNotificationV2Enabled) {
// Compatibility with V2
await this.notificationServiceV2.deleteDevice(uuid);
try {
await this.notificationServiceV2.deleteDevice(uuid);
} catch (error: unknown) {
if (error instanceof NotFoundException) {
// Do not throw a NotFound error when attempting to remove the token from the CGW,
// This ensures the TX service remove method is called
} else {
throw error;
}
}
}

try {
await this.notificationsService.unregisterDevice({ chainId, uuid });
} catch {
} catch (error: unknown) {
// The token might already have been removed from the TX service.
// If this happens, the TX service will throw an error, but it is safe to ignore it.
// If this happens, the TX service will throw a 404 error, but it is safe to ignore it.
if (error instanceof Error) {
if ('code' in error && error.code !== 404) {
throw error;
}
}
}
}

Expand All @@ -174,13 +189,22 @@ export class NotificationsController {
safeAddress: `0x${string}`,
): Promise<void> {
if (this.isPushNotificationV2Enabled) {
// Compatibility with V2
// @TODO Remove NotificationModuleV2 after all clients have migrated and compatibility is no longer needed.
await this.notificationServiceV2.deleteSubscription({
deviceUuid: uuid,
chainId: chainId,
safeAddress: safeAddress,
});
try {
// Compatibility with V2
// @TODO Remove NotificationModuleV2 after all clients have migrated and compatibility is no longer needed.
await this.notificationServiceV2.deleteSubscription({
deviceUuid: uuid,
chainId: chainId,
safeAddress: safeAddress,
});
} catch (error: unknown) {
if (error instanceof NotFoundException) {
// Do not throw a NotFound error when attempting to remove the token from the CGW,
// This ensures the TX service remove method is called
} else {
throw error;
}
}
}

try {
Expand All @@ -189,9 +213,14 @@ export class NotificationsController {
uuid,
safeAddress,
});
} catch {
} catch (error: unknown) {
// The token might already have been removed from the TX service.
// If this happens, the TX service will throw an error, but it is safe to ignore it.
// If this happens, the TX service will throw a 404 error, but it is safe to ignore it.
if (error instanceof Error) {
if ('code' in error && error.code !== 404) {
throw error;
}
}
}
}
}

0 comments on commit 85796c5

Please sign in to comment.