Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
Enhance logging around ZRX price endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cbovis committed Aug 30, 2020
1 parent db2a1bd commit f1c23d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
36 changes: 28 additions & 8 deletions src/app/routes/v1/zrx-price.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
const _ = require('lodash');
const axios = require('axios');
const Router = require('koa-router');

const { logError } = require('../../../util/error-logger');

const createRouter = () => {
const router = new Router({ prefix: '/zrx-price' });

router.get('/', async ({ response, request }, next) => {
const currency = request.query.currency || 'USD';
const { data } = await axios.get(
`https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ZRX&tsyms=${currency}&tryConversion=true`,
);

const price = {
value: data.RAW.ZRX[currency].PRICE,
change: data.RAW.ZRX[currency].CHANGEPCT24HOUR,
};
let data;

try {
const apiResponse = await axios.get(
`https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ZRX&tsyms=${currency}&tryConversion=true`,
);
data = apiResponse.data;
} catch (error) {
logError('Unable to fetch ZRX price', { error });

throw error;
}

response.body = price;
const price = _.get(data, `RAW.ZRX.${currency}.PRICE`);
const priceChange = _.get(data, `RAW.ZRX.${currency}.CHANGEPCT24HOUR`);

if (!_.isNumber(price) || !_.isNumber(priceChange)) {
logError('Invalid ZRX price response', { responseData: data });

throw new Error('Invalid ZRX price response');
}

response.body = {
value: price,
change: priceChange,
};

await next();
});
Expand Down
7 changes: 3 additions & 4 deletions src/util/error-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ const { getLogger } = require('./logging');

let bugsnagClient;

const logError = (error, opts) => {
const logError = (error, metadata) => {
const logger = getLogger('application');
const report = _.get(opts, 'report', false);

if (bugsnagClient && report) {
bugsnagClient.notify(error);
if (bugsnagClient) {
bugsnagClient.notify(error, metadata);
}

logger.error(error);
Expand Down

0 comments on commit f1c23d0

Please sign in to comment.