【问题标题】:How to catch "coldfusion.runtime.MissingArgumentException" equivilant in Railo?如何在 Railo 中捕获“coldfusion.runtime.MissingArgumentException”等价物?
【发布时间】:2013-03-12 20:23:03
【问题描述】:

以下是比较 Coldfusion 9.0.1 和 Railo 4.0.2.002

我有以下(截断)作为 cfc 的初始化

component {
    public myComponent function init(
        required string inSetting1,
        required string inSetting2
    ) {
        return this;
    }
}

如果我运行以下命令,那么它会正确调用

objInstance = new myComponent(
    inSetting1 = 'xxx',
    inSetting2 = 'yyy'
);

但是,我试图在没有所有必需参数的情况下处理请求此对象的错误配置。到目前为止,我得到了以下内容

try {
    // inSetting2 is not defined == will cause error
    objInstance = new myComponent(
        inSetting1 = 'xxx'
    );
} catch (coldfusion.runtime.MissingArgumentException e) {

    // This catches the error for CF 9.0.1

} catch (expression e) {

    // This catches the error for Railo 4.0.2
    debug(e); // get the error dump for stack trace below

}

到目前为止,基于堆栈跟踪中的打开 railo.runtime.type.UDFImpl.defineArguments(UDFImpl.java:171)(这是我检索 CF9 的特定类型的方式),我尝试了以下操作但无济于事

  • 雷洛
  • railo.runtime
  • railo.runtime.type
  • railo.runtime.type.UDFImpl
  • railo.runtime.type.UDFImpl.defineArguments

现在虽然我可以执行 catch (expression e) 甚至 catch (any e),但我更希望能够捕获特定错误,原因很简单,因为在我当前正在更新以与 Railo 一起使用的组件中的某些位置依赖于能够确定无法区分配置错误(缺少参数)和部署错误(缺少模板)的原因。

那么,是否有人知道一种方法,我可以通过该方法使上述 try/catch 专门寻找 coldfusion.runtime.MissingArgumentException 的等价物(或者就此而言,我可以在尝试之前检查所需参数的方法调用/运行有问题的方法)

