Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Attempt to prettify data before displaying in views
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Feb 12, 2016
1 parent f2018fa commit 19c7ae9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 13 deletions.
15 changes: 13 additions & 2 deletions templates/api/responses/badRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,27 @@ module.exports = function badRequest(data, options) {
// 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: data });
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: data }, function couldNotGuessView () {
else return res.guessView({ data: viewData, title: 'Bad Request' }, function couldNotGuessView () {
return res.jsonx(data);
});

Expand Down
17 changes: 14 additions & 3 deletions templates/api/responses/created.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* - pass string to render specified view
*/

module.exports = function sendOK (data, options) {
module.exports = function created (data, options) {

// Get access to `req`, `res`, & `sails`
var req = this.req;
Expand All @@ -33,16 +33,27 @@ module.exports = function sendOK (data, options) {
// 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: data });
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: data }, function couldNotGuessView () {
else return res.guessView({ data: viewData, title: 'Created' }, function couldNotGuessView () {
return res.jsonx(data);
});

Expand Down
15 changes: 13 additions & 2 deletions templates/api/responses/forbidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,27 @@ module.exports = function forbidden (data, options) {
// 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: data });
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: data }, function (err, html) {
else return res.view('403', { data: viewData, title: 'Forbidden' }, function (err, html) {

// If a view error occured, fall back to JSON(P).
if (err) {
Expand Down
15 changes: 13 additions & 2 deletions templates/api/responses/notFound.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,27 @@ module.exports = function notFound (data, options) {
// 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: data });
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: data }, function (err, html) {
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) {
Expand Down
15 changes: 13 additions & 2 deletions templates/api/responses/ok.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,27 @@ module.exports = function sendOK (data, options) {
// 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: data });
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: data }, function couldNotGuessView () {
else return res.guessView({ data: viewData, title: 'OK' }, function couldNotGuessView () {
return res.jsonx(data);
});

Expand Down
15 changes: 13 additions & 2 deletions templates/api/responses/serverError.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,27 @@ module.exports = function serverError (data, options) {
// 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: data });
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: data }, function (err, html) {
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) {
Expand Down

0 comments on commit 19c7ae9

Please sign in to comment.