简体   繁体   English

使用ServiceStack处理错误消息

[英]Handling error messages with ServiceStack

Is there a recommended way to keep error messages within the requested objects from a webservice? 是否有建议的方法将错误消息保留在Web服务的请求对象中?

In some examples I see the webservices returning a wrapper class containing some HTTP error codes, others hold messages of .NET-exceptions. 在某些示例中,我看到Web服务返回了一个包装类,其中包含一些HTTP错误代码,其他类则包含.NET异常消息。 On the other hand there are the examples returning the plain objects or list of objects. 另一方面,有一些示例返回普通对象或对象列表。

The error handling can appear confusing at first. 首先,错误处理可能会引起混乱。 The official documentation is here . 官方文档在这里 But essentially ServiceStack tries to return errors in a consistent way so your client always knows how to handle them. 但是本质上,ServiceStack会尝试以一致的方式返回错误,因此您的客户端始终知道如何处理错误。 You should throw exceptions in the normal c# way, ServiceStack will catch these and encapsulated it in a ResponseStatus object, which looks like this: 您应该以普通的c#方式抛出异常,ServiceStack会捕获这些异常并将其封装在ResponseStatus对象中,该对象如下所示:

public class ResponseStatus 
{
    public string ErrorCode { get; set; }
    public string Message { get; set; }
    public string StackTrace { get; set; }
    public List<ResponseError> Errors { get; set; }
}

Full definition here 完整定义在这里

So you will receive a response object that contains a property called ResponseStatus having the populated ResponseStatus object above. 因此,您将收到一个响应对象,该对象包含一个名为ResponseStatus的属性,上面具有已填充的ResponseStatus对象。

Things to note: 注意事项:

  • The StackTrace property will only be included if you enable debug mode in your ServiceStack AppHost Config. 仅当在ServiceStack AppHost Config中启用debug模式时,才会包括StackTrace属性。 ie: 即:

     SetConfig(new HostConfig { DebugMode = true }); 

    This will be the .NET exception you are referring to, and is shown conditionally depending if DebugMode is set. 这将是您所引用的.NET异常,并根据是否设置了DebugMode而有条件地显示。


  • The Errors list of ResponseError is only populated for validation error failures. 仅针对验证错误失败填充ResponseErrorErrors列表。 This will be the list of validation errors. 这将是验证错误的列表。 Which is why you see in some examples a plain error response (ie not a validation error) , and a response with a list of errors attached (validation error) . 因此,在某些示例中,您会看到一个简单的错误响应(即不是验证错误)和带有附加错误列表的响应(验证错误) You should also read the section about validation in the documentation . 您还应该阅读文档中有关验证的部分。 The format of ResponseError object is this: ResponseError对象的格式是这样的:

     public class ResponseError { public string ErrorCode { get; set; } public string FieldName { get; set; } public string Message { get; set; } } 

    The ErrorCode and Message of the ResponseStatus object will be taken from the first item of the Errors list. ResponseStatus对象的ErrorCodeMessage将取自Errors列表的第一项。

Where things get confusing for some people is how, to include the ResponseStatus as a property of your expected response request. 对于某些人来说,令人困惑的地方是如何将ResponseStatus包含在预期响应请求的属性中。

So assuming your wanted to return a Person object such as this: 因此,假设您想返回诸如以下的Person对象:

class Person
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

If a validation exception was thrown for a request that returns this Person response you may get a JSON response like this: 如果针对返回此Person响应的请求引发了验证异常,则您可能会得到如下JSON响应:

{
    "ResponseStatus": {
         "ErrorCode": "ShouldNotBeEmpty",
         "Message": "'FirstName' should not be empty",
         "StackTrace": "..."
         "Errors": [
             {
                 "ErrorCode": "ShouldNotBeEmpty",
                 "FieldName": "FirstName",
                 "Message": "'FirstName' should not be empty"
             },
             {
                 "ErrorCode": "ShouldNotBeEmpty",
                 "FieldName": "LastName",
                 "Message": "'LastName' should not be empty"
             },
         ]
    }
}

Note that the response object of Person is not included in this response, just an object containing the ResponseStatus property. 请注意,此响应中不包括Person的响应对象,只是包含ResponseStatus属性的对象。 If we want to include the response object with the status we must declare our person class this way: 如果我们想将响应对象包含在状态中,则必须以这种方式声明我们的人员类:

class PersonResponse
{
    ResponseStatus ResponseStatus { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
}

Then when an exception is thrown the response will include the response object and it's status: 然后,当引发异常时,响应将包括响应对象及其状态:

{
    "FirstName": "",
    "LastName": "",
    "ResponseStatus": {
        ...

ServiceStack provides you with a lot of control over the error format you return when an exception is thrown, it's best you read through the official documentation I linked to above to understand it. ServiceStack为您提供了对引发异常时返回的错误格式的很多控制,最好阅读以上我链接的官方文档以了解它。 Customisation is more of an advanced topic. 定制更多是一个高级主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM