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

@ -14,6 +14,7 @@ import apiKeys from './apiKeys';
import validation from './middlewares/validation';
import methodOverride from '../middlewares/methodOverride';
import cache from '../middlewares/cache';
import apiWrapper from './middlewares/apiWrapper';
const api = new Koa();
const router = new Router();
@ -39,7 +40,10 @@ api.use(async (ctx, next) => {
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(cache());
api.use(validation());
api.use(apiWrapper());
router.use('/', auth.routes());
router.use('/', user.routes());
@ -59,9 +64,4 @@ router.use('/', apiKeys.routes());
// allow middleware to catch any routes which were not explicitly defined.
api.use(router.routes());
// API 404 handler
api.use(async () => {
throw httpErrors.NotFound();
});
export default api;