mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +01:00
Add new json-rpc-engine middleware for improved error handling
This commit is contained in:
parent
6bd1b21d3b
commit
ce2834400c
@ -10,6 +10,7 @@
|
|||||||
- Fix bug that prevents setting language locale in settings.
|
- Fix bug that prevents setting language locale in settings.
|
||||||
- Show checksum addresses throughout the UI
|
- Show checksum addresses throughout the UI
|
||||||
- Allow transactions with a 0 gwei gas price
|
- Allow transactions with a 0 gwei gas price
|
||||||
|
- Made provider RPC errors contain useful messages
|
||||||
|
|
||||||
## 4.5.5 Fri Apr 06 2018
|
## 4.5.5 Fri Apr 06 2018
|
||||||
|
|
||||||
|
66
app/scripts/lib/createErrorMiddleware.js
Normal file
66
app/scripts/lib/createErrorMiddleware.js
Normal file
@ -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
|
@ -1,5 +1,6 @@
|
|||||||
const pump = require('pump')
|
const pump = require('pump')
|
||||||
const RpcEngine = require('json-rpc-engine')
|
const RpcEngine = require('json-rpc-engine')
|
||||||
|
const createErrorMiddleware = require('./createErrorMiddleware')
|
||||||
const createIdRemapMiddleware = require('json-rpc-engine/src/idRemapMiddleware')
|
const createIdRemapMiddleware = require('json-rpc-engine/src/idRemapMiddleware')
|
||||||
const createStreamMiddleware = require('json-rpc-middleware-stream')
|
const createStreamMiddleware = require('json-rpc-middleware-stream')
|
||||||
const LocalStorageStore = require('obs-store')
|
const LocalStorageStore = require('obs-store')
|
||||||
@ -44,6 +45,7 @@ function MetamaskInpageProvider (connectionStream) {
|
|||||||
// handle sendAsync requests via dapp-side rpc engine
|
// handle sendAsync requests via dapp-side rpc engine
|
||||||
const rpcEngine = new RpcEngine()
|
const rpcEngine = new RpcEngine()
|
||||||
rpcEngine.push(createIdRemapMiddleware())
|
rpcEngine.push(createIdRemapMiddleware())
|
||||||
|
rpcEngine.push(createErrorMiddleware())
|
||||||
rpcEngine.push(streamMiddleware)
|
rpcEngine.push(streamMiddleware)
|
||||||
self.rpcEngine = rpcEngine
|
self.rpcEngine = rpcEngine
|
||||||
}
|
}
|
||||||
|
1533
package-lock.json
generated
1533
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user