Error handling for queries.

This commit is contained in:
Mike Cao 2020-08-31 21:11:53 -07:00
parent cebd3fd924
commit cb48acde55
3 changed files with 11 additions and 8 deletions

View File

@ -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 (
<div className={styles.card}>

View File

@ -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.'));
}

View File

@ -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;
}, {});