编辑:我忘了包括堆栈跟踪。这是 Railo 堆栈跟踪。我也已经尝试过以下代替expression

 The parameter environment to function init is required but was not
 passed in. at
 railo.runtime.type.UDFImpl.defineArguments(UDFImpl.java:171):171 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:369):369 at
 railo.runtime.type.UDFImpl.callWithNamedValues(UDFImpl.java:275):275
 at railo.runtime.ComponentImpl._call(ComponentImpl.java:608):608 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:490):490 at
 railo.runtime.ComponentImpl.callWithNamedValues(ComponentImpl.java:1800):1800
 at
 railo.runtime.functions.other._CreateComponent.call(_CreateComponent.java:49):49
 at
 mso.dev_test315.mycomponenttest_cfc$cf._1(/var/www/html/myComponentTest.cfc:133):133
 at
 mso.dev_test315.mycomponenttest_cfc$cf.udfCall(/var/www/html/myComponentTest.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.callWithNamedValues(UDFImpl.java:275):275
 at railo.runtime.ComponentImpl._call(ComponentImpl.java:608):608 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:490):490 at
 railo.runtime.ComponentImpl.callWithNamedValues(ComponentImpl.java:1796):1796
 at railo.runtime.tag.Invoke.doComponent(Invoke.java:209):209 at
 railo.runtime.tag.Invoke.doEndTag(Invoke.java:182):182 at
 mxunit.framework.testcase_cfc$cf._2(/var/www/html/mxunit/framework/TestCase.cfc:115):115
 at
 mxunit.framework.testcase_cfc$cf.udfCall(/var/www/html/mxunit/framework/TestCase.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.callWithNamedValues(UDFImpl.java:275):275
 at railo.runtime.ComponentImpl._call(ComponentImpl.java:608):608 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:490):490 at
 railo.runtime.ComponentImpl.callWithNamedValues(ComponentImpl.java:1800):1800
 at
 railo.runtime.util.VariableUtilImpl.callFunctionWithNamedValues(VariableUtilImpl.java:749):749
 at
 railo.runtime.PageContextImpl.getFunctionWithNamedValues(PageContextImpl.java:1521):1521
 at
 mxunit.framework.decorators.dataproviderdecorator_cfc$cf._1(/var/www/html/mxunit/framework/decorators/DataProviderDecorator.cfc:31):31
 at
 mxunit.framework.decorators.dataproviderdecorator_cfc$cf.udfCall(/var/www/html/mxunit/framework/decorators/DataProviderDecorator.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.call(UDFImpl.java:284):284 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:607):607 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:490):490 at
 railo.runtime.ComponentImpl.call(ComponentImpl.java:1781):1781 at
 railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:723):723
 at
 railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:1506):1506
 at
 mxunit.framework.testsuiterunner_cfc$cf._1(/var/www/html/mxunit/framework/TestSuiteRunner.cfc:99):99
 at
 mxunit.framework.testsuiterunner_cfc$cf.udfCall(/var/www/html/mxunit/framework/TestSuiteRunner.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.call(UDFImpl.java:284):284 at
 railo.runtime.type.scope.UndefinedImpl.call(UndefinedImpl.java:774):774
 at
 railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:723):723
 at
 railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:1506):1506
 at
 mxunit.framework.testsuiterunner_cfc$cf._1(/var/www/html/mxunit/framework/TestSuiteRunner.cfc:52):52
 at
 mxunit.framework.testsuiterunner_cfc$cf.udfCall(/var/www/html/mxunit/framework/TestSuiteRunner.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.call(UDFImpl.java:284):284 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:607):607 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:490):490 at
 railo.runtime.ComponentImpl.call(ComponentImpl.java:1781):1781 at
 railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:723):723
 at
 railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:1506):1506
 at
 mxunit.framework.testsuite_cfc$cf._1(/var/www/html/mxunit/framework/TestSuite.cfc:131):131
 at
 mxunit.framework.testsuite_cfc$cf.udfCall(/var/www/html/mxunit/framework/TestSuite.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.call(UDFImpl.java:284):284 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:607):607 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:490):490 at
 railo.runtime.ComponentImpl.call(ComponentImpl.java:1781):1781 at
 railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:723):723
 at
 railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:1506):1506
 at
 mxunit.framework.remotefacade_cfc$cf._1(/var/www/html/mxunit/framework/RemoteFacade.cfc:76):76
 at
 mxunit.framework.remotefacade_cfc$cf.udfCall(/var/www/html/mxunit/framework/RemoteFacade.cfc):-1
 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:103):103 at
 railo.runtime.type.UDFImpl._call(UDFImpl.java:371):371 at
 railo.runtime.type.UDFImpl.call(UDFImpl.java:284):284 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:607):607 at
 railo.runtime.ComponentImpl._call(ComponentImpl.java:498):498 at
 railo.runtime.ComponentImpl.call(ComponentImpl.java:1789):1789 at
 railo.runtime.ComponentWrap.call(ComponentWrap.java:165):165 at
 railo.runtime.net.rpc.server.ComponentController._invoke(ComponentController.java:56):56
 at
 railo.runtime.net.rpc.server.ComponentController.invoke(ComponentController.java:34):34
 at __138.mxunit.framework.remotefacade_wrap.executeTestCase(Unknown
 Source):-1 at sun.reflect.GeneratedMethodAccessor61.invoke(Unknown
 Source):-1 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
 Source):-1 at java.lang.reflect.Method.invoke(Unknown Source):-1 at
 org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397):397
 at
 org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186):186
 at
 org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323):323
 at
 org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32):32
 at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118):118 at
 org.apache.axis.SimpleChain.invoke(SimpleChain.java:83):83 at
 org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454):454
 at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281):281
 at
 railo.runtime.net.rpc.server.RPCServer.doPost(RPCServer.java:312):312
 at
 railo.runtime.ComponentPage.callWebservice(ComponentPage.java:783):783
 at railo.runtime.ComponentPage.call(ComponentPage.java:155):155 at
 railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:801):801
 at
 railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:753):753
 at
 railo.runtime.listener.ModernAppListener._onRequest(ModernAppListener.java:183):183
 at
 railo.runtime.listener.MixedAppListener.onRequest(MixedAppListener.java:18):18
 at
 railo.runtime.PageContextImpl.execute(PageContextImpl.java:2255):2255
 at
 railo.runtime.PageContextImpl.execute(PageContextImpl.java:2222):2222
 at
 railo.runtime.engine.CFMLEngineImpl.serviceCFML(CFMLEngineImpl.java:310):310
 at railo.loader.servlet.CFMLServlet.service(CFMLServlet.java:29):29 at
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728):728 at
 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305):305
 at
 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210):210
 at
 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222):222
 at
 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123):123
 at
 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472):472
 at
 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171):171
 at
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99):99
 at
 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118):118
 at
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407):407
 at
 org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200):200
 at
 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589):589
 at
 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310):310
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown
 Source):-1 at
 java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source):-1
 at java.lang.Thread.run(Unknown Source):-1

