Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Let the new coded exceptions from codecs-core bubble up as-is through @solana/options et al #2244

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/codecs-core/src/__tests__/reverse-codec-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE, SolanaError } from '@solana/errors';
import { SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH, SolanaError } from '@solana/errors';

import { createDecoder, createEncoder } from '../codec';
import { fixCodec } from '../fix-codec';
Expand Down Expand Up @@ -31,7 +31,7 @@ describe('reverseCodec', () => {
// Variable-size codec.
// @ts-expect-error Reversed codec should be fixed-size.
expect(() => reverseCodec(base16)).toThrow(
new SolanaError(SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE),
new SolanaError(SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH),
);
});
});
Expand All @@ -52,7 +52,7 @@ describe('reverseEncoder', () => {

// @ts-expect-error Reversed encoder should be fixed-size.
expect(() => reverseEncoder(base16)).toThrow(
new SolanaError(SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE),
new SolanaError(SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH),
);
});
});
Expand All @@ -70,7 +70,7 @@ describe('reverseDecoder', () => {

// @ts-expect-error Reversed decoder should be fixed-size.
expect(() => reverseDecoder(base16)).toThrow(
new SolanaError(SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE),
new SolanaError(SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH),
);
});
});
27 changes: 2 additions & 25 deletions packages/codecs-core/src/reverse-codec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import {
isSolanaError,
SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE,
SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH,
SolanaError,
} from '@solana/errors';

