servicestack,Exception Handling,servicestack" /> servicestack,Exception Handling,servicestack" />

Exception handling ServiceStack捕获(WebServiceException ex)-错误代码错误

Exception handling ServiceStack捕获(WebServiceException ex)-错误代码错误,exception-handling,servicestack,Exception Handling,servicestack,在ServiceStack服务中,我抛出一个具有内部异常的异常。当我在客户端捕获WebServiceRequest时,ErrorCode是内部异常类型名称 这对我来说很糟糕,因为它不允许我响应服务器上抛出的特定异常类型 我不明白为什么ServiceStack是这样设计的。捕获较低级别的异常并用更多信息(有时是最终用户友好的异常)封装它们是非常典型的 如何更改默认行为,使其使用表面级异常而不是最内部异常?在查看了上的第一个示例后,我决定签出.HandleException,如下所示: pu

在ServiceStack服务中,我抛出一个具有内部异常的异常。当我在客户端捕获WebServiceRequest时,ErrorCode是内部异常类型名称

这对我来说很糟糕,因为它不允许我响应服务器上抛出的特定异常类型

我不明白为什么ServiceStack是这样设计的。捕获较低级别的异常并用更多信息(有时是最终用户友好的异常)封装它们是非常典型的


如何更改默认行为,使其使用表面级异常而不是最内部异常?

在查看了上的第一个示例后,我决定签出.HandleException,如下所示:

    public static object HandleException(IResolver iocResolver, object request, Exception ex)
    {
        if (ex.InnerException != null && !(ex is IHttpError))
            ex = ex.InnerException;

        var responseStatus = ex.ToResponseStatus();

        if (EndpointHost.DebugMode)
        {
            // View stack trace in tests and on the client
            responseStatus.StackTrace = GetRequestErrorBody(request) + ex;
        }

        Log.Error("ServiceBase<TRequest>::Service Exception", ex);

        if (iocResolver != null)
            LogErrorInRedisIfExists(iocResolver.TryResolve<IRedisClientsManager>(), request.GetType().Name, responseStatus);

        var errorResponse = CreateErrorResponse(request, ex, responseStatus);

        return errorResponse;
    }

看起来你不需要维护一堆代码。您正在编写一个方法来实现自己的错误处理。您可以尝试在自己的方法中调用DtoUtils.HandleException(this、request、exception)并修改返回的HttpError对象。不确定您是否有权更改正在查找的所有属性/值

public static object HandleException(IResolver iocResolver, object request, Exception ex)
{
    HttpError err = (HttpError)DtoUtils.HandleException(this, request, ex);
    err.Reponse = ex.InnerException; 
}
catch (WebServiceException ex)
{
    if (ex.ErrorCode == typeof (SomeKindOfException).Name)
    {
        // do something useful here
    }
    else throw;
}
public static object HandleException(IResolver iocResolver, object request, Exception ex)
{
    HttpError err = (HttpError)DtoUtils.HandleException(this, request, ex);
    err.Reponse = ex.InnerException; 
}