Grails错误处理程序总是收到空指针 [英] Grails error handler always receives null pointer

查看:186
本文介绍了Grails错误处理程序总是收到空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个自定义错误控制器,会在我们发生所有错误后被调用。然而,即使原始错误不是空指针,我们大多数抛出的错误最终都会作为空指针进入控制器。有任何想法吗?下面的代码。 Bootstrap和UrlMappings可根据需要提供。谢谢

Error Handler方法

  def HandleErrors = 
{

def exception = request.exception.cause.class

if(exception)
{
Exception ex = request.exception //这个异常永远是NPE
...

代码块抛出异常。我原本在这里没有尝试捕获,但想要添加它,以便我确信抛出的异常不是NPE。它的文件没有找到异常。

  try {
def writer = new FileWriter(new File(fileSaveLocation));
}
catch(ex)
{
throw(ex)
}

编辑:添加推送到异常处理程序的异常

 异常:org。 codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException 
原因:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.NullPointerException


解决方案

你在错误处理程序中引用的内容为null,所以无意中抛出了另一个异常,这又被捕获了?



你可以尝试更改:

  def exception = request.exception.cause.class 

  def exception = request?.exception?.cause?.class 


We have a custom error controller that gets called after all of our errors. However, most of our errors that get thrown end up coming into the controller as null pointers, even though the original error was not a null pointer. Any ideas? Code below. Bootstrap and UrlMappings available if needed. Thanks

Error Handler method

def HandleErrors =
{

    def exception = request.exception.cause.class  

    if (exception) 
    { 
        Exception ex = request.exception  //This exception is always a NPE
            ...

Block of code throwing the exception. I originally did not have a try catch in here, but wanted to add it so that I was sure the exception being thrown was Not a NPE. Its a file not found exception.

try{
        def writer = new FileWriter( new File(fileSaveLocation));
    } 
    catch ( ex)
    {
        throw(ex)
    }

Edit: Adding the exception that is pushed to the exception handler

Exception:org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException
Cause:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

解决方案

It's not because you're referencing something that is null inside the error handler, and so are inadvertently throwing another exception, which is again caught?

can you try changing:

def exception = request.exception.cause.class  

to

def exception = request?.exception?.cause?.class  

这篇关于Grails错误处理程序总是收到空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