Skip to content

Commit c6949ac

Browse files
committed
Use proper MetaMask logging
1 parent 346dad0 commit c6949ac

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

packages/gator-permissions-controller/src/GatorPermissionContoller.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ describe('gator-permissions-controller - fetchAndUpdateGatorPermissions() tests'
386386
});
387387

388388
await expect(controller.fetchAndUpdateGatorPermissions()).rejects.toThrow(
389-
'Failed to fetch gator permissions',
389+
'Gator permissions are not enabled',
390390
);
391391
});
392392

@@ -466,7 +466,7 @@ describe('gator-permissions-controller - fetchAndUpdateGatorPermissions() tests'
466466
await controller.enableGatorPermissions();
467467

468468
await expect(controller.fetchAndUpdateGatorPermissions()).rejects.toThrow(
469-
'Failed to fetch gator permissions',
469+
'Unsupported permission type: invalid-type',
470470
);
471471
});
472472

@@ -500,7 +500,7 @@ describe('gator-permissions-controller - fetchAndUpdateGatorPermissions() tests'
500500
await controller.enableGatorPermissions();
501501

502502
await expect(controller.fetchAndUpdateGatorPermissions()).rejects.toThrow(
503-
'Failed to fetch gator permissions',
503+
'Invalid permission signer type. Only account signer is supported',
504504
);
505505
});
506506

@@ -519,7 +519,7 @@ describe('gator-permissions-controller - fetchAndUpdateGatorPermissions() tests'
519519
await controller.enableGatorPermissions();
520520

521521
await expect(controller.fetchAndUpdateGatorPermissions()).rejects.toThrow(
522-
'Failed to fetch gator permissions',
522+
'Storage error',
523523
);
524524

525525
expect(controller.state.isFetchingGatorPermissions).toBe(false);

packages/gator-permissions-controller/src/GatorPermissionsController.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { BaseController } from '@metamask/base-controller';
88
import type { HandleSnapRequest, HasSnap } from '@metamask/snaps-controllers';
99
import type { SnapId } from '@metamask/snaps-sdk';
1010
import { HandlerType } from '@metamask/snaps-utils';
11+
import { createModuleLogger } from '@metamask/utils';
1112

13+
import { projectLogger } from './logger';
1214
import type {
1315
GatorPermissionsList,
1416
NativeTokenStreamPermission,
@@ -30,6 +32,9 @@ const controllerName = 'GatorPermissionsController';
3032
const defaultGatorPermissionsProviderSnapId =
3133
'@metamask/gator-permissions-snap' as SnapId;
3234

35+
// Logger for the controller
36+
const log = createModuleLogger(projectLogger, 'GatorPermissionsController');
37+
3338
// Enum for the RPC methods of the gator permissions provider snap
3439
enum GatorPermissionsSnapRpcMethod {
3540
/**
@@ -428,9 +433,9 @@ export default class GatorPermissionsController extends BaseController<
428433
});
429434

430435
return gatorPermissionsList;
431-
} catch (err) {
432-
console.error('Failed to fetch gator permissions', err);
433-
throw new Error('Failed to fetch gator permissions');
436+
} catch (error) {
437+
log('Failed to fetch gator permissions', error);
438+
throw error;
434439
} finally {
435440
this.#setIsFetchingGatorPermissions(false);
436441
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* istanbul ignore file */
2+
3+
import { createProjectLogger, createModuleLogger } from '@metamask/utils';
4+
5+
export const projectLogger = createProjectLogger(
6+
'gator-permissions-controller',
7+
);
8+
9+
export { createModuleLogger };

packages/gator-permissions-controller/src/utils.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ describe('utils - serializeGatorPermissionsList() tests', () => {
1818
JSON.stringify(gatorPermissionsList),
1919
);
2020
});
21+
22+
it('throws an error when serialization fails', () => {
23+
// Create a valid GatorPermissionsList structure but with circular reference
24+
const gatorPermissionsList = {
25+
'native-token-stream': {},
26+
'native-token-periodic': {},
27+
'erc20-token-stream': {},
28+
};
29+
30+
// Add circular reference to cause JSON.stringify to fail
31+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
32+
(gatorPermissionsList as any).circular = gatorPermissionsList;
33+
34+
expect(() => {
35+
serializeGatorPermissionsList(gatorPermissionsList);
36+
}).toThrow('Converting circular structure to JSON');
37+
});
2138
});
2239

2340
describe('utils - deserializeGatorPermissionsList() tests', () => {
@@ -39,4 +56,12 @@ describe('utils - deserializeGatorPermissionsList() tests', () => {
3956
gatorPermissionsList,
4057
);
4158
});
59+
60+
it('throws an error when deserialization fails', () => {
61+
const invalidJson = '{"invalid": json}';
62+
63+
expect(() => {
64+
deserializeGatorPermissionsList(invalidJson);
65+
}).toThrow('Unexpected token');
66+
});
4267
});

packages/gator-permissions-controller/src/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { createModuleLogger } from '@metamask/utils';
2+
3+
import { projectLogger } from './logger';
14
import type { GatorPermissionsList } from './types';
25

6+
const log = createModuleLogger(projectLogger, 'utils');
7+
38
/**
49
* Serializes a gator permissions list to a string.
510
*
@@ -9,7 +14,12 @@ import type { GatorPermissionsList } from './types';
914
export function serializeGatorPermissionsList(
1015
gatorPermissionsList: GatorPermissionsList,
1116
): string {
12-
return JSON.stringify(gatorPermissionsList);
17+
try {
18+
return JSON.stringify(gatorPermissionsList);
19+
} catch (error) {
20+
log('Failed to serialize gator permissions list', error);
21+
throw error;
22+
}
1323
}
1424

1525
/**
@@ -21,5 +31,10 @@ export function serializeGatorPermissionsList(
2131
export function deserializeGatorPermissionsList(
2232
gatorPermissionsList: string,
2333
): GatorPermissionsList {
24-
return JSON.parse(gatorPermissionsList);
34+
try {
35+
return JSON.parse(gatorPermissionsList);
36+
} catch (error) {
37+
log('Failed to deserialize gator permissions list', error);
38+
throw error;
39+
}
2540
}

0 commit comments

Comments
 (0)