如何从 CFC 文件获取异常详细信息

发布于 2024-08-09 10:23:47 字数 457 浏览 1 评论 0原文

我在 CFC 文件中有一个函数,将从 .cfm 文件中调用该函数,如下所示

 <cffunction name="cftest" access="public" returntype="query" output="true" hint="Function returns Records">

      <cfquery name="qryTest" datasource="DBTest">
                select * from emp_tab;
       </cfquery>

    <cfreturn selectRecordsResultSet />

 </cffunction>

如何使用 cftry 处理 DB 异常?因为这是返回查询,是否可以捕获数据库异常并将详细信息从调用它的地方传递到另一个页面?

谢谢

I have a Function in CFC file, which will be called from .cfm file like below

 <cffunction name="cftest" access="public" returntype="query" output="true" hint="Function returns Records">

      <cfquery name="qryTest" datasource="DBTest">
                select * from emp_tab;
       </cfquery>

    <cfreturn selectRecordsResultSet />

 </cffunction>

How can I handle DB Exception using cftry? as this is returning Query, Is it possible to catch the DB Exception and pass the Details to the other page from where it is called?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2024-08-16 10:23:47

这是我通常实现的示例:

<cffunction name="getCurrentRecordsCount" access="public" output="false" returntype="any" hint="Get total history records count">
    <cfargument name="filters" type="struct" required="false" default="#StructNew()#" hint="Filtering rules">
    <cfset var qGetRecordCount = "" />
    <cftry>

        <cfquery datasource="#variables.dsn#" name="qGetRecordCount">
            SELECT COUNT(*) AS cnt FROM ....
        </cfquery>

        <cfreturn qGetRecordCount.cnt />

    <cfcatch type="any">
        <cfreturn error(cfcatch.message, cfcatch.detail) />
    </cfcatch>
    </cftry>
</cffunction>

如果您只想处理数据库错误,请将类型更改为数据库

使用这三种方法执行错误处理和报告:

<cffunction name="error" access="private" output="false" returntype="boolean" hint="Set error status and message">
    <cfargument name="message" type="string" required="true" hint="Error message text" />
    <cfargument name="detail" type="string" required="false" default="" hint="Error detail text" />
    <cfset variables.fError = true />
    <cfset variables.fErrorText = arguments.message />
    <cfif Len(arguments.detail)>
        <cfset variables.fErrorText = variables.fErrorText & " [" & arguments.detail & "]" />
    </cfif>
    <cfreturn false />
</cffunction>

<cffunction name="gotError" access="public" output="false" returntype="boolean" hint="Return latest error flag state">
    <cfreturn variables.fError />
</cffunction>

<cffunction name="getError" access="public" output="false" returntype="string" hint="Return latest error text and reset the flag">
    <cfset var txt = variables.fErrorText />
    <cfset variables.fError = false />
    <cfset variables.fErrorText = "" />
    <cfreturn txt />
</cffunction>

请注意,对于 returntype="void" 的方法,我使用 cfset 而不是 cfreturn:

<cfset error(cfcatch.message, cfcatch.detail) />

因此在代码中我可以执行以下操作(cfscript):

// calculate filtered records count
totalLogCount = request.loggingService.getCurrentRecordsCount(filters);

// check if error was thrown
if (request.loggingService.gotError()) {
   // report the error details somehow
   WriteOutput(request.loggingService.getError());
}

Here is an example of my usual implementation of this:

<cffunction name="getCurrentRecordsCount" access="public" output="false" returntype="any" hint="Get total history records count">
    <cfargument name="filters" type="struct" required="false" default="#StructNew()#" hint="Filtering rules">
    <cfset var qGetRecordCount = "" />
    <cftry>

        <cfquery datasource="#variables.dsn#" name="qGetRecordCount">
            SELECT COUNT(*) AS cnt FROM ....
        </cfquery>

        <cfreturn qGetRecordCount.cnt />

    <cfcatch type="any">
        <cfreturn error(cfcatch.message, cfcatch.detail) />
    </cfcatch>
    </cftry>
</cffunction>

If you want to handle only database errors, change type to the database.

Errors handling and reporting performed using these three methods:

<cffunction name="error" access="private" output="false" returntype="boolean" hint="Set error status and message">
    <cfargument name="message" type="string" required="true" hint="Error message text" />
    <cfargument name="detail" type="string" required="false" default="" hint="Error detail text" />
    <cfset variables.fError = true />
    <cfset variables.fErrorText = arguments.message />
    <cfif Len(arguments.detail)>
        <cfset variables.fErrorText = variables.fErrorText & " [" & arguments.detail & "]" />
    </cfif>
    <cfreturn false />
</cffunction>

<cffunction name="gotError" access="public" output="false" returntype="boolean" hint="Return latest error flag state">
    <cfreturn variables.fError />
</cffunction>

<cffunction name="getError" access="public" output="false" returntype="string" hint="Return latest error text and reset the flag">
    <cfset var txt = variables.fErrorText />
    <cfset variables.fError = false />
    <cfset variables.fErrorText = "" />
    <cfreturn txt />
</cffunction>

Please note that for methods with returntype="void" I use cfset instead of cfreturn:

<cfset error(cfcatch.message, cfcatch.detail) />

So in code I can do the following (cfscript):

// calculate filtered records count
totalLogCount = request.loggingService.getCurrentRecordsCount(filters);

// check if error was thrown
if (request.loggingService.gotError()) {
   // report the error details somehow
   WriteOutput(request.loggingService.getError());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文