This method is used to send a 500 ("Server Error") response back down to the client indicating that some kind of server error occurred.
return res.serverError(data, pathToView);
Like the other built-in custom response modules, the behavior of this method is customizable.
By default, it works as follows:
- If the request "wants JSON" (e.g. the request originated from AJAX, WebSockets, or a REST client like cURL), Sails will send the provided error
dataas JSON. If no
datais provided a default response body will be sent (the string
- If the request does not "want JSON" (e.g. a URL typed into a web browser), Sails will attempt to serve one of your views.
- If a specific
pathToViewwas provided, Sails will attempt to use that view.
- Alternatively if
pathToViewwas not provided, Sails will serve a default error page (the view located at
views/500.ejs). If that view does not exist, Sails will just send JSON.
- If Sails serves a view, the
dataargument will be accessible as a view local:
- If a specific
Using the default error view:
return res.serverError('Salesforce could not be reached');
With a custom view:
return res.serverError( 'Salesforce could not be reached', 'salesforce/leads/edit' );
- This method is terminal, meaning it is generally the last line of code your app should run for a given request (hence the advisory usage of
returnthroughout these docs).
res.serverError()(like other userland response methods) can be overridden or modified. It runs the response method defined in
/responses/serverError.js, which is bundled automatically in newly generated Sails apps. If a
serverError.jsresponse method does not exist in your app, Sails will implicitly use the default behavior.
pathToViewrefers to a missing view, this method will respond as if the request "wants JSON". +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').
Is something missing?
If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.
- Blueprint API
- Command Line Interface
- Response (`res`)
- Waterline (ORM)
- Resourceful PubSub
- Socket Client
We wrote a book!
Get early access to the book
with promotion code: mcneildoc