UNPKG

sails-base

Version:

sails1.0+扩展基础包,包含用户体系权限校验及model的attr,action的inputs入参提示语自定义功能

60 lines (54 loc) 1.55 kB
/** * serverError.js * * A custom response. * * Example usage: * ``` * return res.serverError(); * // -or- * return res.serverError(optionalData); * ``` * * Or with actions2: * ``` * exits: { * somethingHappened: { * responseType: 'serverError' * } * } * ``` * * ``` * throw 'somethingHappened'; * // -or- * throw { somethingHappened: optionalData } * ``` */ module.exports = function serverError(optionalData) { // Get access to `req` and `res` var req = this.req; var res = this.res; // Define the status code to send in the response. var statusCodeToSet = 500; // If no data was provided, use res.sendStatus(). if (optionalData === undefined) { sails.log.info('Ran custom response: res.serverError()'); return res.sendStatus(statusCodeToSet); } // Else if the provided data is an Error instance, if it has // a toJSON() function, then always run it and use it as the // response body to send. Otherwise, send down its `.stack`, // except in production use res.sendStatus(). else if (_.isError(optionalData)) { sails.log.info('Custom response `res.serverError()` called with an Error:', optionalData); if(_.isFunction(optionalData.toJSON)) return res.status(statusCodeToSet).send(optionalData.toJSON()); return res.status(statusCodeToSet).send({ message: optionalData.message }); } // Set status code and send response data. else { return res.status(statusCodeToSet).send(optionalData); } };