Skip to content

Commit e8def69

Browse files
authored
fix: Add name property to custom PermissionController errors (#6987)
## Explanation Add a `name` property to `PermissionController` custom errors. This fixes a bug recently introduced in `@metamask/multichain-api-middleware`. A recent PR (#6985) assumed these `name` proeprties were already present. ## References Fixes error in #6985 ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds a base `CustomError` to set `name` on PermissionController errors, updates affected error classes and tests, and updates related changelogs (including multichain API middleware fix note). > > - **Permission Controller**: > - **Errors**: Introduce `CustomError` base that sets `name`; migrate multiple custom errors to extend it (e.g., `InvalidSubjectIdentifierError`, `UnrecognizedSubjectError`, `Caveat*` errors, etc.). > - **Tests**: Add assertions for `.name` on `CaveatMergeTypeMismatchError` and `EndowmentPermissionDoesNotExistError` in `src/errors.test.ts`. > - **Docs**: Update `CHANGELOG.md` with “Added” entry for error `name` property. > - **Multichain API Middleware**: > - **Docs**: Update `CHANGELOG.md` with “Fixed” entry for `wallet_revokeSession` error handling referencing changes in `@metamask/permission-controller`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 9b0e3ed. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 82aec7d commit e8def69

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

packages/multichain-api-middleware/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix `wallet_revokeSession` error handling ([#6987](https://github.com/MetaMask/core/pull/6987))
13+
- This was broken in a different way in v1.2.3. Fixed in this version by a change in `@metamask/permission-controller`.
14+
1015
## [1.2.3]
1116

1217
### Changed

packages/permission-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add `name` property to permission errors ([#6987](https://github.com/MetaMask/core/pull/6987))
13+
1014
## [12.0.0]
1115

1216
### Changed

packages/permission-controller/src/errors.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ describe('error', () => {
1313
},
1414
);
1515
});
16+
17+
it('has the expected name', () => {
18+
expect(new CaveatMergeTypeMismatchError('foo', 'bar').name).toBe(
19+
'CaveatMergeTypeMismatchError',
20+
);
21+
});
1622
});
1723

1824
describe('EndowmentPermissionDoesNotExistError', () => {
@@ -29,5 +35,11 @@ describe('error', () => {
2935
new EndowmentPermissionDoesNotExistError('bar').data,
3036
).toBeUndefined();
3137
});
38+
39+
it('has the expected name', () => {
40+
expect(new EndowmentPermissionDoesNotExistError('bar').name).toBe(
41+
'EndowmentPermissionDoesNotExistError',
42+
);
43+
});
3244
});
3345
});

packages/permission-controller/src/errors.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,32 @@ export function internalError<Data extends Record<string, unknown>>(
9090
return rpcErrors.internal({ message, data });
9191
}
9292

