如何检查 cfcatch 块中的本机代码错误?
How to check native code error in cfcatch block?
我有 cfcatch 块可以捕获任何异常。一旦检测到错误,我将构建以 NativeErrorCode
作为参数的自定义函数。如果错误代码是我要查找的指示 duplicate/PK 违规的错误代码,我会向用户发送自定义消息 return。如果错误代码不是我正在寻找的错误代码,那么全局消息将被 returned。但是,我 运行 在 ColdFusion returned 错误消息 NativeErrorCode
不存在的问题中。我知道本机错误代码是为数据库类型保留的。有没有办法检查类型并防止出现此问题,或者有更好的方法来解决此问题?这是我的代码示例:
<cftry>
// Stored procedure call
<cfcatch type="any">
<cfset local.fnResults = {status : "400", message : Application.functions.errorCatch(cfcatch.NativeErrorCode)}>
</cfcatch>
</cftry>
public string function errorCatch(required string ErrorCode) {
local.message = "";
if(arguments.ErrorCode EQ 2627){
local.message = "Error! Cannot insert duplicate value.";
}else{
local.message = "Error! Please contact your administrator.";
}
return message;
}
您可以在上面看到我的 errorCatch
函数是如何工作的以及我正在检查的代码。我仍然希望 cfcatch
捕获代码中的任何异常,而不仅仅是数据库错误。
想到两种处理分支捕获逻辑的方法,使用 2 个捕获块,或者检查捕获对象是否包含您想要的数据。
在我的第一个示例中,我专门为数据库错误添加了一个 catch 块。如果错误类型是数据库,则将包含本机错误代码,如果数据库驱动程序不包含本机错误代码,则为 -1。对于任何参数,我只是添加了默认的 return 字符串。您可能希望拥有处理非数据库类型异常的自定义逻辑。
<cftry>
// Stored procedure call
<cfcatch type="database">
<cfset local.fnResults = {status : "400", message : Application.functions.errorCatch(cfcatch.NativeErrorCode)}>
</cfcatch>
<cfcatch type="any">
//Non database related error
<cfset local.fnResults = "Error! Please contact your administrator.">
</cfcatch>
</cftry>
在我的第二个示例中,我刚刚更新了您的 errorCatch 函数,在我们尝试传递它之前检查 NativeErrorCode 是否存在。
<cfcatch type="any">
//Passing the default error code value, you may want custom logic here
<cfset local.fnResults = {
status : "400",
message : Application.functions.errorCatch( cfcatch.keyExists("NativeErrorCode")?cfcatch.NativeErrorCode:-1)
}>
</cfcatch>
我有 cfcatch 块可以捕获任何异常。一旦检测到错误,我将构建以 NativeErrorCode
作为参数的自定义函数。如果错误代码是我要查找的指示 duplicate/PK 违规的错误代码,我会向用户发送自定义消息 return。如果错误代码不是我正在寻找的错误代码,那么全局消息将被 returned。但是,我 运行 在 ColdFusion returned 错误消息 NativeErrorCode
不存在的问题中。我知道本机错误代码是为数据库类型保留的。有没有办法检查类型并防止出现此问题,或者有更好的方法来解决此问题?这是我的代码示例:
<cftry>
// Stored procedure call
<cfcatch type="any">
<cfset local.fnResults = {status : "400", message : Application.functions.errorCatch(cfcatch.NativeErrorCode)}>
</cfcatch>
</cftry>
public string function errorCatch(required string ErrorCode) {
local.message = "";
if(arguments.ErrorCode EQ 2627){
local.message = "Error! Cannot insert duplicate value.";
}else{
local.message = "Error! Please contact your administrator.";
}
return message;
}
您可以在上面看到我的 errorCatch
函数是如何工作的以及我正在检查的代码。我仍然希望 cfcatch
捕获代码中的任何异常,而不仅仅是数据库错误。
想到两种处理分支捕获逻辑的方法,使用 2 个捕获块,或者检查捕获对象是否包含您想要的数据。
在我的第一个示例中,我专门为数据库错误添加了一个 catch 块。如果错误类型是数据库,则将包含本机错误代码,如果数据库驱动程序不包含本机错误代码,则为 -1。对于任何参数,我只是添加了默认的 return 字符串。您可能希望拥有处理非数据库类型异常的自定义逻辑。
<cftry>
// Stored procedure call
<cfcatch type="database">
<cfset local.fnResults = {status : "400", message : Application.functions.errorCatch(cfcatch.NativeErrorCode)}>
</cfcatch>
<cfcatch type="any">
//Non database related error
<cfset local.fnResults = "Error! Please contact your administrator.">
</cfcatch>
</cftry>
在我的第二个示例中,我刚刚更新了您的 errorCatch 函数,在我们尝试传递它之前检查 NativeErrorCode 是否存在。
<cfcatch type="any">
//Passing the default error code value, you may want custom logic here
<cfset local.fnResults = {
status : "400",
message : Application.functions.errorCatch( cfcatch.keyExists("NativeErrorCode")?cfcatch.NativeErrorCode:-1)
}>
</cfcatch>