export const UNKNOWN_ERROR = 'UNKNOWN_ERROR' ; export const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED' ; export const MISSING_NEW = 'MISSING_NEW' ; export const CALL_EXCEPTION = 'CALL_EXCEPTION' ; export const INVALID_ARGUMENT = 'INVALID_ARGUMENT' ; export const MISSING_ARGUMENT = 'MISSING_ARGUMENT' ; export const UNEXPECTED_ARGUMENT = 'UNEXPECTED_ARGUMENT' ; export const NUMERIC_FAULT = 'NUMERIC_FAULT' ; export const INSUFFICIENT_FUNDS = 'INSUFFICIENT_FUNDS' ; export const NONCE_EXPIRED = 'NONCE_EXPIRED' ; export const REPLACEMENT_UNDERPRICED = 'REPLACEMENT_UNDERPRICED' ; export const UNSUPPORTED_OPERATION = 'UNSUPPORTED_OPERATION' ; export function InvalidNumberOfSolidityArgs ( given , expected ) { return new Error ( `Invalid number of arguments to Solidity function. given: ${ given }, expected: ${ expected }` ) ; } export function InvalidNumberOfRPCParams ( methodName , given , expected ) { return new Error ( `Invalid number of input parameters to RPC method "${ methodName }" given: ${ given }, expected: ${ expected }` ) ; } export function InvalidConnection ( host ) { return new Error ( "CONNECTION ERROR: Couldn't connect to node " + host + '.' ) ; } export function InvalidProvider ( ) { return new Error ( 'Provider not set or invalid' ) ; } export function InvalidResponse ( result ) { let message = ! ! result && ! ! result . error && ! ! result . error . message ? result . error . message : 'Invalid JSON RPC response: ' + JSON . stringify ( result ) ; return new Error ( message ) ; } export function ConnectionTimeout ( ms ) { return new Error ( 'CONNECTION TIMEOUT: timeout of ' + ms + ' ms achived' ) ; } let _permanentCensorErrors = false ; let _censorErrors = false ; export function createError ( message , code = UNKNOWN_ERROR , params = { } ) { if ( _censorErrors ) { return new Error ( 'unknown error' ) ; } let messageDetails = [ ] ; Object . keys ( params ) . forEach ( ( key ) => { try { messageDetails . push ( key + '=' + JSON . stringify ( params [ key ] ) ) ; } catch ( error ) { messageDetails . push ( key + '=' + JSON . stringify ( params [ key ] . toString ( ) ) ) ; } } ) ; let reason = message ; if ( messageDetails . length ) { message += ' (' + messageDetails . join ( ', ' ) + ')' ; } let error = new Error ( message ) ; error . reason = reason ; error . code = code ; Object . keys ( params ) . forEach ( function ( key ) { error [ key ] = params [ key ] ; } ) ; return error ; } export function checkNew ( self , kind ) { if ( ! ( self instanceof kind ) ) { throw createError ( 'missing new' , MISSING_NEW , { name : kind . name } ) ; } } export function checkArgumentCount ( count , expectedCount , suffix ? ) { if ( ! suffix ) { suffix = '' ; } if ( count < expectedCount ) { throw createError ( 'missing argument' + suffix , MISSING_ARGUMENT , { count : count , expectedCount : expectedCount } ) ; } if ( count > expectedCount ) { throw createError ( 'too many arguments' + suffix , UNEXPECTED_ARGUMENT , { count : count , expectedCount : expectedCount } ) ; } } export function setCensorship ( censorship , permanent ? ) { if ( _permanentCensorErrors ) { throw createError ( 'error censorship permanent' , UNSUPPORTED_OPERATION , { operation : 'setCersorship' } ) ; } _censorErrors = ! ! censorship ; _permanentCensorErrors = ! ! permanent ; }