Skip to content

Commit 6f0c7d1

Browse files
Expose message and reason code (#6797)
## Explanation The Shield backend returns `message` and `reasonCode` from the `/result` endpoint, but we currently don't expose it. This PR exposes these. <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## 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 - [ ] 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 - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Expose backend `message` and `reasonCode` in `CoverageResult` and propagate through coverage checks, with tests/utilities updated. > > - **Shield Controller**: > - **API/Types**: > - Extend `CoverageResult` and `GetCoverageResultResponse` to include optional `message` and `reasonCode`. > - `ShieldRemoteBackend.checkCoverage` and `checkSignatureCoverage` now return `{ coverageId, message, reasonCode, status }`. > - **Tests/Utils**: > - Update `backend.test.ts` to assert returned `message`/`reasonCode`; switch to `getRandomCoverageResult()`. > - Add `tests/utils#getRandomCoverageResult` to generate mock results with `message` and `reasonCode`. > - **Docs**: > - Update `CHANGELOG.md` to note added `message` and `reasonCode` in coverage result type. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 192dd4a. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 9ccfe1b commit 6f0c7d1

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

packages/shield-controller/CHANGELOG.md

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

1212
- Log `not_shown` if result is not available ([#6667](https://github.com/MetaMask/core/pull/6667))
13+
- Add `message` and `reasonCode` to coverage result type ([#6797](https://github.com/MetaMask/core/pull/6797))
1314

1415
### Changed
1516

packages/shield-controller/src/backend.test.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ShieldRemoteBackend } from './backend';
22
import {
33
generateMockSignatureRequest,
44
generateMockTxMeta,
5-
getRandomCoverageStatus,
5+
getRandomCoverageResult,
66
} from '../tests/utils';
77

88
/**
@@ -52,19 +52,19 @@ describe('ShieldRemoteBackend', () => {
5252
const coverageId = 'coverageId';
5353
fetchMock.mockResolvedValueOnce({
5454
status: 200,
55-
json: jest.fn().mockResolvedValue({ coverageId: 'coverageId' }),
55+
json: jest.fn().mockResolvedValue({ coverageId }),
5656
} as unknown as Response);
5757

5858
// Mock get coverage result.
59-
const status = getRandomCoverageStatus();
59+
const result = getRandomCoverageResult();
6060
fetchMock.mockResolvedValueOnce({
6161
status: 200,
62-
json: jest.fn().mockResolvedValue({ status }),
62+
json: jest.fn().mockResolvedValue(result),
6363
} as unknown as Response);
6464

6565
const txMeta = generateMockTxMeta();
6666
const coverageResult = await backend.checkCoverage({ txMeta });
67-
expect(coverageResult).toStrictEqual({ coverageId, status });
67+
expect(coverageResult).toStrictEqual({ coverageId, ...result });
6868
expect(fetchMock).toHaveBeenCalledTimes(2);
6969
expect(getAccessToken).toHaveBeenCalledTimes(2);
7070
});
@@ -88,15 +88,18 @@ describe('ShieldRemoteBackend', () => {
8888
} as unknown as Response);
8989

9090
// Mock get coverage result: result available.
91-
const status = getRandomCoverageStatus();
91+
const result = getRandomCoverageResult();
9292
fetchMock.mockResolvedValueOnce({
9393
status: 200,
94-
json: jest.fn().mockResolvedValue({ status }),
94+
json: jest.fn().mockResolvedValue(result),
9595
} as unknown as Response);
9696

9797
const txMeta = generateMockTxMeta();
9898
const coverageResult = await backend.checkCoverage({ txMeta });
99-
expect(coverageResult).toStrictEqual({ coverageId, status });
99+
expect(coverageResult).toStrictEqual({
100+
coverageId,
101+
...result,
102+
});
100103
expect(fetchMock).toHaveBeenCalledTimes(3);
101104
expect(getAccessToken).toHaveBeenCalledTimes(2);
102105
});
@@ -160,17 +163,20 @@ describe('ShieldRemoteBackend', () => {
160163
} as unknown as Response);
161164

162165
// Mock get coverage result.
163-
const status = getRandomCoverageStatus();
166+
const result = getRandomCoverageResult();
164167
fetchMock.mockResolvedValueOnce({
165168
status: 200,
166-
json: jest.fn().mockResolvedValue({ status }),
169+
json: jest.fn().mockResolvedValue(result),
167170
} as unknown as Response);
168171

169172
const signatureRequest = generateMockSignatureRequest();
170173
const coverageResult = await backend.checkSignatureCoverage({
171174
signatureRequest,
172175
});
173-
expect(coverageResult).toStrictEqual({ coverageId, status });
176+
expect(coverageResult).toStrictEqual({
177+
coverageId,
178+
...result,
179+
});
174180
expect(fetchMock).toHaveBeenCalledTimes(2);
175181
expect(getAccessToken).toHaveBeenCalledTimes(2);
176182
});

packages/shield-controller/src/backend.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export type GetCoverageResultRequest = {
4242
};
4343

4444
export type GetCoverageResultResponse = {
45+
message?: string;
46+
reasonCode?: string;
4547
status: CoverageStatus;
4648
};
4749

@@ -87,7 +89,12 @@ export class ShieldRemoteBackend implements ShieldBackend {
8789
}
8890

8991
const coverageResult = await this.#getCoverageResult(coverageId);
90-
return { coverageId, status: coverageResult.status };
92+
return {
93+
coverageId,
94+
message: coverageResult.message,
95+
reasonCode: coverageResult.reasonCode,
96+
status: coverageResult.status,
97+
};
9198
}
9299

93100
async checkSignatureCoverage(
@@ -103,7 +110,12 @@ export class ShieldRemoteBackend implements ShieldBackend {
103110
}
104111

105112
const coverageResult = await this.#getCoverageResult(coverageId);
106-
return { coverageId, status: coverageResult.status };
113+
return {
114+
coverageId,
115+
message: coverageResult.message,
116+
reasonCode: coverageResult.reasonCode,
117+
status: coverageResult.status,
118+
};
107119
}
108120

109121
async logSignature(req: LogSignatureRequest): Promise<void> {

packages/shield-controller/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { TransactionMeta } from '@metamask/transaction-controller';
33

44
export type CoverageResult = {
55
coverageId: string;
6+
message?: string;
7+
reasonCode?: string;
68
status: CoverageStatus;
79
};
810

packages/shield-controller/tests/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ export function getRandomCoverageStatus(): CoverageStatus {
6666
return coverageStatuses[Math.floor(Math.random() * coverageStatuses.length)];
6767
}
6868

69+
/**
70+
* Get a random coverage result.
71+
*
72+
* @returns A random coverage result.
73+
*/
74+
export function getRandomCoverageResult() {
75+
return {
76+
status: getRandomCoverageStatus(),
77+
message: 'message',
78+
reasonCode: 'reasonCode',
79+
};
80+
}
81+
6982
/**
7083
* Setup a coverage result received handler.
7184
*

0 commit comments

Comments
 (0)