Skip to content

Commit eb2a4c2

Browse files
committed
Mark error context and objects inside those contexts as read-only
1 parent 8f32fdc commit eb2a4c2

File tree

4 files changed

+490
-471
lines changed

4 files changed

+490
-471
lines changed

packages/errors/src/__tests__/error-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@solana/test-matchers/toBeFrozenObject';
2+
13
import { isSolanaError, SolanaError } from '../error';
24
import { getErrorMessage } from '../message-formatter';
35

@@ -25,6 +27,9 @@ describe('SolanaError', () => {
2527
it('calls the message formatter with the code and context', () => {
2628
expect(getErrorMessage).toHaveBeenCalledWith(123, expect.objectContaining({ foo: 'bar' }));
2729
});
30+
it('freezes the context object', () => {
31+
expect(errorWithContext.context).toBeFrozenObject();
32+
});
2833
});
2934
describe('given an error with no context', () => {
3035
beforeEach(() => {

packages/errors/src/__typetests__/error-typetest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ if (unknownError.context.__code === SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSIN
3939
const e = null as unknown;
4040
if (isSolanaError(e)) {
4141
e.context satisfies Readonly<{ __code: SolanaErrorCode }>;
42+
// @ts-expect-error Code is read-only
43+
e.context.__code = SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING;
4244
}
4345
if (isSolanaError(e, SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING)) {
4446
e.context satisfies SolanaErrorContext[typeof SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING];
4547
// @ts-expect-error Context belongs to another error code
4648
e.context satisfies SolanaErrorContext[typeof SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING];
49+
// @ts-expect-error Context is read-only
50+
e.context.addresses = [] as unknown as typeof e.context.addresses;
51+
// @ts-expect-error Objects in context are read-only
52+
e.context.addresses.push('abc' as unknown as (typeof e.context.addresses)[number]);
4753
}
4854

4955
// `SolanaErrorContext` must not contain any keys reserved by `ErrorOptions` (eg. `cause`)

0 commit comments

Comments
 (0)