Hey
back again, at 3am:P
Just
looking for some pointers regarding catching errors produced BY the
CFC.
Just
say for example we have this simple CFC as outlined below.
<cffunction name="getUser"
hint="Get the user" returntype="query">
<!--- User ID is required --->
<cfargument name="UserID" required="Yes" type="numeric">
<!--- Query database --->
<cfquery datasource="#request.thisApplication.datasource#" name="getAllowed">
SELECT *
FROM Users
WHERE UserID = #arguments.UserID#
</cfquery>
<!--- Return result --->
<cfreturn getUser>
</cffunction>
<!--- User ID is required --->
<cfargument name="UserID" required="Yes" type="numeric">
<!--- Query database --->
<cfquery datasource="#request.thisApplication.datasource#" name="getAllowed">
SELECT *
FROM Users
WHERE UserID = #arguments.UserID#
</cfquery>
<!--- Return result --->
<cfreturn getUser>
</cffunction>
Now...........this is all good and well and wont error unless no userid
has been passed in. Now the purpose of having a CFC is to encapsulate your
code logic right?? So I ask the question, is there a way to catch the
error that the CFC would make if the required arguments were not passed
in?
There
are many ways I could get around this. For example, I could pull the whole
CFARGUMENT part out of the CFC and test for the arguments myself, see whats
missing and then return the error message. But then whats the point of having
CFARGUMENT available then?
Wouldnt it be better to be able to put a CFTRY around
the CFARGUMENT and everything else inside the CFC?
Like
this...
<cffunction name="getUser" hint="Get the user"
returntype="query">
<cftry>
<!--- User ID is required --->
<cfargument name="UserID" required="Yes" type="numeric">
<!--- Query database --->
<cfquery datasource="#request.thisApplication.datasource#" name="getAllowed">
SELECT *
FROM Users
WHERE UserID = #arguments.UserID#
</cfquery>
<cfcatch>
<cfset getUser = QueryNew("UserID,UserName")>
</cfcatch>
</cftry>
<!--- Return result --->
<cftry>
<!--- User ID is required --->
<cfargument name="UserID" required="Yes" type="numeric">
<!--- Query database --->
<cfquery datasource="#request.thisApplication.datasource#" name="getAllowed">
SELECT *
FROM Users
WHERE UserID = #arguments.UserID#
</cfquery>
<cfcatch>
<cfset getUser = QueryNew("UserID,UserName")>
</cfcatch>
</cftry>
<!--- Return result --->
<cfreturn
getUser>
</cffunction>
</cffunction>
This
to me is encapsulating code, where by all processing of code is handled in the
one spot, in the CFC. Not to mention the less code you will have to write
when calling your components, not only in CF, but in flash
also.
To be
able to handle all your exeptions from within your CFC, to me, seems way to
logical to not be able to do it.
So
this issue I am adding to my CFC wishlist.
Regards
Steve Onnis
Domain Concept Designs
+61 422
337 685
+61 3 9431 4249
| http://www.domainconceptdesigns.com [EMAIL PROTECTED] |
http://www.cfcentral.com.au [EMAIL PROTECTED] | |
|
|
("If you think it can't be done, you haven't asked me!") - Steve Onnis
---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
MX Downunder AsiaPac DevCon - http://mxdu.com/
