From 3f4837c7c5a35ff5d1a27ee49f8825dbd9305240 Mon Sep 17 00:00:00 2001 From: William O'Beirne Date: Mon, 29 Jan 2018 17:17:58 -0500 Subject: [PATCH] Fix metamask reject error (#957) --- common/libs/nodes/web3/index.ts | 2 +- common/libs/validators.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/common/libs/nodes/web3/index.ts b/common/libs/nodes/web3/index.ts index f0d1a241..6540eeaf 100644 --- a/common/libs/nodes/web3/index.ts +++ b/common/libs/nodes/web3/index.ts @@ -9,7 +9,7 @@ import { isValidSignMessage, isValidGetAccounts, isValidGetNetVersion -} from '../../validators'; +} from 'libs/validators'; export default class Web3Node extends RPCNode { public client: Web3Client; diff --git a/common/libs/validators.ts b/common/libs/validators.ts index 68b1fb56..66b85812 100644 --- a/common/libs/validators.ts +++ b/common/libs/validators.ts @@ -192,7 +192,16 @@ function isValidResult(response: JsonRpcResponse, schemaFormat): boolean { function formatErrors(response: JsonRpcResponse, apiType: string) { if (response.error) { - return `${response.error.message} ${response.error.data || ''}`; + // Metamask errors are sometimes full-blown stacktraces, no bueno. Instead, + // We'll just take the first line of it, and the last thing after all of + // the colons. An example error message would be: + // "Error: Metamask Sign Tx Error: User rejected the signature." + const lines = response.error.message.split('\n'); + if (lines.length > 2) { + return lines[0].split(':').pop(); + } else { + return `${response.error.message} ${response.error.data || ''}`; + } } return `Invalid ${apiType} Error`; }