morcutt
morcutt

Reputation: 3749

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();

    });

},

Upvotes: 5

Views: 1442

Answers (3)

Syed Tabish Ali
Syed Tabish Ali

Reputation: 313

I have faced the same issue and resolved through adding below code in config/env/production.js

module.exports = {
   keepResponseErrors: true
}

Upvotes: 0

Anthony Yuen
Anthony Yuen

Reputation: 166

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.

Upvotes: 4

finnergizer
finnergizer

Reputation: 406

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.

Upvotes: 7

Related Questions