Skip to content

Commit bff28f2

Browse files
chore: optimize error (#624)
1 parent ff34fa6 commit bff28f2

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

packages/core/src/api/cosmos/CosmosSignTransaction.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { CosmosSignTx as HardwareCosmosSignTx } from '@onekeyfe/hd-transport';
1+
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2+
23
import { serializedPath, validatePath } from '../helpers/pathUtils';
34
import { BaseMethod } from '../BaseMethod';
45
import { validateParams } from '../helpers/paramsValidator';
5-
import { CosmosSignTransactionParams } from '../../types';
66
import { formatAnyHex } from '../helpers/hexUtils';
77

8+
import type { CosmosSignTx as HardwareCosmosSignTx } from '@onekeyfe/hd-transport';
9+
import type { CosmosSignTransactionParams } from '../../types';
10+
811
export default class CosmosSignTransaction extends BaseMethod<HardwareCosmosSignTx> {
912
hasBundle = false;
1013

@@ -40,15 +43,26 @@ export default class CosmosSignTransaction extends BaseMethod<HardwareCosmosSign
4043
}
4144

4245
async run() {
43-
const res = await this.device.commands.typedCall('CosmosSignTx', 'CosmosSignedTx', {
44-
...this.params,
45-
});
46+
try {
47+
const res = await this.device.commands.typedCall('CosmosSignTx', 'CosmosSignedTx', {
48+
...this.params,
49+
});
4650

47-
const { signature } = res.message;
51+
const { signature } = res.message;
4852

49-
return {
50-
path: serializedPath(this.params.address_n),
51-
signature,
52-
};
53+
return {
54+
path: serializedPath(this.params.address_n),
55+
signature,
56+
};
57+
} catch (error) {
58+
const { message } = error;
59+
if (
60+
message.includes('Failure_DataError,Json parse failed') ||
61+
message.includes('Failure_DataError,Invalid message')
62+
) {
63+
throw ERRORS.TypedError(HardwareErrorCode.CosmosInvalidJsonMessage, message);
64+
}
65+
throw error;
66+
}
5367
}
5468
}

packages/core/src/device/DeviceCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ export class DeviceCommands {
441441
ERRORS.TypedError(
442442
HardwareErrorCode.RuntimeError,
443443
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
444-
`${(code as any) || 'Failure_UnknownCode'},${message || 'Failure_UnknownMessage'}`
444+
`${(code as any) || 'Failure_UnknownCode'},${message || 'no error message'}`
445445
)
446446
);
447447
}

packages/shared/src/HardwareError.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,14 @@ export const HardwareErrorCode = {
455455
BridgeNeedsPermission: 821,
456456

457457
CallQueueActionCancelled: 822,
458-
459458
/**
460459
* Firmware downgrade not allowed
461460
*/
462461
FirmwareDowngradeNotAllowed: 823,
462+
/**
463+
* Cosmos invalid JSON message, Only support Amino msgs.
464+
*/
465+
CosmosInvalidJsonMessage: 824,
463466

464467
/**
465468
* Lowlevel transport connect error
@@ -606,6 +609,8 @@ export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
606609
[HardwareErrorCode.FirmwareVerificationFailed]: 'Firmware verification failed',
607610
[HardwareErrorCode.BridgeNeedsPermission]: 'Bridge needs permission',
608611
[HardwareErrorCode.FirmwareDowngradeNotAllowed]: 'Firmware downgrade not allowed',
612+
[HardwareErrorCode.CosmosInvalidJsonMessage]:
613+
'Cosmos invalid JSON message, Only support Amino msgs.',
609614

610615
/**
611616
* Lowlevel transport

0 commit comments

Comments
 (0)