Skip to content

Commit dab1a7f

Browse files
feat: create common graphql error handler (#384)
1 parent 96e3944 commit dab1a7f

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

apps/portal/src/app/graphql/graphql.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,16 @@ export class GraphqlService {
5959
fetchPolicy: 'network-only',
6060
});
6161
}
62+
63+
// eslint-disable-next-line class-methods-use-this
64+
validateApolloErrors<T>(response: ApolloQueryResult<T>): void {
65+
if (response.errors && response.errors.length > 0) {
66+
const errorMessages = response.errors.map((err) => err.message).join('; ');
67+
throw new Error(`GraphQL Formatted Error(s): ${errorMessages}`);
68+
}
69+
70+
if (response.error) {
71+
throw new Error(`Apollo Error: ${response.error.message}`);
72+
}
73+
}
6274
}

apps/portal/src/app/views/notifications/notifications.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export class NotificationsComponent implements OnInit {
404404
key: 'tst',
405405
severity: 'error',
406406
summary: 'Error',
407-
detail: `There was an error while loading notifications. Reason: ${error.message}`,
407+
detail: `There was an error while loading archived notifications. Reason: ${error.message}`,
408408
});
409409
this.loading = false;
410410
return of(null);

apps/portal/src/app/views/notifications/notifications.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class NotificationsService {
3232
getNotifications(variables: unknown, inputToken: string): Observable<NotificationResponse> {
3333
return this.graphqlService.query(GetNotifications, variables, inputToken).pipe(
3434
map((response: ApolloQueryResult<GetNotificationsResponse>) => {
35+
this.graphqlService.validateApolloErrors(response);
3536
const notificationArray = response.data?.notifications.notifications;
3637

3738
const notificationResponseObject: NotificationResponse = {
@@ -52,6 +53,7 @@ export class NotificationsService {
5253
getArchivedNotifications(variables, inputToken): Observable<NotificationResponse> {
5354
return this.graphqlService.query(GetArchivedNotifications, variables, inputToken).pipe(
5455
map((response: ApolloQueryResult<GetArchivedNotificationsResponse>) => {
56+
this.graphqlService.validateApolloErrors(response);
5557
const archivedNotificationArray =
5658
response.data?.archivedNotifications.archivedNotifications;
5759

apps/portal/src/app/views/providers/providers.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class ProvidersService {
5555
.query(GetProvidersAndArchivedNotifications, variables, inputToken)
5656
.pipe(
5757
map((response: ApolloQueryResult<GetProviderArchivedNotificationResponse>) => {
58+
this.graphqlService.validateApolloErrors(response);
5859
const providerArray = response.data?.providers.providers;
5960
const archivedNotificationArray =
6061
response.data?.archivedNotifications.archivedNotifications;
@@ -95,6 +96,7 @@ export class ProvidersService {
9596

9697
return this.graphqlService.query(GetProvidersAndNotifications, variables, inputToken).pipe(
9798
map((response: ApolloQueryResult<GetProviderNotificationResponse>) => {
99+
this.graphqlService.validateApolloErrors(response);
98100
const providerArray = response.data?.providers.providers;
99101
const notificationArray = response.data?.notifications.notifications;
100102

0 commit comments

Comments
 (0)