93-
export class InvalidSubjectIdentifierError extends Error {
93+
class CustomError extends Error {
94+
constructor(message?: string) {
95+
super(message);
96+
this.name = this.constructor.name;
97+
}
98+
}
99+
100+
export class InvalidSubjectIdentifierError extends CustomError {
94101
constructor(origin: unknown) {
95102
super(
96103
`Invalid subject identifier: "${
97104
typeof origin === 'string' ? origin : typeof origin
98105
}"`,
99106
);
107+
this.name = this.constructor.name;
100108
}
101109
}
102110

103-
export class UnrecognizedSubjectError extends Error {
111+
export class UnrecognizedSubjectError extends CustomError {
104112
constructor(origin: string) {
105113
super(`Unrecognized subject: "${origin}" has no permissions.`);
114+
this.name = this.constructor.name;
106115
}
107116
}
108117

109-
export class CaveatMergerDoesNotExistError extends Error {
118+
export class CaveatMergerDoesNotExistError extends CustomError {
110119
constructor(caveatType: string) {
111120
super(`Caveat value merger does not exist for type: "${caveatType}"`);
112121
}
@@ -132,7 +141,7 @@ export class InvalidMergedPermissionsError extends Error {
132141
}
133142
}
134143

135-
export class InvalidApprovedPermissionError extends Error {
144+
export class InvalidApprovedPermissionError extends CustomError {
136145
public data: {
137146
origin: string;
138147
target: string;
@@ -150,13 +159,13 @@ export class InvalidApprovedPermissionError extends Error {
150159
this.data = { origin, target, approvedPermission };
151160
}
152161
}
153-
export class PermissionDoesNotExistError extends Error {
162+
export class PermissionDoesNotExistError extends CustomError {
154163
constructor(origin: string, target: string) {
155164
super(`Subject "${origin}" has no permission for "${target}".`);
156165
}
157166
}
158167

159-
export class EndowmentPermissionDoesNotExistError extends Error {
168+
export class EndowmentPermissionDoesNotExistError extends CustomError {
160169
public data?: { origin: string };
161170

162171
constructor(target: string, origin?: string) {
@@ -171,7 +180,7 @@ export class EndowmentPermissionDoesNotExistError extends Error {
171180
}
172181
}
173182

174-
export class UnrecognizedCaveatTypeError extends Error {
183+
export class UnrecognizedCaveatTypeError extends CustomError {
175184
public data: {
176185
caveatType: string;
177186
origin?: string;
@@ -195,7 +204,7 @@ export class UnrecognizedCaveatTypeError extends Error {
195204
}
196205
}
197206

198-
export class InvalidCaveatsPropertyError extends Error {
207+
export class InvalidCaveatsPropertyError extends CustomError {
199208
public data: { origin: string; target: string; caveatsProperty: unknown };
200209

201210
constructor(origin: string, target: string, caveatsProperty: unknown) {
@@ -206,15 +215,15 @@ export class InvalidCaveatsPropertyError extends Error {
206215
}
207216
}
208217

209-
export class CaveatDoesNotExistError extends Error {
218+
export class CaveatDoesNotExistError extends CustomError {
210219
constructor(origin: string, target: string, caveatType: string) {
211220
super(
212221
`Permission for "${target}" of subject "${origin}" has no caveat of type "${caveatType}".`,
213222
);
214223
}
215224
}
216225

217-
export class CaveatAlreadyExistsError extends Error {
226+
export class CaveatAlreadyExistsError extends CustomError {
218227
constructor(origin: string, target: string, caveatType: string) {
219228
super(
220229
`Permission for "${target}" of subject "${origin}" already has a caveat of type "${caveatType}".`,
@@ -237,7 +246,7 @@ export class InvalidCaveatError extends JsonRpcError<
237246
}
238247
}
239248

240-
export class InvalidCaveatTypeError extends Error {
249+
export class InvalidCaveatTypeError extends CustomError {
241250
public data: {
242251
caveat: Record<string, unknown>;
243252
origin: string;
@@ -250,7 +259,7 @@ export class InvalidCaveatTypeError extends Error {
250259
}
251260
}
252261

253-
export class CaveatMissingValueError extends Error {
262+
export class CaveatMissingValueError extends CustomError {
254263
public data: {
255264
caveat: Record<string, unknown>;
256265
origin: string;
@@ -263,7 +272,7 @@ export class CaveatMissingValueError extends Error {
263272
}
264273
}
265274

266-
export class CaveatInvalidJsonError extends Error {
275+
export class CaveatInvalidJsonError extends CustomError {
267276
public data: {
268277
caveat: Record<string, unknown>;
269278
origin: string;
@@ -276,7 +285,7 @@ export class CaveatInvalidJsonError extends Error {
276285
}
277286
}
278287

279-
export class InvalidCaveatFieldsError extends Error {
288+
export class InvalidCaveatFieldsError extends CustomError {
280289
public data: {
281290
caveat: Record<string, unknown>;
282291
origin: string;
@@ -291,7 +300,7 @@ export class InvalidCaveatFieldsError extends Error {
291300
}
292301
}
293302

294-
export class ForbiddenCaveatError extends Error {
303+
export class ForbiddenCaveatError extends CustomError {
295304
public data: {
296305
caveatType: string;
297306
origin: string;
@@ -306,7 +315,7 @@ export class ForbiddenCaveatError extends Error {
306315
}
307316
}
308317

309-
export class DuplicateCaveatError extends Error {
318+
export class DuplicateCaveatError extends CustomError {
310319
public data: {
311320
caveatType: string;
312321
origin: string;
@@ -321,7 +330,7 @@ export class DuplicateCaveatError extends Error {
321330
}
322331
}
323332

324-
export class CaveatMergeTypeMismatchError extends Error {
333+
export class CaveatMergeTypeMismatchError extends CustomError {
325334
public data: {
326335
leftCaveatType: string;
327336
rightCaveatType: string;
@@ -335,7 +344,7 @@ export class CaveatMergeTypeMismatchError extends Error {
335344
}
336345
}
337346

338-
export class CaveatSpecificationMismatchError extends Error {
347+
export class CaveatSpecificationMismatchError extends CustomError {
339348
public data: {
340349
caveatSpec: Record<string, unknown>;
341350
permissionType: PermissionType;
@@ -352,7 +361,7 @@ export class CaveatSpecificationMismatchError extends Error {
352361
}
353362
}
354363

355-
export class PermissionsRequestNotFoundError extends Error {
364+
export class PermissionsRequestNotFoundError extends CustomError {
356365
constructor(id: string) {
357366
super(`Permissions request with id "${id}" not found.`);
358367
}

0 commit comments

Comments
 (0)