作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
由于 RAML 规范失败,我正在尝试自定义处理发送给调用者的响应。目前我的代码执行以下操作。
const cfg = require("./cfg");
const log = require('./logging');
const RAML = require('osprey');
const startMessage = "My Service started on port " + cfg.SERVER_PORT + " at " + cfg.API_MOUNT_POINT;
// start an express server
const start = x => {
// server dependencies
const fs = require('fs'),
express = require('express'),
app = express(),
router = express.Router(),
bodyParser = require('body-parser'),
api = require('./api');
RAML.loadFile(cfg.API_SPEC).then(_raml => {
app.use(bodyParser.json({ extended: true }));
// hide the useless "powered by express" header
app.disable('x-powered-by');
// RAML validation
app.use(cfg.API_MOUNT_POINT, _raml);
app.use(cfg.API_MOUNT_POINT, api);
})
.then(v => {
app.listen(cfg.SERVER_PORT, function() {
log.info(startMessage);
});
})
.catch(e => log.error(e));
}
这很好用,但验证失败时发送给调用者的响应如下所示。
{
"errors": [
{
"type": "json",
"dataPath": "redeemtype",
"keyword": "required",
"schema": true,
"message": "Missing required property: redeemtype"
}
],
"stack": "BadRequestError: Request failed to validate against RAML definition\n at createValidationError (/Volumes/Devel/dollardine/node_modules/osprey-method-handler/osprey-method-handler.js:735:14)\n at ospreyJsonBody (/Volumes/Devel/dollardine/node_modules/osprey-method-handler/osprey-method-handler.js:448:21)\n at handle (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:56:16)\n at dispatch (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:39:20)\n at next (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:37:24)\n at jsonParser (/Volumes/Devel/dollardine/node_modules/body-parser/lib/types/json.js:94:7)\n at handle (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:56:16)\n at dispatch (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:39:20)\n at middleware (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:41:16)\n at /Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:10:16\n at ospreyContentType (/Volumes/Devel/dollardine/node_modules/osprey-method-handler/osprey-method-handler.js:325:17)\n at handle (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:56:16)\n at dispatch (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:39:20)\n at next (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:37:24)\n at ospreyMethodHeader (/Volumes/Devel/dollardine/node_modules/osprey-method-handler/osprey-method-handler.js:262:12)\n at handle (/Volumes/Devel/dollardine/node_modules/compose-middleware/lib/index.js:56:16)"
}
这很好,但我不想将所有这些信息发送给来电者。我只想在本地记录并发送 {"code": 400, "message": "Invalid input"}
如何让 osprey 具备处理错误响应的能力?
最佳答案
我找到了我自己问题的答案。以防将来有人卡在这里。
const start = x => {
// server dependencies
const fs = require('fs'),
express = require('express'),
app = express(),
router = express.Router(),
bodyParser = require('body-parser'),
api = require('./api');
const ramlConfig = {
"server": {
"notFoundHandler": false
},
"disableErrorInterception": true
}
osprey.loadFile(cfg.API_SPEC, ramlConfig).then(_raml => {
app.use(bodyParser.json({ extended: true }));
// hide the useless "powered by express" header
app.disable('x-powered-by');
// RAML validation
app.use(cfg.API_MOUNT_POINT, _raml);
app.use(customNotFoundHandler);
app.use(ramlErrorChecker);
app.use(cfg.API_MOUNT_POINT, api);
//app.use(ramlErrorChecker);
})
.then(v => {
app.listen(cfg.SERVER_PORT, function() {
log.info(startMessage);
});
})
.catch(e => log.error(e));
}
const ramlErrorChecker = (err, req, res, next) => {
if (err) {
log.error("RAML validation failed. Reason: " + err);
res.status(400);
return res.json(buildResponseJSON(400));
}
return next();
}
const customNotFoundHandler = (req, res, next) => {
// Check for existence of the method handler.
if (req.resourcePath) {
return next()
}
res.status(404);
return res.json({"message": "The path is not found"});
}
重要的部分是 ramlConfig,它有助于进行一些定制。通过将“disableErrorInterception”设置为 true,我们接管了错误处理,这对标准化来说更好,最重要的是隐藏了正在使用 RAML 的事实。将“notFoundHandler”设置为 false 意味着,未声明的路由将被优雅地拒绝,而不是随机 html。
关于javascript - Osprey RAML 验证错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035782/
由于 RAML 规范失败,我正在尝试自定义处理发送给调用者的响应。目前我的代码执行以下操作。 const cfg = require("./cfg"); const log = require('./
我想用 ffmpeg 从 Directshow 卡(Osprey Card 450e)捕获的实时流视频编码为 mp4 流多播。目前我有这个错误。 ffmpeg -f dshow -i video="O
我是一名优秀的程序员,十分优秀!