Swagger-node - 捕获multer“意外字段”错误

时间:2018-04-08 02:32:38

标签: node.js swagger multer swagger-tools swagger-node

我正在使用swagger-node v0.7.5来处理涉及上传文件的小项目。

要上传的文件已在YAML文件中声明,并且按预期工作,但由于以下皱纹我无法解决。

如果客户端发送了一个未在YAML中声明的额外文件字段(“意外字段”),则在控制台中出现以下错误,并且还将其作为500错误发送到客户端:

Error: Unexpected field
application.js:630
    at makeError (c:\node_projects\open-protocol-loader-v2\node_modules\multer\lib\make-error.js:12:13)
    at wrappedFileFilter (c:\node_projects\open-protocol-loader-v2\node_modules\multer\index.js:40:19)
    at Busboy.<anonymous> (c:\node_projects\open-protocol-loader-v2\node_modules\multer\lib\make-middleware.js:114:7)
    at emitMany (events.js:146:13)
    at Busboy.emit (events.js:223:7)
    at Busboy.emit (c:\node_projects\open-protocol-loader-v2\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (c:\node_projects\open-protocol-loader-v2\node_modules\busboy\lib\types\multipart.js:213:13)
    at emitOne (events.js:115:13)
    at PartStream.emit (events.js:210:7)
    at HeaderParser.<anonymous> (c:\node_projects\open-protocol-loader-v2\node_modules\dicer\lib\Dicer.js:51:16)
    at emitOne (events.js:115:13)
    at HeaderParser.emit (events.js:210:7)
    at HeaderParser._finish (c:\node_projects\open-protocol-loader-v2\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH.<anonymous> (c:\node_projects\open-protocol-loader-v2\node_modules\dicer\lib\HeaderParser.js:40:12)
    at emitMany (events.js:146:13)
    at SBMH.emit (events.js:223:7)
    at SBMH._sbmh_feed (c:\node_projects\open-protocol-loader-v2\node_modules\streamsearch\lib\sbmh.js:159:14)
    at SBMH.push (c:\node_projects\open-protocol-loader-v2\node_modules\streamsearch\lib\sbmh.js:56:14)
    at HeaderParser.push (c:\node_projects\open-protocol-loader-v2\node_modules\dicer\lib\HeaderParser.js:46:19)
    at Dicer._oninfo (c:\node_projects\open-protocol-loader-v2\node_modules\dicer\lib\Dicer.js:197:25)
    at SBMH.<anonymous> (c:\node_projects\open-protocol-loader-v2\node_modules\dicer\lib\Dicer.js:127:10)
    at emitMany (events.js:146:13)

我希望能够忽略客户端发送的未在Swagger YAML中声明的任何字段/文件。

swagger.yaml

swagger: "2.0"
info:
  version: "0.0.1"
  title: XSD Parser
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /import_xsd:
    x-swagger-router-controller: import_xsd
    post:
      description: Returns success or failure of the XSD import.
      operationId : parse_xsd
      consumes:
        - multipart/form-data
      parameters:
        - name: xsd_file
          in: formData
          type: file
          description: The file to upload
          required: true
      responses:
        "200":
          description: Success
          schema:
            $ref : "#/definitions/ImportXsdResponse"

        "400":
          description: Bad Request. Something sent by the client wasn't right.
          schema:
            $ref: "#/definitions/ErrorResponse"

        "500":
          description: Server Error. The server had an ouchie.
          schema:
            $ref: "#/definitions/ErrorResponse"

        default:
          description: Something went wrong.
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
  ImportXsdResponse:
    required:
      - message
    properties:
      message:
        type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

控制器:import_xsd.js

'use strict'

var fs = require('fs')
var uuid = require('uuid').v4

module.exports = {
    parse_xsd : function(request, response){

        fs.writeFile(`./uploads/${uuid()}`, request.swagger.params.xsd_file.value.buffer, function(error){
            if(error){
                debug(error)
            }
        })

        response.json({
            "message" : "File was uploaded successfully."
        })
    }
}

0 个答案:

没有答案