11import { abciQuery } from './rpc' ;
2+ import { ProtocolError , AbciError } from '@interchainjs/types' ;
23
34// Mock fetch globally
45const mockFetch = jest . fn ( ) ;
@@ -13,17 +14,18 @@ describe('abciQuery', () => {
1314 mockFetch . mockReset ( ) ;
1415 } ) ;
1516
16- it ( 'should throw on JSON-RPC error' , async ( ) => {
17+ it ( 'should throw ProtocolError on JSON-RPC error' , async ( ) => {
1718 mockFetch . mockResolvedValue ( {
1819 json : ( ) => Promise . resolve ( {
19- error : 'Internal error' ,
20+ error : { code : - 32600 , message : 'Invalid Request' } ,
2021 } ) ,
2122 } ) ;
2223
23- await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( 'Request Error: Internal error' ) ;
24+ await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( ProtocolError ) ;
25+ await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( 'JSON-RPC error' ) ;
2426 } ) ;
2527
26- it ( 'should throw on non-zero ABCI response code' , async ( ) => {
28+ it ( 'should throw AbciError on non-zero ABCI response code' , async ( ) => {
2729 mockFetch . mockResolvedValue ( {
2830 json : ( ) => Promise . resolve ( {
2931 result : {
@@ -36,12 +38,19 @@ describe('abciQuery', () => {
3638 } ) ,
3739 } ) ;
3840
39- await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow (
40- 'ABCI Error (code 11): out of gas in location: ReadFlat; gasWanted: 0, gasUsed: 1000'
41- ) ;
41+ await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( AbciError ) ;
42+
43+ try {
44+ await abciQuery ( endpoint , path , data ) ;
45+ } catch ( e ) {
46+ expect ( e ) . toBeInstanceOf ( AbciError ) ;
47+ const abciError = e as AbciError ;
48+ expect ( abciError . abciCode ) . toBe ( 11 ) ;
49+ expect ( abciError . log ) . toBe ( 'out of gas in location: ReadFlat; gasWanted: 0, gasUsed: 1000' ) ;
50+ }
4251 } ) ;
4352
44- it ( 'should throw with generic message when ABCI error has no log' , async ( ) => {
53+ it ( 'should throw AbciError with generic message when ABCI error has no log' , async ( ) => {
4554 mockFetch . mockResolvedValue ( {
4655 json : ( ) => Promise . resolve ( {
4756 result : {
@@ -54,7 +63,15 @@ describe('abciQuery', () => {
5463 } ) ,
5564 } ) ;
5665
57- await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( 'ABCI Error (code 5): Unknown error' ) ;
66+ await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( AbciError ) ;
67+
68+ try {
69+ await abciQuery ( endpoint , path , data ) ;
70+ } catch ( e ) {
71+ const abciError = e as AbciError ;
72+ expect ( abciError . abciCode ) . toBe ( 5 ) ;
73+ expect ( abciError . log ) . toBe ( 'Unknown error' ) ;
74+ }
5875 } ) ;
5976
6077 it ( 'should return decoded value on success (code 0)' , async ( ) => {
@@ -75,4 +92,20 @@ describe('abciQuery', () => {
7592 const result = await abciQuery ( endpoint , path , data ) ;
7693 expect ( result ) . toEqual ( new Uint8Array ( [ 4 , 5 , 6 ] ) ) ;
7794 } ) ;
95+
96+ it ( 'should preserve instanceof Error for backward compatibility' , async ( ) => {
97+ mockFetch . mockResolvedValue ( {
98+ json : ( ) => Promise . resolve ( {
99+ result : {
100+ response : {
101+ code : 11 ,
102+ log : 'test error' ,
103+ value : '' ,
104+ } ,
105+ } ,
106+ } ) ,
107+ } ) ;
108+
109+ await expect ( abciQuery ( endpoint , path , data ) ) . rejects . toThrow ( Error ) ;
110+ } ) ;
78111} ) ;
0 commit comments