@@ -4,10 +4,8 @@ import {
44 ProgramNode ,
55 structTypeNodeFromInstructionArgumentNodes ,
66} from '@codama/nodes' ;
7- import { mapFragmentContent } from '@codama/renderers-core' ;
8- import { pipe } from '@codama/visitors-core' ;
97
10- import { addFragmentImports , Fragment , fragment , mergeFragments , RenderScope , use } from '../utils' ;
8+ import { Fragment , fragment , mergeFragments , RenderScope , use } from '../utils' ;
119import { getDiscriminatorConditionFragment } from './discriminatorCondition' ;
1210
1311export function getProgramInstructionsFragment (
@@ -76,22 +74,15 @@ function getProgramInstructionsIdentifierFunctionFragment(
7674 c => c . join ( '\n' ) ,
7775 ) ;
7876
79- return pipe (
80- discriminatorsFragment ,
81- f =>
82- mapFragmentContent (
83- f ,
84- discriminators =>
85- `export function ${ programInstructionsIdentifierFunction } (` +
86- `instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array` +
87- `): ${ programInstructionsEnum } {\n` +
88- `const data = 'data' in instruction ? instruction.data : instruction;\n` +
89- `${ discriminators } \n` +
90- `throw new Error("The provided instruction could not be identified as a ${ programNode . name } instruction.")\n` +
91- `}` ,
92- ) ,
93- f => addFragmentImports ( f , 'solanaCodecsCore' , [ 'type ReadonlyUint8Array' ] ) ,
94- ) ;
77+ const readonlyUint8Array = use ( 'type ReadonlyUint8Array' , 'solanaCodecsCore' ) ;
78+ const solanaError = use ( 'SolanaError' , 'solanaErrors' ) ;
79+ const solanaErrorCode = use ( 'SOLANA_ERROR__PROGRAM_CLIENTS__FAILED_TO_IDENTIFY_INSTRUCTION' , 'solanaErrors' ) ;
80+
81+ return fragment `export function ${ programInstructionsIdentifierFunction } (instruction: { data: ${ readonlyUint8Array } } | ${ readonlyUint8Array } ): ${ programInstructionsEnum } {
82+ const data = 'data' in instruction ? instruction.data : instruction;
83+ ${ discriminatorsFragment }
84+ throw new ${ solanaError } (${ solanaErrorCode } , { instructionData: data, programName: "${ programNode . name } " });
85+ }` ;
9586}
9687
9788function getProgramInstructionsParsedUnionTypeFragment (
@@ -159,6 +150,9 @@ function getProgramInstructionsParseFunctionFragment(
159150 c => c . join ( '\n' ) ,
160151 ) ;
161152
153+ const solanaError = use ( 'SolanaError' , 'solanaErrors' ) ;
154+ const solanaErrorCode = use ( 'SOLANA_ERROR__PROGRAM_CLIENTS__UNRECOGNIZED_INSTRUCTION_TYPE' , 'solanaErrors' ) ;
155+
162156 return fragment `
163157 export function ${ parseFunction } <TProgram extends string>(
164158 instruction: ${ use ( 'type Instruction' , 'solanaInstructions' ) } <TProgram>
@@ -167,7 +161,7 @@ function getProgramInstructionsParseFunctionFragment(
167161 const instructionType = ${ programInstructionsIdentifierFunction } (instruction);
168162 switch (instructionType) {
169163 ${ switchCases }
170- default: throw new Error(\`Unrecognized instruction type: \${ instructionType as string}\` );
164+ default: throw new ${ solanaError } ( ${ solanaErrorCode } , { instructionType: instructionType as string, programName: " ${ programNode . name } " } );
171165 }
172166 }` ;
173167}
0 commit comments