记录来自 ASP.NET Web API 的自定义错误代码
Documenting custom error codes from ASP.NET Web API
是否有任何最佳做法来记录来自 Web API 调用的可能错误代码 return?我指的是与标准 HTTP return 代码相对的自定义逻辑错误。
例如,考虑一个 API 方法来允许用户更改他们的密码。一个可能的错误条件可能是提供的新密码之前已经被该用户使用过(即,密码历史要求)。您可以使用以下代码将其传达给调用者:
public HttpResponseMessage ChangePassword(string oldPassword, string newPassword)
{
try
{
passwordService.ChangePassword(oldPassword, newPassword)
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
switch(ex.Message)
{
case "PasswordHistoryFailed":
return Request.CreateResponse(HttpStatusCode.BadRequest, new CustomErrorClass("FailedHistoryRequirements"));
break;
...
}
}
}
在此示例中,我使用自定义错误 class 来包装自定义错误代码 "FailedHistoryRequirements"。对于此操作,我可能会有更多错误代码,例如 24 小时内密码更改次数过多或其他。
我想知道是否有一种公认的方法可以在方法的 XML 代码注释中自动记录这些自定义错误代码,以便它可以被 Swashbuckle/Swagger 或类似的文档生成器使用.
我通过捕获特定的异常类型来做到这一点,而不是解析消息。
这里我将 MyDepartmentCentricBaseException 作为自定义异常。我可能有 2-3 个异常来自它。但是通过使用基本异常,我的异常捕获更加清晰。
try
{
/* do something */
}
catch (MyDepartmentCentricBaseException deptEx)
{
HttpResponseException hrex = this.GetDepartmentMissingHttpResponseException(deptEx.DepartmentSurrogateKey);
throw hrex;
}
catch (Exception ex)
{
/* log it somewhere !*/
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
private HttpResponseException GetDepartmentMissingHttpResponseException(int DepartmentSurrogateKey)
{
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Department with DepartmentSurrogateKey = {0}", DepartmentSurrogateKey)),
ReasonPhrase = "DepartmentSurrogateKey Not Found"
};
HttpResponseException returnEx = new HttpResponseException(resp);
return returnEx;
}
这里还有其他想法:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling
但我不知道有什么方法可以使用文档来自动巫毒教。 :(
如果你使用Swagger,你可以使用SwaggerResponse
属性。
查看此博客post:
https://mattfrear.com/2015/04/21/generating-swagger-example-responses-with-swashbuckle/
是否有任何最佳做法来记录来自 Web API 调用的可能错误代码 return?我指的是与标准 HTTP return 代码相对的自定义逻辑错误。
例如,考虑一个 API 方法来允许用户更改他们的密码。一个可能的错误条件可能是提供的新密码之前已经被该用户使用过(即,密码历史要求)。您可以使用以下代码将其传达给调用者:
public HttpResponseMessage ChangePassword(string oldPassword, string newPassword)
{
try
{
passwordService.ChangePassword(oldPassword, newPassword)
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
switch(ex.Message)
{
case "PasswordHistoryFailed":
return Request.CreateResponse(HttpStatusCode.BadRequest, new CustomErrorClass("FailedHistoryRequirements"));
break;
...
}
}
}
在此示例中,我使用自定义错误 class 来包装自定义错误代码 "FailedHistoryRequirements"。对于此操作,我可能会有更多错误代码,例如 24 小时内密码更改次数过多或其他。
我想知道是否有一种公认的方法可以在方法的 XML 代码注释中自动记录这些自定义错误代码,以便它可以被 Swashbuckle/Swagger 或类似的文档生成器使用.
我通过捕获特定的异常类型来做到这一点,而不是解析消息。
这里我将 MyDepartmentCentricBaseException 作为自定义异常。我可能有 2-3 个异常来自它。但是通过使用基本异常,我的异常捕获更加清晰。
try
{
/* do something */
}
catch (MyDepartmentCentricBaseException deptEx)
{
HttpResponseException hrex = this.GetDepartmentMissingHttpResponseException(deptEx.DepartmentSurrogateKey);
throw hrex;
}
catch (Exception ex)
{
/* log it somewhere !*/
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
private HttpResponseException GetDepartmentMissingHttpResponseException(int DepartmentSurrogateKey)
{
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Department with DepartmentSurrogateKey = {0}", DepartmentSurrogateKey)),
ReasonPhrase = "DepartmentSurrogateKey Not Found"
};
HttpResponseException returnEx = new HttpResponseException(resp);
return returnEx;
}
这里还有其他想法:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling
但我不知道有什么方法可以使用文档来自动巫毒教。 :(
如果你使用Swagger,你可以使用SwaggerResponse
属性。
查看此博客post:
https://mattfrear.com/2015/04/21/generating-swagger-example-responses-with-swashbuckle/