aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDan Finlay <542863+danfinlay@users.noreply.github.com>2018-05-19 02:57:29 +0800
committerGitHub <noreply@github.com>2018-05-19 02:57:29 +0800
commit713c77db54e7866da169205d7f665c33a4c626e0 (patch)
treed4cda5bbefc1c83d942c4fac56a2d19047baf77d /app
parent708422432c634ffbd4c73388f980c43f766b3355 (diff)
parentce2834400ca202d5767c3d896407ac565fadfc14 (diff)
downloadtangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.tar
tangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.tar.gz
tangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.tar.bz2
tangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.tar.lz
tangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.tar.xz
tangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.tar.zst
tangerine-wallet-browser-713c77db54e7866da169205d7f665c33a4c626e0.zip
Merge pull request #4199 from MetaMask/i3487-provider-errors
RPC error middleware for json-rpc-engine
Diffstat (limited to 'app')
-rw-r--r--app/scripts/lib/createErrorMiddleware.js66
-rw-r--r--app/scripts/lib/inpage-provider.js2
2 files changed, 68 insertions, 0 deletions
diff --git a/app/scripts/lib/createErrorMiddleware.js b/app/scripts/lib/createErrorMiddleware.js
new file mode 100644
index 000000000..baed99e45
--- /dev/null
+++ b/app/scripts/lib/createErrorMiddleware.js
@@ -0,0 +1,66 @@
+const log = require('loglevel')
+
+/**
+ * JSON-RPC error object
+ *
+ * @typedef {Object} RpcError
+ * @property {number} code - Indicates the error type that occurred
+ * @property {Object} [data] - Contains additional information about the error
+ * @property {string} [message] - Short description of the error
+ */
+
+/**
+ * Middleware configuration object
+ *
+ * @typedef {Object} MiddlewareConfig
+ * @property {boolean} [override] - Use RPC_ERRORS message in place of provider message
+ */
+
+/**
+ * Map of standard and non-standard RPC error codes to messages
+ */
+const RPC_ERRORS = {
+ 1: 'An unauthorized action was attempted.',
+ 2: 'A disallowed action was attempted.',
+ 3: 'An execution error occurred.',
+ [-32600]: 'The JSON sent is not a valid Request object.',
+ [-32601]: 'The method does not exist / is not available.',
+ [-32602]: 'Invalid method parameter(s).',
+ [-32603]: 'Internal JSON-RPC error.',
+ [-32700]: 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
+ internal: 'Internal server error.',
+ unknown: 'Unknown JSON-RPC error.',
+}
+
+/**
+ * Modifies a JSON-RPC error object in-place to add a human-readable message,
+ * optionally overriding any provider-supplied message
+ *
+ * @param {RpcError} error - JSON-RPC error object
+ * @param {boolean} override - Use RPC_ERRORS message in place of provider message
+ */
+function sanitizeRPCError (error, override) {
+ if (error.message && !override) { return error }
+ const message = error.code > -31099 && error.code < -32100 ? RPC_ERRORS.internal : RPC_ERRORS[error.code]
+ error.message = message || RPC_ERRORS.unknown
+}
+
+/**
+ * json-rpc-engine middleware that both logs standard and non-standard error
+ * messages and ends middleware stack traversal if an error is encountered
+ *
+ * @param {MiddlewareConfig} [config={override:true}] - Middleware configuration
+ * @returns {Function} json-rpc-engine middleware function
+ */
+function createErrorMiddleware ({ override = true } = {}) {
+ return (req, res, next) => {
+ next(done => {
+ const { error } = res
+ if (!error) { return done() }
+ sanitizeRPCError(error)
+ log.error(`MetaMask - RPC Error: ${error.message}`, error)
+ })
+ }
+}
+
+module.exports = createErrorMiddleware \ No newline at end of file
diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js
index 99cc5d2cf..4e65f0a23 100644
--- a/app/scripts/lib/inpage-provider.js
+++ b/app/scripts/lib/inpage-provider.js
@@ -1,5 +1,6 @@
const pump = require('pump')
const RpcEngine = require('json-rpc-engine')
+const createErrorMiddleware = require('./createErrorMiddleware')
const createIdRemapMiddleware = require('json-rpc-engine/src/idRemapMiddleware')
const createStreamMiddleware = require('json-rpc-middleware-stream')
const LocalStorageStore = require('obs-store')
@@ -44,6 +45,7 @@ function MetamaskInpageProvider (connectionStream) {
// handle sendAsync requests via dapp-side rpc engine
const rpcEngine = new RpcEngine()
rpcEngine.push(createIdRemapMiddleware())
+ rpcEngine.push(createErrorMiddleware())
rpcEngine.push(streamMiddleware)
self.rpcEngine = rpcEngine
}