avian
avian

Reputation: 353

Bind a controller to handle bad request in sails js

I'm new to sails js and still exploring the framework. My question is that, can we bind an action inside a controller whenever there is a bad request from front end? rather than calling in the response page, the customized controller is instead called.

Please shed some light. Thanks

Upvotes: 1

Views: 703

Answers (1)

sgress454
sgress454

Reputation: 24948

You have a few options:

  1. You can always call res.badRequest(), which will return a 400 status response to the browser.
  2. You can create a service to house custom "bad request" response code, and call it from any controller. For example, make a api/services/ResponseService.js file with a method like:

    badRequest: function(req, res) {
        return res.send(400, "You've been bad!");
    }
    

    and call it from your controller like ResponseService.badRequest(req, res);

  3. If you're using Sails v0.10.x, you can customize the default badRequest response by modifying api/responses/badRequest.js. This has all the benefits of #2, while allowing you to use the semantics of #1 (i.e. just calling res.badRequest()) and avoiding the overhead of a separate service module.

Upvotes: 1

Related Questions