Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js Production Environment does not return JSON response on badRequest but dev environment does

As stated in the title, my sails app GET request to a specific route/controller function returns badRequest with JSON in a dev environment but not in a 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();

    });

},
like image 472
morcutt Avatar asked Jun 15 '15 15:06

morcutt


2 Answers

Just bumped into this too,

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

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

And taking a look 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 this out, I am sure you will get your desired result in production. At the same time, it seems to be here for a reason, so maybe send back custom error codes with res.send([statusCode], body) where body is your JSON, or alternatively create some client side handling with a less descriptive explanation of the bad request.

like image 159
finnergizer Avatar answered Nov 01 '22 19:11

finnergizer


Similar to the answer provided by finnergizer, if you check the api/responses/badRequest.js, the actual code should be 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 already stated in the comments. If you still think that you want to display the error message in the production environment, instead of comment out this line, actually you can add the item 'keepResponseErrors' and set it to true in config/env/production.js. Like this:

module.exports = {
  keepResponseErrors: true
};

In this way, there is no need to change every api/response js files.

like image 4
Anthony Yuen Avatar answered Nov 01 '22 19:11

Anthony Yuen