diff --git a/src/app/routes/v1/zrx-price.js b/src/app/routes/v1/zrx-price.js index dbd15b84a..0d7a682b5 100644 --- a/src/app/routes/v1/zrx-price.js +++ b/src/app/routes/v1/zrx-price.js @@ -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(); }); diff --git a/src/util/error-logger.js b/src/util/error-logger.js index c239e75a0..0fc69ec25 100644 --- a/src/util/error-logger.js +++ b/src/util/error-logger.js @@ -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);