Added better API errors and stuff

This commit is contained in:
Jori Lallo
2016-09-11 13:38:52 -07:00
parent 9b49b45fe7
commit 53c3d3c318
6 changed files with 34 additions and 37 deletions

View File

@ -1,6 +1,7 @@
exports[`test should require authentication 1`] = ` exports[`test should require authentication 1`] = `
Object { Object {
"message": "Authentication required" "error": "Authentication required",
"success": false
} }
`; `;
@ -11,6 +12,7 @@ Object {
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
"name": "User 1", "name": "User 1",
"username": "user1" "username": "user1"
} },
"success": true
} }
`; `;

View File

@ -14,6 +14,7 @@ import apiKeys from './apiKeys';
import validation from './middlewares/validation'; import validation from './middlewares/validation';
import methodOverride from '../middlewares/methodOverride'; import methodOverride from '../middlewares/methodOverride';
import cache from '../middlewares/cache'; import cache from '../middlewares/cache';
import apiWrapper from './middlewares/apiWrapper';
const api = new Koa(); const api = new Koa();
const router = new Router(); const router = new Router();
@ -39,7 +40,10 @@ api.use(async (ctx, next) => {
ctx.app.emit('error', err, ctx); ctx.app.emit('error', err, ctx);
} }
ctx.body = { message }; ctx.body = {
success: false,
error: message,
};
} }
}); });
@ -47,6 +51,7 @@ api.use(bodyParser());
api.use(methodOverride()); api.use(methodOverride());
api.use(cache()); api.use(cache());
api.use(validation()); api.use(validation());
api.use(apiWrapper());
router.use('/', auth.routes()); router.use('/', auth.routes());
router.use('/', user.routes()); router.use('/', user.routes());
@ -59,9 +64,4 @@ router.use('/', apiKeys.routes());
// allow middleware to catch any routes which were not explicitly defined. // allow middleware to catch any routes which were not explicitly defined.
api.use(router.routes()); api.use(router.routes());
// API 404 handler
api.use(async () => {
throw httpErrors.NotFound();
});
export default api; export default api;

View File

@ -0,0 +1,12 @@
export default function apiWrapper(_options) {
return async function apiWrapperMiddleware(ctx, next) {
await next();
const success = ctx.status < 400;
ctx.body = {
...ctx.body,
success,
};
};
}

View File

@ -13,12 +13,14 @@ const app = new Koa();
app.use(compress()); app.use(compress());
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
const convert = require('koa-convert') /* eslint-disable global-require */
const convert = require('koa-convert');
const webpack = require('webpack'); const webpack = require('webpack');
const devMiddleware = require('koa-webpack-dev-middleware'); const devMiddleware = require('koa-webpack-dev-middleware');
const hotMiddleware = require('koa-webpack-hot-middleware'); const hotMiddleware = require('koa-webpack-hot-middleware');
const config = require('../webpack.config.dev'); const config = require('../webpack.config.dev');
const compile = webpack(config); const compile = webpack(config);
/* eslint-enable global-require */
app.use(convert(devMiddleware(compile, { app.use(convert(devMiddleware(compile, {
// display no info to console (only warnings and errors) // display no info to console (only warnings and errors)
@ -43,13 +45,13 @@ if (process.env.NODE_ENV === 'development') {
// options for formating the statistics // options for formating the statistics
stats: { stats: {
colors: true colors: true,
} },
}))); })));
app.use(convert(hotMiddleware(compile, { app.use(convert(hotMiddleware(compile, {
log: console.log, log: console.log, // eslint-disable-line
path: '/__webpack_hmr', path: '/__webpack_hmr',
heartbeat: 10 * 1000 heartbeat: 10 * 1000,
}))); })));
app.use(logger()); app.use(logger());
} }

View File

@ -1,20 +1,11 @@
import Sequelize from 'sequelize';
import _ from 'lodash'; import _ from 'lodash';
import { Document, Atlas, User, Revision } from './models'; import { Document, Atlas, User } from './models';
export function presentUser(ctx, user) { import presentUser from './presenters/user';
ctx.cache.set(user.id, user);
return new Promise(async (resolve, _reject) => { export {
const data = { presentUser,
id: user.id, };
name: user.name,
username: user.username,
avatarUrl: user.slackData.image_192,
};
resolve(data);
});
}
export function presentTeam(ctx, team) { export function presentTeam(ctx, team) {
ctx.cache.set(team.id, team); ctx.cache.set(team.id, team);

View File

@ -9,16 +9,6 @@ import subdomainRedirect from './middlewares/subdomainRedirect';
const koa = new Koa(); const koa = new Koa();
const router = new Router(); const router = new Router();
// // error handler
// koa.use(async (ctx, next) => {
// try {
// await next();
// } catch (err) {
// ctx.status = err.status || 500;
// ctx.body = err.message;
// }
// });
router.get('/service-worker.js', async (ctx) => { router.get('/service-worker.js', async (ctx) => {
ctx.set('Content-Type', 'application/javascript'); ctx.set('Content-Type', 'application/javascript');
if (process.env.NODE_ENV === 'production') ctx.set('Cache-Control', `max-age=${30}`); if (process.env.NODE_ENV === 'production') ctx.set('Cache-Control', `max-age=${30}`);