Skip to content

Commit 006f0c3

Browse files
chore: fix error msg mapping
1 parent 3234421 commit 006f0c3

File tree

5 files changed

+91
-11
lines changed

5 files changed

+91
-11
lines changed

packages/exceptions/src/exceptions/base.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,20 @@ export abstract class ConcreteException extends Error implements Exception {
102102
}
103103

104104
public parseContext(errorContext?: ErrorContext) {
105-
const { contextModule, type, code, context } = errorContext || {
106-
contextModule: 'Unknown',
107-
context: 'Unknown',
108-
code: UnspecifiedErrorCode,
109-
type: ErrorType.Unspecified,
110-
}
105+
const { contextModule, type, code, context, contextCode } =
106+
errorContext || {
107+
contextModule: 'Unknown',
108+
context: 'Unknown',
109+
code: UnspecifiedErrorCode,
110+
type: ErrorType.Unspecified,
111+
contextCode: undefined,
112+
}
111113

112114
this.context = context
113115
this.contextModule = contextModule
114116
this.type = type || ErrorType.Unspecified
115117
this.code = code || UnspecifiedErrorCode
118+
this.contextCode = contextCode
116119
}
117120

118121
public setType(type: ErrorType) {

packages/exceptions/src/exceptions/messages.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ const exchangeErrorMap = {
288288
'Client order id already exists',
289289
[ChainExchangeModuleErrorCode.ErrInvalidCid]:
290290
'Client order id is invalid. Max length is 36 chars',
291+
[ChainExchangeModuleErrorCode.ErrInvalidClosingDirection]:
292+
'Invalid closing direction',
293+
[ChainExchangeModuleErrorCode.ErrInvalidNotional]: 'Invalid notional',
291294
}
292295

293296
const insuranceErrorMap = {
@@ -1445,6 +1448,12 @@ export const chainErrorMessagesMap: Record<
14451448
module: TransactionChainErrorModule.Exchange,
14461449
},
14471450

1451+
'invalid notional': {
1452+
message: 'Invalid notional',
1453+
code: ChainExchangeModuleErrorCode.ErrInvalidNotional,
1454+
module: TransactionChainErrorModule.Exchange,
1455+
},
1456+
14481457
'empty validator address': {
14491458
message: 'empty validator address',
14501459
code: ChainStakingErrorCodes.ErrEmptyValidatorAddr,

packages/exceptions/src/exceptions/types/codes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ export const ChainExchangeModuleErrorCode = {
332332
ErrClientOrderIdAlreadyExists: 97,
333333
// client order id is invalid. Max length is 36 chars
334334
ErrInvalidCid: 98,
335+
// invalid closing direction
336+
ErrInvalidClosingDirection: 99,
337+
// invalid notional
338+
ErrInvalidNotional: 100,
335339
} as const
336340

337341
export type ChainExchangeModuleErrorCode =

packages/exceptions/src/exceptions/utils/maps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ export const mapFailedTransactionMessage = (
130130
return reason
131131
}
132132

133-
const ABCICode = context && context.code ? context.code : getABCICode(message)
133+
const ABCICode =
134+
context && context.contextCode ? context.contextCode : getABCICode(message)
134135
const contextModule = context?.contextModule || getContextModule(message)
135136
const reason = getReason(message)
136137

packages/sdk-ts/src/client/base/BaseGrpcConsumer.ts

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { RpcError } from '@protobuf-ts/runtime-rpc'
22
import {
33
UnspecifiedErrorCode,
4+
TransactionException,
45
grpcErrorCodeToErrorCode,
56
GrpcUnaryRequestException,
67
} from '@injectivelabs/exceptions'
@@ -81,19 +82,81 @@ export default class BaseGrpcConsumer {
8182
}
8283

8384
/**
84-
* Centralized error handler for gRPC calls
85-
* Converts RpcError to GrpcUnaryRequestException with proper error codes
85+
* Extracts the ABCI error code from a gRPC error message.
86+
* Chain errors contain patterns like: {key:"ABCICode" value:"100"}
87+
*/
88+
private getABCICodeFromMessage(message: string): number | undefined {
89+
const ABCICodePattern = /{key:"ABCICode"[ \t]+value:"(.*?)"}/g
90+
const ABCICode = ABCICodePattern.exec(message)
91+
92+
if (!ABCICode || ABCICode.length < 2) {
93+
return undefined
94+
}
95+
96+
return Number(ABCICode[1])
97+
}
98+
99+
/**
100+
* Extracts the codespace/module from a gRPC error message.
101+
* Chain errors contain patterns like: {key:"Codespace" value:"exchange"}
102+
*/
103+
private getCodespaceFromMessage(message: string): string | undefined {
104+
const codespacePattern = /{key:"Codespace"[ \t]+value:"(.*?)"}/g
105+
const codespace = codespacePattern.exec(message)
106+
107+
if (!codespace || codespace.length < 2) {
108+
return undefined
109+
}
110+
111+
return codespace[1]
112+
}
113+
114+
/**
115+
* Centralized error handler for gRPC calls.
116+
* When the error contains chain error details (ABCI code and codespace),
117+
* throws a TransactionException which will map the error to a user-friendly message.
118+
* Otherwise throws a GrpcUnaryRequestException for generic gRPC errors.
86119
*/
87120
protected handleGrpcError(e: unknown, context: string): never {
88121
if (e instanceof RpcError) {
89-
throw new GrpcUnaryRequestException(new Error(e.message), {
122+
const message = e.message
123+
const abciCode = this.getABCICodeFromMessage(message)
124+
const codespace = this.getCodespaceFromMessage(message)
125+
126+
// If we have chain error details, throw TransactionException
127+
// which will map the error to a user-friendly message
128+
if (abciCode && codespace) {
129+
throw new TransactionException(new Error(message), {
130+
code: grpcErrorCodeToErrorCode(Number(e.code)),
131+
context,
132+
contextModule: codespace,
133+
contextCode: abciCode,
134+
})
135+
}
136+
137+
throw new GrpcUnaryRequestException(new Error(message), {
90138
code: grpcErrorCodeToErrorCode(Number(e.code)),
91139
context,
92140
contextModule: this.module,
93141
})
94142
}
95143

96-
throw new GrpcUnaryRequestException(e as Error, {
144+
const error = e as Error
145+
const message = error?.message || ''
146+
const abciCode = this.getABCICodeFromMessage(message)
147+
const codespace = this.getCodespaceFromMessage(message)
148+
149+
// If we have chain error details, throw TransactionException
150+
if (abciCode && codespace) {
151+
throw new TransactionException(error, {
152+
code: UnspecifiedErrorCode,
153+
context,
154+
contextModule: codespace,
155+
contextCode: abciCode,
156+
})
157+
}
158+
159+
throw new GrpcUnaryRequestException(error, {
97160
code: UnspecifiedErrorCode,
98161
context,
99162
contextModule: this.module,

0 commit comments

Comments
 (0)