gpt4 book ai didi

ServiceStack 不填充错误响应

转载 作者:行者123 更新时间:2023-12-02 09:30:46 26 4
gpt4 key购买 nike

我们正在开发一个使用 ServiceStack 的项目(喜欢它),但需要一些帮助来解决一个奇怪的问题。在我们的项目中,我们使用辅助方法通过 ErrorResponse 和 ResponseStatus 对象抛出或返回各种类型的 HttpError。

它遵循描述的模式 here 。像这样的事情:

protected HttpError BusinessError(string summaryMessage = "Some generic validation summary.")
{
return new HttpError(HttpStatusCode.BadRequest, "MyValidationType", summaryMessage)
{
Response = new ErrorResponse
{
ResponseStatus = new ResponseStatus
{
ErrorCode = "MyValidationType",
Message = summaryMessage,
Errors = new List<ResponseError>()
},
}
};
}

在服务调用中,我们会像这样使用它:

throw BusinessError("Help I've fallen and can't get up!");

这会很有效,我们会将其输入到 ss-validation JS 库中以呈现我们的验证消息。工作得很好。

问题是,现在 ServiceStack 不会将任何 HttpError 的详细信息序列化到响应中。我们得到的只是包含 400 状态代码、“MyValidationType”错误代码和空 JSON 响应 {} 的响应。

我尝试过组合抛出/返回错误并将服务方法的返回类型切换为object等,但似乎没有什么区别。老实说,我不确定我们可以在项目中进行哪些更改来导致这种行为。

如果有任何关于可能导致此问题的建议或指示,我将不胜感激?

最佳答案

如果您使用 {RequestName}{RequestName}Response 命名约定以及 Response DTO does not have a ResponseStatus property,则可能会发生这种情况。 :

The {RequestDto}Response is returned, regardless of the service method's response type. If the {RequestDto}Response DTO has a ResponseStatus property, it is populated otherwise no ResponseStatus will be returned. (If you have decorated the {ResponseDto}Response class and properties with [DataContract]/[DataMember] attributes, then ResponseStatus also needs to be decorated, to get populated).

在这种情况下,解决方案只是将 ResponseStatus 属性添加到 Response DTO:

public class RequestNameResponse
{
public ResponseStatus ResponseStatus { get; set; }
}

关于ServiceStack 不填充错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com