【问题讨论】:

  • 如果你cfdump 发现错误,我相信堆栈跟踪会告诉你你想要什么。您可以在此处发布生成的堆栈跟踪吗?
  • 那么当你在Railo中捕捉到错误时,e.type的值是多少?
  • e.type 就是expression。为清楚起见,以上来自作为单元测试的 myComponentTest.cfc,堆栈跟踪来自执行 debug(e);在} catch (any e) {}
  • 我相信这是 Railo 源中的位置,希望能提供一些启示。 github.com/getrailo/railo/blob/master/railo-java/railo-core/src/…
  • Simon,关于您的标签编辑:cfscript 是 cfml 的一部分。此外,CFML 是指 ColdFusion 和 Railo 时使用的术语。

标签: coldfusion error-handling railo cfml


【解决方案1】:

比您当前使用的更简单的解决方法:

try
{
    // standard function call
}
catch (expression e)
{
    if ( NOT refind('The parameter \S+ to function \S+ is required but was not passed in',e.message) )
        rethrow;
}

当然,这依赖于错误信息的措辞,所以有些脆弱。

你也可以做一个不太精确但更灵活的版本,只检查关键字:

if ( NOT find('parameter',e.message) AND find('required',e.message) )

【讨论】:

  • 我曾考虑过这一点,但决定反对它,因为它与 CF 以及 Railo 不兼容,因此仍需要实现特定于 CF 的错误消息。
  • 嗯? CF 兼容很简单:if ( e.type EQ 'coldfusion.runtime.MissingArgumentException' OR ... )
  • 将选择此作为答案,因为它最适合原始问题,尽管不适合我自己的目的 - 最后我还是坚持了我自己的解决方案。
【解决方案2】:

目前正在采用的解决方案(除非在接下来的几天内提供更好的解决方案)将使用以下方法:

boolean function checkRequiredArguments(
    required inFunction,
    required struct inArguments
) {
    // This is intended to check the function for required arguments and ensure they are present in the struct
    var arrFunctionArguments = GetMetaData(arguments.inFunction).parameters;
    for (var thisArgument in arrFunctionArguments) {
        if (
            thisArgument.required // argument is required
            &&
            !IsDefined('arguments.inArguments.'&thisArgument.name) // but not defined
        ) {
            return false;   
        }
    }
    return true;    
}

如果所有必需的参数都有效,这将返回 true。在上面组件的 init 参数的上下文中,以下示例代码将确保所有必需的参数至少以某种形式定义(一些细化将允许这也确认类型)

objTest = CreateObject('myComponent');
stcArguments = {
    inSetting1 = 'XXX'
}
if ( checkRequiredArguments( inFunction = objTest.init , inArguments = stcArguments ) ) {
    objTest.init( argumentCollection=stcArguments );
} else {
    throw(type='MissingArguments');
}

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多