处理 Sails.js 中的异常
Handling exceptions in Sails.js
我正在使用 Sails.js 开发 REST API 服务器。
为了便于使用和抽象,我想在我的控制器中抛出异常,例如:
// api/controllers/TempController.js
module.exports = {
index: function(request, response) {
throw new NotFoundException('Specific user is not found.');
throw new AccessDeniedException('You have no permissions to access this resource.');
throw new SomeOtherException('Something went wrong.');
}
};
如何自动捕获这些异常(在全局级别)并将它们转换为有效的 JSON 响应?例如:
{
"success": false,
"exception": {
"type": "NotFoundException",
"message": "Specific user is not found."
}
}
使用内置 serverError
响应来处理此类异常是否是最佳方法?还是创建一些自定义中间件更好?如果是这样,你能提供一个简单的例子吗?
未处理的异常作为第一个参数 data
.
传递给 api/responses/serverError.js
中的默认响应
下面是如何处理此类异常的示例:
var Exception = require('../exceptions/Exception.js');
module.exports = function serverError (data, options) {
var request = this.req;
var response = this.res;
var sails = request._sails;
// Logging error to the console.
if (data !== undefined) {
sails.log.error('Sending 500 ("Server Error") response: \n', String(data));
} else {
sails.log.error('Sending empty 500 ("Server Error") response');
}
response.status(500);
if (data instanceof Exception) {
return response.json({
success: false,
exception: {
type: data.constructor.name,
message: data.message
}
});
} else {
return response.json(data);
}
};
控制器抛出异常时:
// api/controllers/TempController.js
var NotFoundException = require('../exceptions/NotFoundException.js');
module.exports = {
index: function(request, response) {
throw new NotFoundException('Specific user is not found.');
}
};
这将输出以下内容 JSON:
{
"success": false,
"exception": {
"type": "NotFoundException",
"message": "Specific user is not found."
}
}
我正在使用 Sails.js 开发 REST API 服务器。
为了便于使用和抽象,我想在我的控制器中抛出异常,例如:
// api/controllers/TempController.js
module.exports = {
index: function(request, response) {
throw new NotFoundException('Specific user is not found.');
throw new AccessDeniedException('You have no permissions to access this resource.');
throw new SomeOtherException('Something went wrong.');
}
};
如何自动捕获这些异常(在全局级别)并将它们转换为有效的 JSON 响应?例如:
{
"success": false,
"exception": {
"type": "NotFoundException",
"message": "Specific user is not found."
}
}
使用内置 serverError
响应来处理此类异常是否是最佳方法?还是创建一些自定义中间件更好?如果是这样,你能提供一个简单的例子吗?
未处理的异常作为第一个参数 data
.
api/responses/serverError.js
中的默认响应
下面是如何处理此类异常的示例:
var Exception = require('../exceptions/Exception.js');
module.exports = function serverError (data, options) {
var request = this.req;
var response = this.res;
var sails = request._sails;
// Logging error to the console.
if (data !== undefined) {
sails.log.error('Sending 500 ("Server Error") response: \n', String(data));
} else {
sails.log.error('Sending empty 500 ("Server Error") response');
}
response.status(500);
if (data instanceof Exception) {
return response.json({
success: false,
exception: {
type: data.constructor.name,
message: data.message
}
});
} else {
return response.json(data);
}
};
控制器抛出异常时:
// api/controllers/TempController.js
var NotFoundException = require('../exceptions/NotFoundException.js');
module.exports = {
index: function(request, response) {
throw new NotFoundException('Specific user is not found.');
}
};
这将输出以下内容 JSON:
{
"success": false,
"exception": {
"type": "NotFoundException",
"message": "Specific user is not found."
}
}