Skip to content

Commit

Permalink
refactor(responses): always return json
Browse files Browse the repository at this point in the history
  • Loading branch information
RWOverdijk committed Jan 15, 2017
1 parent cc409ba commit 846aca6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 244 deletions.
36 changes: 3 additions & 33 deletions api/responses/badRequest.js
Expand Up @@ -38,39 +38,9 @@ module.exports = function badRequest(data, options) {
data = undefined;
}

// If the user-agent wants JSON, always respond with JSON
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
if (typeof data === 'string') {
data = {error: data};
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Bad Request' });
}

// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else return res.guessView({ data: viewData, title: 'Bad Request' }, function couldNotGuessView () {
return res.jsonx(data);
});

return res.jsonx(data);
};

35 changes: 3 additions & 32 deletions api/responses/created.js
Expand Up @@ -23,38 +23,9 @@ module.exports = function created (data, options) {
// Set status code
res.status(201);

// If appropriate, serve data as JSON(P)
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
if (typeof data === 'string') {
data = {message: data};
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Created' });
}

// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else return res.guessView({ data: viewData, title: 'Created' }, function couldNotGuessView () {
return res.jsonx(data);
});

return res.jsonx(data);
};
52 changes: 3 additions & 49 deletions api/responses/forbidden.js
Expand Up @@ -35,55 +35,9 @@ module.exports = function forbidden (data, options) {
data = undefined;
}

// If the user-agent wants JSON, always respond with JSON
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
if (typeof data === 'string') {
data = {message: data};
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Forbidden' });
}

// If no second argument provided, try to serve the default view,
// but fall back to sending JSON(P) if any errors occur.
else return res.view('403', { data: viewData, title: 'Forbidden' }, function (err, html) {

// If a view error occured, fall back to JSON(P).
if (err) {
//
// Additionally:
// • If the view was missing, ignore the error but provide a verbose log.
if (err.code === 'E_VIEW_FAILED') {
sails.log.verbose('res.forbidden() :: Could not locate view for error page (sending JSON instead). Details: ',err);
}
// Otherwise, if this was a more serious error, log to the console with the details.
else {
sails.log.warn('res.forbidden() :: When attempting to render error page view, an error occured (sending JSON instead). Details: ', err);
}
return res.jsonx(data);
}

return res.send(html);
});

return res.jsonx(data);
};

52 changes: 3 additions & 49 deletions api/responses/notFound.js
Expand Up @@ -40,55 +40,9 @@ module.exports = function notFound (data, options) {
data = undefined;
}

// If the user-agent wants JSON, always respond with JSON
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
if (typeof data === 'string') {
data = {message: data};
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Not Found' });
}

// If no second argument provided, try to serve the default view,
// but fall back to sending JSON(P) if any errors occur.
else return res.view('404', { data: viewData, title: 'Not Found' }, function (err, html) {

// If a view error occured, fall back to JSON(P).
if (err) {
//
// Additionally:
// • If the view was missing, ignore the error but provide a verbose log.
if (err.code === 'E_VIEW_FAILED') {
sails.log.verbose('res.notFound() :: Could not locate view for error page (sending JSON instead). Details: ',err);
}
// Otherwise, if this was a more serious error, log to the console with the details.
else {
sails.log.warn('res.notFound() :: When attempting to render error page view, an error occured (sending JSON instead). Details: ', err);
}
return res.jsonx(data);
}

return res.send(html);
});

return res.jsonx(data);
};

35 changes: 3 additions & 32 deletions api/responses/ok.js
Expand Up @@ -25,38 +25,9 @@ module.exports = function sendOK (data, options) {
// Set status code
res.status(200);

// If appropriate, serve data as JSON(P)
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
if (typeof data === 'string') {
data = {message: data};
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'OK' });
}

// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else return res.guessView({ data: viewData, title: 'OK' }, function couldNotGuessView () {
return res.jsonx(data);
});

return res.jsonx(data);
};
52 changes: 3 additions & 49 deletions api/responses/serverError.js
Expand Up @@ -35,55 +35,9 @@ module.exports = function serverError (data, options) {
data = undefined;
}

// If the user-agent wants JSON, always respond with JSON
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
if (typeof data === 'string') {
data = {error: data};
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Server Error' });
}

// If no second argument provided, try to serve the default view,
// but fall back to sending JSON(P) if any errors occur.
else return res.view('500', { data: viewData, title: 'Server Error' }, function (err, html) {

// If a view error occured, fall back to JSON(P).
if (err) {
//
// Additionally:
// • If the view was missing, ignore the error but provide a verbose log.
if (err.code === 'E_VIEW_FAILED') {
sails.log.verbose('res.serverError() :: Could not locate view for error page (sending JSON instead). Details: ',err);
}
// Otherwise, if this was a more serious error, log to the console with the details.
else {
sails.log.warn('res.serverError() :: When attempting to render error page view, an error occured (sending JSON instead). Details: ', err);
}
return res.jsonx(data);
}

return res.send(html);
});

return res.jsonx(data);
};

0 comments on commit 846aca6

Please sign in to comment.