Skip to content

Commit

Permalink
refactor: better v2 API error messages (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Aug 15, 2018
1 parent c3e0d5d commit 6f8986c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
6 changes: 5 additions & 1 deletion packages/core-api/lib/versions/2/handlers/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exports.show = {
async handler (request, h) {
const block = await database.blocks.findById(request.params.id)

if (!block) {
return Boom.notFound('Block not found')
}

return utils.respondWithResource(request, block, 'block')
},
options: {
Expand All @@ -56,7 +60,7 @@ exports.transactions = {
const block = await database.blocks.findById(request.params.id)

if (!block) {
return Boom.notFound()
return Boom.notFound('Block not found')
}

const transactions = await database.transactions.findAllByBlock(block.id, utils.paginate(request))
Expand Down
8 changes: 4 additions & 4 deletions packages/core-api/lib/versions/2/handlers/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports.show = {
const delegate = await database.delegates.findById(request.params.id)

if (!delegate) {
return Boom.notFound()
return Boom.notFound('Delegate not found')
}

return utils.respondWithResource(request, delegate, 'delegate')
Expand Down Expand Up @@ -84,7 +84,7 @@ exports.blocks = {
const delegate = await database.delegates.findById(request.params.id)

if (!delegate) {
return Boom.notFound()
return Boom.notFound('Delegate not found')
}

const blocks = await database.blocks.findAllByGenerator(delegate.publicKey, utils.paginate(request))
Expand All @@ -109,7 +109,7 @@ exports.voters = {
const delegate = await database.delegates.findById(request.params.id)

if (!delegate) {
return Boom.notFound()
return Boom.notFound('Delegate not found')
}

const wallets = await database.wallets.findAllByVote(delegate.publicKey, utils.paginate(request))
Expand All @@ -134,7 +134,7 @@ exports.voterBalances = {
const delegate = await database.delegates.findById(request.params.id)

if (!delegate) {
return Boom.notFound()
return Boom.notFound('Delegate not found')
}

const wallets = await database.wallets
Expand Down
8 changes: 7 additions & 1 deletion packages/core-api/lib/versions/2/handlers/peers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const Boom = require('boom')
const blockchain = require('@arkecosystem/core-container').resolvePlugin('blockchain')
const utils = require('../utils')
const schema = require('../schema/peers')
Expand Down Expand Up @@ -51,8 +52,13 @@ exports.show = {
*/
async handler (request, h) {
const peers = await blockchain.p2p.getPeers()
const peer = peers.find(p => p.ip === request.params.ip)

return utils.respondWithResource(request, peers.find(p => p.ip === request.params.ip), 'peer')
if (!peer) {
return Boom.notFound('Peer not found')
}

return utils.respondWithResource(request, peer, 'peer')
},
options: {
validate: schema.show
Expand Down
4 changes: 2 additions & 2 deletions packages/core-api/lib/versions/2/handlers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ exports.show = {
const transaction = await database.transactions.findById(request.params.id)

if (!transaction) {
return Boom.notFound()
return Boom.notFound('Transaction not found')
}

return utils.respondWithResource(request, transaction, 'transaction')
Expand Down Expand Up @@ -158,7 +158,7 @@ exports.showUnconfirmed = {
let transaction = await transactionPool.getTransaction(request.params.id)

if (!transaction) {
return Boom.notFound()
return Boom.notFound('Transaction not found')
}

transaction = { serialized: transaction.serialized.toString('hex') }
Expand Down
5 changes: 5 additions & 0 deletions packages/core-api/lib/versions/2/handlers/votes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const Boom = require('boom')
const { TRANSACTION_TYPES } = require('@arkecosystem/crypto').constants
const database = require('@arkecosystem/core-container').resolvePlugin('database')
const utils = require('../utils')
Expand Down Expand Up @@ -36,6 +37,10 @@ exports.show = {
async handler (request, h) {
const transaction = await database.transactions.findByTypeAndId(TRANSACTION_TYPES.VOTE, request.params.id)

if (!transaction) {
return Boom.notFound('Vote not found')
}

return utils.respondWithResource(request, transaction, 'transaction')
},
options: {
Expand Down
10 changes: 5 additions & 5 deletions packages/core-api/lib/versions/2/handlers/wallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ exports.show = {
const wallet = await database.wallets.findById(request.params.id)

if (!wallet) {
return Boom.notFound()
return Boom.notFound('Wallet not found')
}

return utils.respondWithResource(request, wallet, 'wallet')
Expand All @@ -76,7 +76,7 @@ exports.transactions = {
const wallet = await database.wallets.findById(request.params.id)

if (!wallet) {
return Boom.notFound()
return Boom.notFound('Wallet not found')
}

const transactions = await database.transactions.findAllByWallet(
Expand Down Expand Up @@ -106,7 +106,7 @@ exports.transactionsSent = {
const wallet = await database.wallets.findById(request.params.id)

if (!wallet) {
return Boom.notFound()
return Boom.notFound('Wallet not found')
}

const transactions = await database.transactions.findAllBySender(
Expand Down Expand Up @@ -136,7 +136,7 @@ exports.transactionsReceived = {
const wallet = await database.wallets.findById(request.params.id)

if (!wallet) {
return Boom.notFound()
return Boom.notFound('Wallet not found')
}

const transactions = await database.transactions.findAllByRecipient(
Expand Down Expand Up @@ -166,7 +166,7 @@ exports.votes = {
const wallet = await database.wallets.findById(request.params.id)

if (!wallet) {
return Boom.notFound()
return Boom.notFound('Wallet not found')
}

const transactions = await database.transactions.allVotesBySender(
Expand Down

0 comments on commit 6f8986c

Please sign in to comment.