import {
assertIsFixedSize,
createDecoder,
Expand All @@ -21,15 +14,7 @@ import { combineCodec } from './combine-codec';
export function reverseEncoder<TFrom, TSize extends number>(
encoder: FixedSizeEncoder<TFrom, TSize>,
): FixedSizeEncoder<TFrom, TSize> {
try {
assertIsFixedSize(encoder);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE);
} else {
throw e;
}
}
assertIsFixedSize(encoder);
return createEncoder({
...encoder,
write: (value: TFrom, bytes, offset) => {
Expand All @@ -47,15 +32,7 @@ export function reverseEncoder<TFrom, TSize extends number>(
export function reverseDecoder<TTo, TSize extends number>(
decoder: FixedSizeDecoder<TTo, TSize>,
): FixedSizeDecoder<TTo, TSize> {
try {
assertIsFixedSize(decoder);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE);
} else {
throw e;
}
}
assertIsFixedSize(decoder);
return createDecoder({
...decoder,
read: (bytes, offset) => {
Expand Down
6 changes: 2 additions & 4 deletions packages/codecs-data-structures/src/__tests__/boolean-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getU32Codec } from '@solana/codecs-numbers';
import { getStringCodec } from '@solana/codecs-strings';
import { SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE, SolanaError } from '@solana/errors';
import { SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH, SolanaError } from '@solana/errors';

import { getBooleanCodec } from '../boolean';
import { b } from './__setup__';
Expand Down Expand Up @@ -28,9 +28,7 @@ describe('getBooleanCodec', () => {
// Fails if the codec is not fixed size.
// @ts-expect-error Boolean codec should be fixed-size.
expect(() => boolean({ size: string() }).read(b('00000000'), 0)).toThrow(
new SolanaError(SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE, {
codecDescription: 'bool',
}),
new SolanaError(SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH),
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FixedSizeCodec } from '@solana/codecs-core';
import { getU8Codec, getU16Codec, getU64Codec } from '@solana/codecs-numbers';
import { getStringCodec } from '@solana/codecs-strings';
import { SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC, SolanaError } from '@solana/errors';
import { SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH, SolanaError } from '@solana/errors';

import { getNullableCodec } from '../nullable';
import { getUnitCodec } from '../unit';
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('getNullableCodec', () => {
// Fixed nullables must wrap fixed-size items.
// @ts-expect-error It cannot wrap a variable size item when fixed is true.
expect(() => nullable(string(), { fixed: true })).toThrow(
new SolanaError(SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC),
new SolanaError(SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH),
);
});

Expand Down
30 changes: 2 additions & 28 deletions packages/codecs-data-structures/src/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ import {
NumberDecoder,
NumberEncoder,
} from '@solana/codecs-numbers';
import {
isSolanaError,
SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE,
SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH,
SolanaError,
} from '@solana/errors';

/** Defines the config for boolean codecs. */
export type BooleanCodecConfig<TSize extends NumberCodec | NumberEncoder | NumberDecoder> = {
Expand All @@ -48,17 +42,7 @@ export function getBooleanEncoder<TSize extends number>(
export function getBooleanEncoder(config: BooleanCodecConfig<NumberEncoder>): Encoder<boolean>;
export function getBooleanEncoder(config: BooleanCodecConfig<NumberEncoder> = {}): Encoder<boolean> {
const size = config.size ?? getU8Encoder();
try {
assertIsFixedSize(size);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE, {
codecDescription: 'bool',
});
} else {
throw e;
}
}
assertIsFixedSize(size);
return mapEncoder(size, (value: boolean) => (value ? 1 : 0));
}

Expand All @@ -74,17 +58,7 @@ export function getBooleanDecoder<TSize extends number>(
export function getBooleanDecoder(config: BooleanCodecConfig<NumberDecoder>): Decoder<boolean>;
export function getBooleanDecoder(config: BooleanCodecConfig<NumberDecoder> = {}): Decoder<boolean> {
const size = config.size ?? getU8Decoder();
try {
assertIsFixedSize(size);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE, {
codecDescription: 'bool',
});
} else {
throw e;
}
}
assertIsFixedSize(size);
return mapDecoder(size, (value: number | bigint): boolean => Number(value) === 1);
}

Expand Down
47 changes: 4 additions & 43 deletions packages/codecs-data-structures/src/nullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ import {
NumberDecoder,
NumberEncoder,
} from '@solana/codecs-numbers';
import {
isSolanaError,
SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH,
SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC,
SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX,
SolanaError,
} from '@solana/errors';

import { getMaxSize, sumCodecSizes } from './utils';

Expand Down Expand Up @@ -81,24 +74,8 @@ export function getNullableEncoder<TFrom>(

const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
try {
assertIsFixedSize(item);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC);
} else {
throw e;
}
}
try {
assertIsFixedSize(prefix);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX);
} else {
throw e;
}
}
assertIsFixedSize(item);
assertIsFixedSize(prefix);
const fixedSize = prefix.fixedSize + item.fixedSize;
return createEncoder({
fixedSize,
Expand Down Expand Up @@ -154,24 +131,8 @@ export function getNullableDecoder<TTo>(
let fixedSize: number | null = null;
const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
try {
assertIsFixedSize(item);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC);
} else {
throw e;
}
}
try {
assertIsFixedSize(prefix);
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH)) {
throw new SolanaError(SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX);
} else {
throw e;
}
}
assertIsFixedSize(item);
assertIsFixedSize(prefix);
fixedSize = prefix.fixedSize + item.fixedSize;
}

Expand Down
20 changes: 6 additions & 14 deletions packages/errors/src/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,12 @@ export const SOLANA_ERROR__CODECS_EXPECTED_VARIABLE_LENGTH_GOT_FIXED_LENGTH = 80
export const SOLANA_ERROR__CODECS_ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH = 8078004 as const;
export const SOLANA_ERROR__CODECS_FIXED_SIZE_ENCODER_DECODER_SIZE_MISMATCH = 8078005 as const;
export const SOLANA_ERROR__CODECS_VARIABLE_SIZE_ENCODER_DECODER_MAX_SIZE_MISMATCH = 8078006 as const;
export const SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE = 8078007 as const;
export const SOLANA_ERROR__CODECS_WRONG_NUMBER_OF_ITEMS = 8078008 as const;
export const SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE = 8078009 as const;
export const SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT = 8078010 as const;
export const SOLANA_ERROR__CODECS_INVALID_SCALAR_ENUM_VARIANT = 8078011 as const;
export const SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC = 8078012 as const;
export const SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX = 8078013 as const;
export const SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE = 8078014 as const;
export const SOLANA_ERROR__CODECS_NUMBER_OUT_OF_RANGE = 8078015 as const;
export const SOLANA_ERROR__CODECS_INVALID_STRING_FOR_BASE = 8078016 as const;
export const SOLANA_ERROR__CODECS_WRONG_NUMBER_OF_ITEMS = 8078007 as const;
export const SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE = 8078008 as const;
export const SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT = 8078009 as const;
export const SOLANA_ERROR__CODECS_INVALID_SCALAR_ENUM_VARIANT = 8078010 as const;
export const SOLANA_ERROR__CODECS_NUMBER_OUT_OF_RANGE = 8078011 as const;
export const SOLANA_ERROR__CODECS_INVALID_STRING_FOR_BASE = 8078012 as const;
// Reserve subscription-related error codes in the range [8160000-8160999]
export const SOLANA_ERROR__RPC_SUBSCRIPTIONS_CANNOT_CREATE_SUBSCRIPTION_REQUEST = 8190000 as const;
export const SOLANA_ERROR__RPC_SUBSCRIPTIONS_EXPECTED_SERVER_SUBSCRIPTION_ID = 8190001 as const;
Expand Down Expand Up @@ -254,14 +250,10 @@ export type SolanaErrorCode =
| typeof SOLANA_ERROR__CODECS_ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH
| typeof SOLANA_ERROR__CODECS_FIXED_SIZE_ENCODER_DECODER_SIZE_MISMATCH
| typeof SOLANA_ERROR__CODECS_VARIABLE_SIZE_ENCODER_DECODER_MAX_SIZE_MISMATCH
| typeof SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE
| typeof SOLANA_ERROR__CODECS_WRONG_NUMBER_OF_ITEMS
| typeof SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE
| typeof SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT
| typeof SOLANA_ERROR__CODECS_INVALID_SCALAR_ENUM_VARIANT
| typeof SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC
| typeof SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX
| typeof SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE
| typeof SOLANA_ERROR__CODECS_INVALID_STRING_FOR_BASE
| typeof SOLANA_ERROR__CODECS_NUMBER_OUT_OF_RANGE
| typeof SOLANA_ERROR__SIGNER_ADDRESS_CANNOT_HAVE_MULTIPLE_SIGNERS
Expand Down
4 changes: 0 additions & 4 deletions packages/errors/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SOLANA_ERROR__BLOCKHASH_BYTE_LENGTH_OUT_OF_RANGE,
SOLANA_ERROR__BLOCKHASH_STRING_LENGTH_OUT_OF_RANGE,
SOLANA_ERROR__CODECS_CANNOT_DECODE_EMPTY_BYTE_ARRAY,
SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE,
SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE,
SOLANA_ERROR__CODECS_FIXED_SIZE_ENCODER_DECODER_SIZE_MISMATCH,
SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT,
Expand Down Expand Up @@ -210,9 +209,6 @@ export type SolanaErrorContext = DefaultUnspecifiedErrorContextToUndefined<
[SOLANA_ERROR__CODECS_CANNOT_DECODE_EMPTY_BYTE_ARRAY]: {
codecDescription: string;
};
[SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE]: {
codecDescription: string;
};
[SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE]: {
discriminator: bigint | number;
maxRange: number;
Expand Down
10 changes: 0 additions & 10 deletions packages/errors/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import {
SOLANA_ERROR__BLOCKHASH_BYTE_LENGTH_OUT_OF_RANGE,
SOLANA_ERROR__BLOCKHASH_STRING_LENGTH_OUT_OF_RANGE,
SOLANA_ERROR__CODECS_CANNOT_DECODE_EMPTY_BYTE_ARRAY,
SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE,
SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE,
SOLANA_ERROR__CODECS_ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH,
SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE,
SOLANA_ERROR__CODECS_EXPECTED_FIXED_LENGTH_GOT_VARIABLE_LENGTH,
SOLANA_ERROR__CODECS_EXPECTED_VARIABLE_LENGTH_GOT_FIXED_LENGTH,
SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC,
SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX,
SOLANA_ERROR__CODECS_FIXED_SIZE_ENCODER_DECODER_SIZE_MISMATCH,
SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT,
SOLANA_ERROR__CODECS_INVALID_SCALAR_ENUM_VARIANT,
Expand Down Expand Up @@ -205,8 +201,6 @@ export const SolanaErrorMessages: Readonly<{
[SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED]:
'The network has progressed past the last block for which this transaction could have been committed.',
[SOLANA_ERROR__CODECS_CANNOT_DECODE_EMPTY_BYTE_ARRAY]: 'Codec [$codecDescription] cannot decode empty byte arrays.',
[SOLANA_ERROR__CODECS_CANNOT_REVERSE_CODEC_OF_VARIABLE_SIZE]: 'Cannot reverse a codec of variable size.',
[SOLANA_ERROR__CODECS_CODEC_REQUIRES_FIXED_SIZE]: 'Codec [$codecDescription] requires a fixed size.',
[SOLANA_ERROR__CODECS_ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH]:
'Encoder and decoder must either both be fixed-size or variable-size.',
[SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE]:
Expand All @@ -215,10 +209,6 @@ export const SolanaErrorMessages: Readonly<{
'Expected a fixed-size codec, got a variable-size one.',
[SOLANA_ERROR__CODECS_EXPECTED_VARIABLE_LENGTH_GOT_FIXED_LENGTH]:
'Expected a variable-size codec, got a fixed-size one.',
[SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_CODEC]:
'Fixed nullables can only be used with fixed-size codecs.',
[SOLANA_ERROR__CODECS_FIXED_NULLABLE_WITH_VARIABLE_SIZE_PREFIX]:
'Fixed nullables can only be used with fixed-size prefix.',
[SOLANA_ERROR__CODECS_FIXED_SIZE_ENCODER_DECODER_SIZE_MISMATCH]:
'Encoder and decoder must have the same fixed size, got [$encoderFixedSize] and [$decoderFixedSize].',
[SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT]:
Expand Down
28 changes: 4 additions & 24 deletions packages/options/src/option-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,8 @@ export function getOptionEncoder<TFrom>(

const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
try {
assertIsFixedSize(item);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size codecs.');
}
try {
assertIsFixedSize(prefix);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size prefix.');
}
assertIsFixedSize(item);
assertIsFixedSize(prefix);
const fixedSize = prefix.fixedSize + item.fixedSize;
return createEncoder({
fixedSize,
Expand Down Expand Up @@ -149,18 +139,8 @@ export function getOptionDecoder<TTo>(
let fixedSize: number | null = null;
const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
try {
assertIsFixedSize(item);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size codecs.');
}
try {
assertIsFixedSize(prefix);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size prefix.');
}
assertIsFixedSize(item);
assertIsFixedSize(prefix);
fixedSize = prefix.fixedSize + item.fixedSize;
}

Expand Down
Loading