Skip to content

Commit

Permalink
refactoring: Consistent new on Error creation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Mar 18, 2021
1 parent 7f0a6fe commit 39f93d3
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions es/account/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default AccountBase.compose(AsyncInit, {
*/
selectAccount (address) {
assertedType(address, 'ak')
if (!this.accounts[address]) throw Error(`Account for ${address} not available`)
if (!this.accounts[address]) throw new Error(`Account for ${address} not available`)
this.selectedAddress = address
},
/**
Expand All @@ -143,7 +143,7 @@ export default AccountBase.compose(AsyncInit, {
switch (typeof account) {
case 'string':
assertedType(account, 'ak')
if (!this.accounts[account]) throw Error(`Account for ${account} not available`)
if (!this.accounts[account]) throw new Error(`Account for ${account} not available`)
return this.accounts[account]
case 'object':
return isAccountBase(account) ? account : MemoryAccount({ keypair: account })
Expand Down
2 changes: 1 addition & 1 deletion es/ae/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function pollForQueryResponse (oracleId, queryId, { attempts = 20,
return { response, decode: () => responseBuffer } // TODO: Return just responseBuffer
}
}
throw Error(`Giving up after ${(attempts - 1) * interval}ms`)
throw new Error(`Giving up after ${(attempts - 1) * interval}ms`)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions es/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function sendTransaction (tx, options = {}) {
if (verify || (typeof verify !== 'boolean' && this.verifyTxBeforeSend)) {
const { validation, tx: txObject, txType } = await this.unpackAndVerify(tx)
if (validation.length) {
throw Object.assign(Error('Transaction verification error: ' + JSON.stringify(validation)), {
throw Object.assign(new Error('Transaction verification error: ' + JSON.stringify(validation)), {
code: 'TX_VERIFICATION_ERROR',
errorData: { validation, tx: txObject, txType },
txHash: tx
Expand Down Expand Up @@ -121,7 +121,7 @@ async function awaitHeight (height, { interval = 5000, attempts = 20 } = {}) {
currentHeight = await this.height()
if (currentHeight >= height) return currentHeight
}
throw Error(`Giving up after ${(attempts - 1) * interval}ms, current height: ${currentHeight}, desired height: ${height}`)
throw new Error(`Giving up after ${(attempts - 1) * interval}ms, current height: ${currentHeight}, desired height: ${height}`)
}

async function topBlock () {
Expand Down
2 changes: 1 addition & 1 deletion es/channel/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function appendSignature (tx, signFn) {
function handleUnexpectedMessage (channel, message, state) {
if (state && state.reject) {
state.reject(Object.assign(
Error(`Unexpected message received:\n\n${JSON.stringify(message)}`),
new Error(`Unexpected message received:\n\n${JSON.stringify(message)}`),
{ wsMessage: message }
))
}
Expand Down
6 changes: 3 additions & 3 deletions es/channel/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function ping (channel) {
})
pongTimeoutId.set(channel, setTimeout(() => {
disconnect(channel)
emit(channel, 'error', Error('Server pong timed out'))
emit(channel, 'error', new Error('Server pong timed out'))
}, PONG_TIMEOUT_MS))
}, PING_TIMEOUT_MS))
}
Expand Down Expand Up @@ -169,9 +169,9 @@ function onMessage (channel, data) {
function wrapCallErrorMessage (message) {
const [{ message: details } = {}] = message.error.data || []
if (details) {
return Error(`${message.error.message}: ${details}`)
return new Error(`${message.error.message}: ${details}`)
}
return Error(message.error.message)
return new Error(message.error.message)
}

export function call (channel, method, params) {
Expand Down
2 changes: 1 addition & 1 deletion es/contract/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function prepareCompilerOption ({ backend = this.compilerOptions.backend, filesy
}

function isInit () {
if (this.compilerVersion === null) throw Error('Compiler not defined')
if (this.compilerVersion === null) throw new Error('Compiler not defined')
return true
}

Expand Down
2 changes: 1 addition & 1 deletion es/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const loader = ({ url, internalUrl }) => (path, definition) => {
} else if (!R.isNil(internalUrl) && R.contains('internal', tags)) {
return `${internalUrl}${path}`
} else {
throw Error(`Method ${operationId} is unsupported. No interface for ${R.toString(tags)}`)
throw new Error(`Method ${operationId} is unsupported. No interface for ${R.toString(tags)}`)
}
}

Expand Down
4 changes: 2 additions & 2 deletions es/tx/builder/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ export function isNameValid (name, throwError = true) {
*/
export function classify (s) {
if (!s.match(/^[a-z]{2}_.+/)) {
throw Error('Not a valid hash')
throw new Error('Not a valid hash')
}

const klass = s.substr(0, 2)
if (klass in NAME_ID_KEY) {
return NAME_ID_KEY[klass]
} else {
throw Error(`Unknown class ${klass}`)
throw new Error(`Unknown class ${klass}`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion es/utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export function decryptPubKey (password, encrypted) {
export function assertedType (data, type, omitError) {
if (RegExp(`^${type}_.+$`).test(data)) return data.split('_')[1]
else if (omitError) return false
else throw Error(`Data doesn't match expected type ${type}`)
else throw new Error(`Data doesn't match expected type ${type}`)
}

/**
Expand Down
18 changes: 9 additions & 9 deletions es/utils/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ function lookupType (path, spec, types) {
if (match !== undefined) {
return match[1]
} else {
throw Error(`Reference path does not meet specification: ${path}`)
throw new Error(`Reference path does not meet specification: ${path}`)
}
})()

if (type in types) {
return types[type]
} else {
throw Error(`Couldn't find definition for ${type}`)
throw new Error(`Couldn't find definition for ${type}`)
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ function extendingErrorPath (key, fn) {
* @return {Error} Enhanced Error
*/
function TypeError (msg, spec, value) {
const e = Error(msg)
const e = new Error(msg)
return Object.assign(e, { spec, value })
}

Expand Down Expand Up @@ -175,7 +175,7 @@ function conformDispatch (spec) {
} else if ('type' in spec) {
return spec.type
} else {
throw Object.assign(Error('Could not determine type'), { spec })
throw Object.assign(new Error('Could not determine type'), { spec })
}
}

Expand All @@ -190,7 +190,7 @@ function conformDispatch (spec) {
*/
function conform (value, spec, types) {
return (conformTypes[conformDispatch(spec)] || (() => {
throw Object.assign(Error('Unsupported type'), { spec })
throw Object.assign(new Error('Unsupported type'), { spec })
}))(value, spec, types)
}

Expand Down Expand Up @@ -318,7 +318,7 @@ function assertOne (coll) {
if (coll.length === 1) {
return R.head(coll)
} else {
throw Error(`Expected exactly one element in ${coll}`)
throw new Error(`Expected exactly one element in ${coll}`)
}
}

Expand Down Expand Up @@ -352,7 +352,7 @@ function resolveRef (ref, swag) {
}
}

throw Error(`Could not resolve reference: ${ref}`)
throw new Error(`Could not resolve reference: ${ref}`)
}

/**
Expand Down Expand Up @@ -394,7 +394,7 @@ const operation = (path, method, definition, swag, { config, errorHandler } = {}
} else if (arguments.length === req.length + 1) {
return [R.dropLast(1, arguments), R.merge(defaults, R.last(arguments))]
} else {
throw Error(`Function call doesn't conform to ${signature}`)
throw new Error(`Function call doesn't conform to ${signature}`)
}
})()

Expand Down Expand Up @@ -422,7 +422,7 @@ const operation = (path, method, definition, swag, { config, errorHandler } = {}
} else if (method === 'post') {
return conformed[assertOne(bodyArgs)]
} else {
throw Error(`Unsupported method ${method}`)
throw new Error(`Unsupported method ${method}`)
}
})())

Expand Down
4 changes: 2 additions & 2 deletions examples/node/aecrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function generateKeyPair (name, { output }) {
// transaction and turn it into an RLP-encoded tuple ready for mining
function signTx (tx, privKey) {
if (!tx.match(/^tx_.+/)) {
throw Error('Not a valid transaction')
throw new Error('Not a valid transaction')
}

const binaryKey = (() => {
Expand All @@ -95,7 +95,7 @@ function signTx (tx, privKey) {
} else if (privKey) {
return Buffer.from(privKey, 'hex')
} else {
throw Error('Must provide either [privkey] or [file]')
throw new Error('Must provide either [privkey] or [file]')
}
})()

Expand Down

0 comments on commit 39f93d3

Please sign in to comment.