From cb48acde552092a8263066bc315a458847951857 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Mon, 31 Aug 2020 21:11:53 -0700 Subject: [PATCH] Error handling for queries. --- components/metrics/MetricCard.js | 2 +- lib/queries.js | 10 +++++----- pages/api/website/[id]/metrics.js | 7 +++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/components/metrics/MetricCard.js b/components/metrics/MetricCard.js index ad6c62eb..6209509c 100644 --- a/components/metrics/MetricCard.js +++ b/components/metrics/MetricCard.js @@ -4,7 +4,7 @@ import { formatNumber } from '../../lib/format'; import styles from './MetricCard.module.css'; const MetricCard = ({ value = 0, label, format = formatNumber }) => { - const props = useSpring({ x: value, from: { x: 0 } }); + const props = useSpring({ x: Number(value) || 0, from: { x: 0 } }); return (
diff --git a/lib/queries.js b/lib/queries.js index d76cdaff..204b545b 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -299,7 +299,7 @@ export function getMetrics(website_id, start_at, end_at) { ); } - return Promise.resolve({}); + return Promise.reject(new Error('Unknown database.')); } export function getPageviews( @@ -346,7 +346,7 @@ export function getPageviews( ); } - return Promise.resolve([]); + return Promise.reject(new Error('Unknown database.')); } export function getRankings(website_id, start_at, end_at, type, table, domain) { @@ -388,7 +388,7 @@ export function getRankings(website_id, start_at, end_at, type, table, domain) { ); } - return Promise.resolve([]); + return Promise.reject(new Error('Unknown database.')); } export function getActiveVisitors(website_id) { @@ -421,7 +421,7 @@ export function getActiveVisitors(website_id) { ); } - return Promise.resolve([]); + return Promise.reject(new Error('Unknown database.')); } export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit = 'day') { @@ -465,5 +465,5 @@ export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit = ); } - return Promise.resolve([]); + return Promise.reject(new Error('Unknown database.')); } diff --git a/pages/api/website/[id]/metrics.js b/pages/api/website/[id]/metrics.js index 82ab393e..4b0d71e1 100644 --- a/pages/api/website/[id]/metrics.js +++ b/pages/api/website/[id]/metrics.js @@ -3,11 +3,14 @@ import { ok } from 'lib/response'; export default async (req, res) => { const { id, start_at, end_at } = req.query; + const websiteId = +id; + const startDate = new Date(+start_at); + const endDate = new Date(+end_at); - const metrics = await getMetrics(+id, new Date(+start_at), new Date(+end_at)); + const metrics = await getMetrics(websiteId, startDate, endDate); const stats = Object.keys(metrics[0]).reduce((obj, key) => { - obj[key] = +metrics[0][key]; + obj[key] = Number(metrics[0][key]) || 0; return obj; }, {});