Skip to content

Commit

Permalink
Improve custom responses with Boom
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelsicoko committed Jan 4, 2017
1 parent 5ffc502 commit ceab4e2
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 102 deletions.
34 changes: 31 additions & 3 deletions packages/strapi/lib/configuration/hooks/core/responses/policy.js
Expand Up @@ -7,17 +7,45 @@
// Public node modules.
const _ = require('lodash');
const Boom = require('boom');
const delegate = require('delegates');

// Local utilities.
const responses = require('./responses/index');

// Custom function to avoid ctx.body repeat
const createResponses = ctx => {
return _.merge(
responses,
_.mapValues(_.omit(Boom, ['wrap', 'create']), (fn, name) => (...rest) => {
ctx.body = fn(...rest);
})
);
};

/**
* Policy used to add responses in the `this.response` object.
*/

module.exports = async function (ctx, next) {
// Add the custom responses to the `ctx` object.
_.assign(ctx, responses, Boom);
const delegator = delegate(ctx, 'response');

_.forEach(createResponses(ctx), (value, key) => {
// Assign new error methods to context.response
ctx.response[key] = value;
// Delegate error methods to context
delegator.method(key);
});

try {
await next();

if (_.get(ctx.body, 'isBoom')) {
ctx.throw(ctx.status);
}
} catch (error) {
const formattedError = _.get(ctx.body, 'isBoom') ? ctx.body : Boom.wrap(error, error.status, ctx.body);

await next();
ctx.status = formattedError.output.statusCode || error.status || 500;
ctx.body = formattedError.output.payload;
}
};

This file was deleted.

This file was deleted.

Expand Up @@ -5,10 +5,6 @@
*/

module.exports = {
badRequest: require('./badRequest'),
created: require('./created'),
forbidden: require('./forbidden'),
notFound: require('./notFound'),
ok: require('./ok'),
serverError: require('./serverError')
send: require('./send')
};

This file was deleted.

@@ -1,7 +1,7 @@
'use strict';

/**
* Default `ok` response.
* Default `send` response.
*/

module.exports = function sendOk(data) {
Expand Down

This file was deleted.

18 changes: 1 addition & 17 deletions packages/strapi/lib/configuration/hooks/core/router/index.js
Expand Up @@ -185,22 +185,6 @@ module.exports = strapi => {
strapi.app.use(router.middleware());
});

// Wrap error into Boom object
strapi.app.use(async (ctx, next) => {
try {
await next();

if (ctx.status >= 400 || _.get(ctx.body, 'isBoom')) {
ctx.throw(ctx.status);
}
} catch (error) {
const formattedError = _.get(ctx.body, 'isBoom') ? ctx.body : Boom.wrap(error, error.status, ctx.body);

ctx.status = formattedError.output.statusCode || error.status || 500;
ctx.body = formattedError.output.payload;
}
});

// Let the router use our routes and allowed methods.
strapi.app.use(strapi.router.middleware());
strapi.app.use(strapi.router.router.allowedMethods({
Expand All @@ -214,7 +198,7 @@ module.exports = strapi => {
// Middleware used for every routes.
// Expose the endpoint in `this`.
function globalPolicy(endpoint, value, route) {
return async function (ctx, next) {
return async (ctx, next) => {
ctx.request.route = {
endpoint: _.trim(endpoint),
controller: _.trim(value.controller),
Expand Down
1 change: 1 addition & 0 deletions packages/strapi/package.json
Expand Up @@ -36,6 +36,7 @@
"async": "^2.1.2",
"boom": "^4.2.0",
"consolidate": "~0.14.0",
"delegates": "^1.0.0",
"kcors": "^2.2.0",
"koa": "^2.0.0-alpha.7",
"koa-better-static": "^1.0.5",
Expand Down

0 comments on commit ceab4e2

Please sign in to comment.