Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
feat: improve error handling in new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnygleason committed Sep 19, 2019
1 parent d54914f commit decf158
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
49 changes: 41 additions & 8 deletions api/network-explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {loadBlockIndex} from './loaders/blocks/index';
import {loadBlockDetail} from './loaders/blocks/detail';
import {loadTransactionIndex} from './loaders/transactions/index';
import {loadTransactionDetail} from './loaders/transactions/detail';
import {FriendlyGet} from './friendlyGet';

function prettify(req, data) {
const isPretty = req.query && req.query.pretty === 'true';
Expand Down Expand Up @@ -35,19 +36,33 @@ export function addNetworkExplorerRoutes(redisX, app) {
const start = q.start || '+';
const count = q.count ? parseInt(q.count) : DEFAULT_PAGE_SIZE;
const direction = q.direction || '-';
const rawData = await loadBlockIndex(redisX, start, count, direction);
const {__errors__, rawData} = await new FriendlyGet()
.with('rawData', loadBlockIndex(redisX, start, count, direction), {})
.get();

res.send(prettify(req, new BlockIndexView().asVersion(rawData, version)));
res.send(
prettify(
req,
new BlockIndexView().asVersion(rawData, __errors__, version),
),
);
});

// Network Explorer Block Detail
app.get('/explorer/blocks/:id', async (req, res) => {
const q = req.query || {};

const version = q.v || 'BlockDetailView@latest';
const rawData = await loadBlockDetail(redisX, req.params.id, version);
const {__errors__, rawData} = await new FriendlyGet()
.with('rawData', loadBlockDetail(redisX, req.params.id, version), {})
.get();

res.send(prettify(req, new BlockDetailView().asVersion(rawData, version)));
res.send(
prettify(
req,
new BlockDetailView().asVersion(rawData, __errors__, version),
),
);
});

// Network Explorer Transaction Index
Expand All @@ -58,10 +73,19 @@ export function addNetworkExplorerRoutes(redisX, app) {
const start = q.start || '+';
const count = q.count ? parseInt(q.count) : DEFAULT_PAGE_SIZE;
const direction = q.direction || '-';
const rawData = await loadTransactionIndex(redisX, start, count, direction);
const {__errors__, rawData} = await new FriendlyGet()
.with(
'rawData',
loadTransactionIndex(redisX, start, count, direction),
{},
)
.get();

res.send(
prettify(req, new TransactionIndexView().asVersion(rawData, version)),
prettify(
req,
new TransactionIndexView().asVersion(rawData, __errors__, version),
),
);
});

Expand All @@ -70,10 +94,19 @@ export function addNetworkExplorerRoutes(redisX, app) {
const q = req.query || {};

const version = q.v || 'TransactionDetailView@latest';
const rawData = await loadTransactionDetail(redisX, req.params.id, version);
const {__errors__, rawData} = await new FriendlyGet()
.with(
'rawData',
loadTransactionDetail(redisX, req.params.id, version),
{},
)
.get();

res.send(
prettify(req, new TransactionDetailView().asVersion(rawData, version)),
prettify(
req,
new TransactionDetailView().asVersion(rawData, __errors__, version),
),
);
});
}
9 changes: 8 additions & 1 deletion api/views/blocks/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
const __VERSION__ = '[email protected]';

export class BlockDetailView {
asVersion(rawData, version) {
asVersion(rawData, __errors__, version) {
if (__errors__) {
return {
__VERSION__,
__errors__,
};
}

const entries = rawData.entries;
const blockData = rawData.block;

Expand Down
9 changes: 8 additions & 1 deletion api/views/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import _ from 'lodash';
*/
const __VERSION__ = '[email protected]';
export class BlockIndexView {
asVersion(rawData, version) {
asVersion(rawData, __errors__, version) {
if (__errors__) {
return {
__VERSION__,
__errors__,
};
}

const timelineData = rawData.timelinePage;
const timelineInfo = rawData.timelineInfo;

Expand Down
9 changes: 8 additions & 1 deletion api/views/transactions/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import _ from 'lodash';
*/
const __VERSION__ = '[email protected]';
export class TransactionDetailView {
asVersion(rawData, version) {
asVersion(rawData, __errors__, version) {
if (__errors__) {
return {
__VERSION__,
__errors__,
};
}

const transactionData = rawData.transaction;
const entryData = rawData.entry;
const blockData = rawData.block;
Expand Down
9 changes: 8 additions & 1 deletion api/views/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import _ from 'lodash';
*/
const __VERSION__ = '[email protected]';
export class TransactionIndexView {
asVersion(rawData, version) {
asVersion(rawData, __errors__, version) {
if (__errors__) {
return {
__VERSION__,
__errors__,
};
}

const timelineData = rawData.timelinePage;
const timelineInfo = rawData.timelineInfo;

Expand Down

0 comments on commit decf158

Please sign in to comment.