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

这对我不利,因为它不允许我响应服务器上抛出的特定异常类型。

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

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

最佳答案

在查看 https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling 的第一个示例后,我决定查看 DtoUtils .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;
    }

第一条指令用它的内部异常替换异常。我不确定那是什么想法。这对我来说似乎违反直觉,所以我只是在我的 AppHost 类中重新实现了该方法,删除了第一个 if 语句块:
    public override void Configure(Container container)
    {
        ServiceExceptionHandler += (request, exception) => HandleException(this, request, exception);
    }

    /// <remarks>
    /// Verbatim implementation of DtoUtils.HandleException, without the innerexception replacement.
    /// </remarks>
    public static object HandleException(IResolver iocResolver, object request, Exception ex)
    {
        var responseStatus = ex.ToResponseStatus();

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

        var log = LogManager.GetLogger(typeof(DtoUtils));
        log.Error("ServiceBase<TRequest>::Service Exception", ex);

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

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

        return errorResponse;
    }

这显然不理想,因为我不得不复制一堆与我在原始实现中遇到的问题完全无关的代码。这让我觉得每次更新 ServiceStack 时都必须维护这个方法。我很想在这里找到一种更好的方法来实现这一目标。

无论如何,我在客户端代码中有我喜欢的异常处理:
catch (WebServiceException ex)
{
    if (ex.ErrorCode == typeof (SomeKindOfException).Name)
    {
        // do something useful here
    }
    else throw;
}

关于exception-handling - ServiceStack catch (WebServiceException ex) - 有错误的错误代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14982504/

10-17 02:10