代码之家  ›  专栏  ›  技术社区  ›  Jeremy

如何使用虚张声势注释错误

  •  0
  • Jeremy  · 技术社区  · 5 年前

    我用了一个虚张声势的Nuget软件包。net框架webapi。

    我试图对api响应进行注释。这对于成功响应非常好,我只需指定对象模型的类型:

    [SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(WebAPI.Models.APIResponse), Description = "Successful operation.")]
    

    如果我返回这样的错误:

    return this.Request.CreateErrorResponse(HttpStatusCode.NotFound, "Document not found.");
    

    我得到以下json/xml响应

    {
      "Message": "Document not found."
    }
    
    <Error>
        <Message>Document not found.</Message>
    </Error>
    

    如果存在未经处理的异常,我将得到一个http 500,其中包含如下json/xml响应(不是,只有在从本地计算机执行rest调用时才会返回异常详细信息):

    {
      "Message": "An error has occurred.",
      "ExceptionMessage": "This is a test error",
      "ExceptionType": "System.Exception",
      "StackTrace": "   at WebAPI.Controllers.MyController.Post(Guid id) in ..."
    }
    
    <Error>
        <Message>An error has occurred.</Message>
        <ExceptionMessage>This is a test error</ExceptionMessage
        <ExceptionType>System.Exception</ExceptionType>
        <StackTrace>   at WebAPI.Controllers.MyController.Post(Guid id) in ...</StackTrace>
    </Error>
    

    对于这些回答,我该如何用虚张声势的语言来诠释它们?

    我试过:

    [SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(HttpResponseMessage), Description = "Error.")]
    
    [SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(HttpResponseException), Description = "Error.")]
    
    [SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(Exception), Description = "Error.")]
    

    在模型示例部分中,我要么得到消息“Object is not a primitive”,就像异常和HttpResponse的情况一样,要么示例没有反映响应的实际外观。例如,HttpResponseException在swagger用户界面中看起来像这样:

    {
      "Response": {},
      "Message": "string",
      "Data": {},
      "InnerException": {},
      "StackTrace": "string",
      "HelpLink": "string",
      "Source": "string",
      "HResult": 0
    }
    
    <?xml version="1.0"?>
    <HttpResponseException>
      <!-- invalid XML -->
      <Message>string</Message>
      <Data>
        <!-- additional elements allowed -->
      </Data>
      <!-- invalid XML -->
      <StackTrace>string</StackTrace>
      <HelpLink>string</HelpLink>
      <Source>string</Source>
      <HResult>1</HResult>
    </HttpResponseException>
    
    0 回复  |  直到 5 年前
    推荐文章