Fix custom response body

This commit is contained in:
Jim Laurie 2017-08-18 18:18:21 +02:00
parent 2e6d5c9a1a
commit 82603ad480
2 changed files with 8 additions and 2 deletions

View File

@ -153,9 +153,10 @@ module.exports = {
const { env } = ctx.params;
let params = ctx.request.body;
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
const [name] = _.keys(params.database.connections);
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
if (!name || _.find(Service.getDatabases(env), { name })) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.database.exist' }] }]);
const model = Service.databases(name, env);

View File

@ -37,18 +37,23 @@ module.exports = strapi => {
// Log error.
strapi.log.error(error);
// Wrap error into a Boom's response.
ctx.status = error.status || 500;
ctx.body = _.get(ctx.body, 'isBoom')
? ctx.body || error && error.message
: Boom.wrap(error, ctx.status, ctx.body || error.message);
}
// Empty body is considered as `notFound` response.
if (!ctx.body) {
ctx.notFound();
}
if (ctx.body.isBoom && ctx.body.data) {
ctx.body.output.payload.message = ctx.body.data;
}
// Format `ctx.body` and `ctx.status`.
ctx.status = ctx.body.isBoom ? ctx.body.output.statusCode : ctx.status;
ctx.body = ctx.body.isBoom ? ctx.body.output.payload : ctx.body;