Osprey RAML 验证错误处理

Posted

技术标签:

【中文标题】Osprey RAML 验证错误处理【英文标题】:Osprey RAML Validation Error Handling 【发布时间】:2017-08-19 12:48:16 【问题描述】:

由于 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 让我能够处理错误响应?

【问题讨论】:

【参考方案1】:

我找到了自己问题的答案。以防将来有人卡在这里。

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

【讨论】:

以上是关于Osprey RAML 验证错误处理的主要内容,如果未能解决你的问题,请参考以下文章

Mule 应用程序不断触发错误的流程 - 可能的 RAML 问题

SpringBoot系列五:SpringBoot错误处理(数据验证处理错误页全局异常)

RAML 模拟服务 POST 请求在 Mule 设计中心显示 404 错误

在“422(无法处理的实体)”Laravel 7 中验证错误时出现错误

未处理的拒绝(错误):GraphQL 错误:未经过身份验证

表单验证 - Django 中的注册和登录错误处理