Skip to content

Commit e962d6f

Browse files
authored
fix: Log Firebase auth and send error message (#2554)
In a previous PR, we added the ability to directly log error messages from the Firebase Auth endpoint. However, the error structure for sendMessage differs from that of the Auth endpoint. In this PR, we update the logging to handle and log both error structures.
1 parent 6e6e39c commit e962d6f

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/datasources/push-notifications-api/firebase-cloud-messaging-api.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {
2020
FirebaseOauth2Token,
2121
FirebaseOauth2TokenSchema,
2222
} from '@/datasources/push-notifications-api/entities/firebase-oauth2-token.entity';
23-
import { get } from 'lodash';
2423
import { NetworkResponseError } from '@/datasources/network/entities/network.error.entity';
24+
import { getFirstAvailable } from '@/domain/common/utils/array';
2525

2626
@Injectable()
2727
export class FirebaseCloudMessagingApiService implements IPushNotificationsApi {
@@ -35,7 +35,10 @@ export class FirebaseCloudMessagingApiService implements IPushNotificationsApi {
3535
private static readonly DefaultIosNotificationBody =
3636
'New Activity with your Safe';
3737

38-
private static readonly ERROR_ARRAY_PATH = 'data.error_description';
38+
private static readonly ERROR_ARRAY_PATH = [
39+
'data.error_description',
40+
'data.error.message',
41+
];
3942

4043
private readonly baseUrl: string;
4144
private readonly project: string;
@@ -199,7 +202,7 @@ export class FirebaseCloudMessagingApiService implements IPushNotificationsApi {
199202

200203
private mapError(error: unknown): unknown {
201204
if (error instanceof NetworkResponseError) {
202-
const errorMessage = get(
205+
const errorMessage = getFirstAvailable(
203206
error,
204207
FirebaseCloudMessagingApiService.ERROR_ARRAY_PATH,
205208
);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { getFirstAvailable } from '../array';
2+
3+
describe('getFirstAvailable()', () => {
4+
it('Should return the first available value from the given paths', () => {
5+
const sourceObject = { a: 1, b: 2, c: 3 };
6+
const paths = ['b', 'c', 'a'];
7+
8+
const result = getFirstAvailable(sourceObject, paths);
9+
10+
expect(result).toBe(sourceObject.b);
11+
});
12+
13+
it('Should return undefined if none of the paths exist in the source object', () => {
14+
const sourceObject = { a: 1, b: 2 };
15+
const paths = ['c', 'd'];
16+
17+
const result = getFirstAvailable(sourceObject, paths);
18+
19+
expect(result).toBeUndefined();
20+
});
21+
22+
it('Should return undefined if the paths array is empty', () => {
23+
const sourceObject = { a: 1, b: 2 };
24+
const paths: Array<string> = [];
25+
26+
const result = getFirstAvailable(sourceObject, paths);
27+
28+
expect(result).toBeUndefined();
29+
});
30+
31+
it('Should handle nested paths correctly', () => {
32+
const sourceObject = { a: { b: { c: 42 } } };
33+
const paths = ['a.b.c', 'a.b', 'a'];
34+
35+
const result = getFirstAvailable(sourceObject, paths);
36+
37+
expect(result).toBe(sourceObject.a.b.c);
38+
});
39+
40+
it('Should return undefined if the source object is empty', () => {
41+
const sourceObject = {};
42+
const paths = ['a', 'b'];
43+
const result = getFirstAvailable(sourceObject, paths);
44+
expect(result).toBeUndefined();
45+
});
46+
47+
it('Should return the first non-undefined value even if it is falsy', () => {
48+
const sourceObject = { a: undefined, b: 0, c: false };
49+
const paths = ['a', 'b', 'c'];
50+
const result = getFirstAvailable(sourceObject, paths);
51+
expect(result).toBe(0);
52+
});
53+
});

src/domain/common/utils/array.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { GetFieldType } from 'lodash';
2+
import get from 'lodash/get';
3+
4+
export function getFirstAvailable<TObject, TPath extends string>(
5+
sourceObject: TObject,
6+
paths: Array<string>,
7+
): GetFieldType<TObject, TPath> | undefined {
8+
for (const path of paths) {
9+
const value = get(sourceObject, path);
10+
if (value !== undefined && value !== null) {
11+
return value;
12+
}
13+
}
14+
15+
return undefined;
16+
}

0 commit comments

Comments
 (0)