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