Sails.js production environment does not return JSON response to badRequest, but dev environment

As the name indicates, my GET request to the Sails application for a specific route / controller function returns badRequest with JSON in the dev environment, but not in the prod environment. Why is this?

Here is the controller function:

index: function(req, res) { async.auto({ companies: function(cb) { User.findOneById(req.session.user.id) .populate('companies') .exec(function(err, user) { if(err) { var badRequestData = { error: err }; return cb(badRequestData, null); } else if(user.companies.length == 0) { var badRequestData = { error: "This user has no associated companies." }; return cb(badRequestData, null); } cb(null, user.companies) }); }, validateForNullCompanies: ['companies', function(cb, results) { var nullCompanies = _.where(results.companies, { stripeAccountId: null }); if(nullCompanies.length > 0) { var badRequestData = { error: "This user needs to authenticate stripe with their company." }; return cb(badRequestData, null); } else { return cb(); } }] }, function(err, results) { if (err) { return res.badRequest(err); } return res.ok(); }); }, 
+5
source share
2 answers

Just came across this

If you look at the notes in http://sailsjs.org/documentation/reference/response-res/res-bad-request , he says

By default, the specified error (err) will be excluded if the application is running in a "production" environment (i.e. process.env.NODE_ENV === 'production').

And looking at api/responses/badRequest.js , we can see the following code:

 // Only include errors in response if application environment // is not set to 'production'. In production, we shouldn't // send back any identifying information about errors. if (sails.config.environment === 'production') { data = undefined; } 

So, if you comment on this, I am sure that you will get the desired result in production. At the same time, there seems to be a reason here, so maybe send special error codes using res.send([statusCode], body) , where the body is your JSON, or, alternatively, create client-side processing with a less descriptive explanation for a bad request.

+6
source

As with finnergizer's answer, if you check api / answers / badRequest.js, the actual code should look something like this:

  // Only include errors in response if application environment // is not set to 'production'. In production, we shouldn't // send back any identifying information about errors. if (sails.config.environment === 'production' && sails.config.keepResponseErrors !== true) { data = undefined; } 

The reason that was already mentioned in the comments. If you still think that you want to display an error message in the working environment, instead of commenting on this line, you can actually add the keepResponseErrors element and set it to true in config / env / production.js. Like this:

 module.exports = { keepResponseErrors: true }; 

Thus, there is no need to modify each api / response js file.

+2
source

All Articles