处理过的果园异常仍在冒泡

时间:2021-12-14 04:04:39

In my custom Orchard module I have written some ftp client code within a controller action that happens to throw a custom FtpException during login. I've wrapped the Login code in a try-catch and am sending a notification message back to the view if there's an error. The problem is that the error is still bubbling up and showing the standard "Oops. Something went wrong ... sorry" Orchard error message. Here's the code I am wrapping:

在我的自定义Orchard模块中,我在一个控制器操作中编写了一些ftp客户端代码,在登录过程中会抛出一个自定义的FtpException。我将登录代码封装在try-catch中,如果出现错误,我将向视图发送一条通知消息。问题是错误仍然冒出来并显示标准的“哎呀”。事情错了……对不起”果园错误消息。下面是我正在包装的代码:

        try
        {
            ftpClient.Login();
            ftpClient.Upload(fileName);
        }
        catch (FtpException ex)
        {
            services.Notifier.Error(T("There was an error sending the file - {0}.", ex.Message));
            return RedirectToAction("Edit", "Project");
        }
        finally
        {
            ftpClient.Close();
        }

        services.Notifier.Information(T("File uploaded successfully."));
        return RedirectToAction("Edit", "Project");

If I look in the Orchard logs, I see that an FTP exception is thrown, from within ftpClient.Login(). The code in the catch block is executed, so I know that the exception has been handled. Trouble is, the unhandled exception behaviour is still being displayed by Orchard. How can I stop the exception from bubbling up so that my RedirectToAction will fire?

如果我查看Orchard日志,我将看到一个FTP异常从ftpClient.Login()中抛出。catch块中的代码被执行,所以我知道这个异常已经被处理了。问题是,乌节仍然在显示未处理的异常行为。我如何阻止异常冒泡,以使我的重定向操作启动?

1 个解决方案

#1


2  

As discussed in the comments, it is possible that the following is happening:

正如评论中所讨论的,有可能发生以下情况:

  1. Execution enters the try, and gets to the call to Login(), and throws an exception.
  2. 执行进入try,并调用Login(),并抛出异常。
  3. Execution jumps to your catch block, which attempts to call Close() on your ftpClient object.
  4. 执行跳转到catch块,该块试图调用ftpClient对象上的Close()。
  5. I'm unsure of the exact type of ftpClient, but it makes sense that calling Close() on an instance that may not have necessarily been opened (via Login in the first place) would throw another exception, and it is that which is being caught by Orchard's exception handler.
  6. 我不确定ftpClient的确切类型,但是对一个可能没有被打开的实例(首先通过登录)调用Close()会抛出另一个异常,而Orchard的异常处理程序正在捕捉这个异常。

I'd start by looking for (or adding) some kind of IsOpen property to the FTP client class, or by wrapping the Login call in a separate try/catch so you can distinguish between a failure to login and another failure. Or if you have access to the source, you could get Login to throw a different type of exception if it fails.

首先,我将查找(或添加)某种IsOpen属性到FTP客户端类中,或者将登录调用包装在一个单独的try/catch中,以便您可以区分登录失败和另一个失败。或者,如果您可以访问源代码,那么如果失败,您可以通过登录来抛出不同类型的异常。

#1


2  

As discussed in the comments, it is possible that the following is happening:

正如评论中所讨论的,有可能发生以下情况:

  1. Execution enters the try, and gets to the call to Login(), and throws an exception.
  2. 执行进入try,并调用Login(),并抛出异常。
  3. Execution jumps to your catch block, which attempts to call Close() on your ftpClient object.
  4. 执行跳转到catch块,该块试图调用ftpClient对象上的Close()。
  5. I'm unsure of the exact type of ftpClient, but it makes sense that calling Close() on an instance that may not have necessarily been opened (via Login in the first place) would throw another exception, and it is that which is being caught by Orchard's exception handler.
  6. 我不确定ftpClient的确切类型,但是对一个可能没有被打开的实例(首先通过登录)调用Close()会抛出另一个异常,而Orchard的异常处理程序正在捕捉这个异常。

I'd start by looking for (or adding) some kind of IsOpen property to the FTP client class, or by wrapping the Login call in a separate try/catch so you can distinguish between a failure to login and another failure. Or if you have access to the source, you could get Login to throw a different type of exception if it fails.

首先,我将查找(或添加)某种IsOpen属性到FTP客户端类中,或者将登录调用包装在一个单独的try/catch中,以便您可以区分登录失败和另一个失败。或者,如果您可以访问源代码,那么如果失败,您可以通过登录来抛出不同类型的异常。