Final direct BadRequest removals

This commit is contained in:
Tom Moor
2018-02-24 20:52:56 -08:00
parent f3c7fb8bc6
commit f8bdadfd9a
4 changed files with 15 additions and 14 deletions

View File

@ -1,6 +1,6 @@
// @flow // @flow
import httpErrors from 'http-errors';
import querystring from 'querystring'; import querystring from 'querystring';
import { InvalidRequestError } from '../../errors';
import { type Context } from 'koa'; import { type Context } from 'koa';
export default function pagination(options?: Object) { export default function pagination(options?: Object) {
@ -24,7 +24,7 @@ export default function pagination(options?: Object) {
offset = isNaN(offset) ? 0 : offset; offset = isNaN(offset) ? 0 : offset;
if (limit > opts.maxLimit) { if (limit > opts.maxLimit) {
throw httpErrors.BadRequest( throw new InvalidRequestError(
`Pagination limit is too large (max ${opts.maxLimit})` `Pagination limit is too large (max ${opts.maxLimit})`
); );
} }

View File

@ -1,7 +1,7 @@
// @flow // @flow
import Koa from 'koa'; import Koa from 'koa';
import Router from 'koa-router'; import Router from 'koa-router';
import httpErrors from 'http-errors'; import { NotFoundError } from '../errors';
import { Mailer } from '../mailer'; import { Mailer } from '../mailer';
const emailPreviews = new Koa(); const emailPreviews = new Koa();
@ -22,7 +22,7 @@ router.get('/:type/:format', async ctx => {
if (Object.getOwnPropertyNames(previewMailer).includes(ctx.params.type)) { if (Object.getOwnPropertyNames(previewMailer).includes(ctx.params.type)) {
// $FlowIssue flow doesn't like this but we're ok with it // $FlowIssue flow doesn't like this but we're ok with it
previewMailer[ctx.params.type]('user@example.com'); previewMailer[ctx.params.type]('user@example.com');
} else throw httpErrors.NotFound(); } else throw new NotFoundError('Email template could not be found');
} }
if (!mailerOutput) return; if (!mailerOutput) return;

View File

@ -2,7 +2,6 @@
import React from 'react'; import React from 'react';
import path from 'path'; import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import httpErrors from 'http-errors';
import Koa from 'koa'; import Koa from 'koa';
import Router from 'koa-router'; import Router from 'koa-router';
import sendfile from 'koa-sendfile'; import sendfile from 'koa-sendfile';
@ -11,6 +10,7 @@ import subdomainRedirect from './middlewares/subdomainRedirect';
import renderpage from './utils/renderpage'; import renderpage from './utils/renderpage';
import { slackAuth } from '../shared/utils/routeHelpers'; import { slackAuth } from '../shared/utils/routeHelpers';
import { robotsResponse } from './utils/robots'; import { robotsResponse } from './utils/robots';
import { NotFoundError } from './errors';
import Home from './pages/Home'; import Home from './pages/Home';
import About from './pages/About'; import About from './pages/About';
@ -88,7 +88,7 @@ router.get('/robots.txt', ctx => (ctx.body = robotsResponse(ctx)));
// catch all for react app // catch all for react app
router.get('*', async ctx => { router.get('*', async ctx => {
await renderapp(ctx); await renderapp(ctx);
if (!ctx.status) ctx.throw(httpErrors.NotFound()); if (!ctx.status) ctx.throw(new NotFoundError());
}); });
// middleware // middleware

View File

@ -1,14 +1,15 @@
// @flow // @flow
import fetch from 'isomorphic-fetch'; import fetch from 'isomorphic-fetch';
import querystring from 'querystring'; import querystring from 'querystring';
import httpErrors from 'http-errors'; import { InvalidRequestError } from './errors';
const SLACK_API_URL = 'https://slack.com/api'; const SLACK_API_URL = 'https://slack.com/api';
export async function post(endpoint: string, body: Object) { export async function post(endpoint: string, body: Object) {
let data; let data;
try {
const token = body.token; const token = body.token;
try {
const response = await fetch(`${SLACK_API_URL}/${endpoint}`, { const response = await fetch(`${SLACK_API_URL}/${endpoint}`, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -18,10 +19,10 @@ export async function post(endpoint: string, body: Object) {
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
data = await response.json(); data = await response.json();
} catch (e) { } catch (err) {
throw httpErrors.BadRequest(); throw new InvalidRequestError(err.message);
} }
if (!data.ok) throw httpErrors.BadRequest(data.error); if (!data.ok) throw new InvalidRequestError(data.error);
return data; return data;
} }
@ -33,10 +34,10 @@ export async function request(endpoint: string, body: Object) {
`${SLACK_API_URL}/${endpoint}?${querystring.stringify(body)}` `${SLACK_API_URL}/${endpoint}?${querystring.stringify(body)}`
); );
data = await response.json(); data = await response.json();
} catch (e) { } catch (err) {
throw httpErrors.BadRequest(); throw new InvalidRequestError(err.message);
} }
if (!data.ok) throw httpErrors.BadRequest(data.error); if (!data.ok) throw new InvalidRequestError(data.error);
return data; return data;
} }