Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Alternative to CFTRY/CFCATCH

Posted on 2011-10-12
18
Medium Priority
?
714 Views
Last Modified: 2013-12-24
In my environment, we are restricted from using CFTRY/CFCATCH for anything.  I am attempting to param the URL attributes for a page.  I have listed the code below...can anyone advise an alternative method to this and still get the same result?
<!--- Kill extra output. --->
<cfsilent>

  <!--- Param the URL attributes. --->
  <cftry>
	  <cfparam
		  name="REQUEST.Attributes.date"
		  type="numeric"
		  default="#REQUEST.DefaultDate#"
		  />
	  <cfcatch>
		  <cfset REQUEST.Attributes.date = REQUEST.DefaultDate />
	  </cfcatch>
  </cftry>
  
  <cftry>
	  <cfparam
		  name="URL.month"
		  type="numeric"
		  default="#Month( REQUEST.Attributes.date )#"
		  />
	  <cfcatch>
		  <cfset URL.month = Month( REQUEST.Attributes.date ) />
	  </cfcatch>
  </cftry>
  
  <cftry>
	  <cfparam
		  name="URL.year"
		  type="numeric"
		  default="#Year( REQUEST.Attributes.date )#"
		  />
	  <cfcatch>
		  <cfset URL.year = Year( REQUEST.Attributes.date ) />
	  </cfcatch>
  </cftry>

<!--- Based on the month and year, let's get the first 
	  day of this month. In case the year or 
           month are not valid, put this in a try / catch.
  --->
  <cftry>
	  <cfset dtThisMonth = CreateDate( 
		  URL.year, 
		  URL.month, 
		  1 
		  ) />
	  
	  <cfcatch>
<!--- If there was an error, just default the month 
	view to be the current month. --->
		  <cfset dtThisMonth = CreateDate(
			  Year( Now() ),
			  Month( Now() ),
			  1
			  ) />
	  </cfcatch>
  </cftry>
</cfsilent>

Open in new window

0
Comment
Question by:Lee R Liddick Jr
  • 6
  • 5
  • 5
  • +1
18 Comments
 
LVL 36

Expert Comment

by:SidFishes
ID: 36959352
you are restricted from proper error handling? That makes no sense. Do the people have have mandated this have any reason that has any basis??

I know that doesn't answer your question (and frankly I don't think there -is- an alternative) but sometimes pointless restrictions need to be challenged (and you can quote me on that ;)
0
 
LVL 53

Expert Comment

by:_agx_
ID: 36959432
I'm pretty sure you could re-write it and simplify to achieve something similar.  But as far as cftry/cfcatch ....

> That makes no sense

+1. There is no reasonable alternative to a try / catch.  The best you could do is try and handle most possibilities with a ridiculous series of cfif's:

      <cfif not structKeyExists(request.attributes, "date") or
                  not IsNumeric(request.attribute.date)>
            <cfset request.attributes.date = REQUEST.DefaultDate>
      </cfif>
      <cfif not structKeyExists(URL, "month") or
                  not IsNumeric(URL.month)>
            <cfset URL.month = Month( REQUEST.Attributes.date )>
      </cfif>
        .... etc....

But ultimately, if you missed anything your page will just blow up without the try/catch. The <cfsilent> won't prevent that afaik. There's just no substitute for try/catch.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 36959491
I will have to look at the specific reasons but it's just the challenge I'm presented with.  I will try what you suggested agx...thanks!
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 53

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 36959510
I'm heading out, but here's a quick and dirty re-write.  I didn't have time to work through all the logic to see if it could be simplified further - but I'd be surprised if it couldn't.  

	<cfparam name="REQUEST.Attributes.date" default="#REQUEST.DefaultDate#" />
	
	<!--- note: not sure if this value is a date or number. 
               so change IsDate() to IsNumeric() if needed 
         --->
	<!--- if it's not a valid date, use the default instead --->
	<cfif not IsDate(REQUEST.Attributes.date)>
		<cfset REQUEST.Attributes.date = REQUEST.DefaultDate />
	</cfif>

	<!--- if the month is missing or invalid ...--->
	<cfif not (structKeyExists(URL, "month") AND IsNumeric(URL.month))>
		<cfset URL.month = Month( REQUEST.Attributes.date ) />
	</cfif>
	
	<!--- if the year is missing or invalid ...--->
	<cfif not (structKeyExists(URL, "year") AND IsNumeric(URL.year))>
		<cfset URL.Year = Year( REQUEST.Attributes.date ) />
	</cfif>

	<cfset dateString = url.year &"-"& url.month &"-01">
	<cfif isDate(dateString)>
		<!--- convert it to date time object --->
		<cfset dtThisMonth = parseDateTime(dateString)>
	<cfelse>
		<cfset dtThisMonth = CreateDate( Year( Now() ), Month( Now() ), 1)>
	</cfif>
	<cfoutput>#dtThisMonth#</cfoutput>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36959968
Looking at the code, it seems to me that you want the value of

REQUEST.Attributes.date

and

URL.month

and

URL.year

to all be in sync?   Don't you?

For example, if the date is 10/1/2011, but on the URL the month passed is 9 and the year is 2009, do you really want a date that doesn't match the month and year?  Or is this an overly cautious way to just get tha date and find the month and year?

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36962336
Ok, I certainly admin that this code snipped doesn't match the logic of the askers code 100%, but I am really wondering if leer's logic is what he wants?

The following logic is:  if the month and year passed in on the URL can make a valid date, then use it, otherwise, use the first day of the month found in request.attributes.date, if that is not a valid date, then use the first day of the current month.

leer, if that's not it, can you tell us your desired algorithm?


<cfparam name="url.month" default="">
<cfparam name="url.year" default="">
<cfset dtThisMonth = url.month & "/1/" & url.year>
<cfif NOT isDate(dtThisMonth)>
   <cfset dtThisMonth = Month(REQUEST.Attributes.date) & "/1/" & Year(REQUEST.Attributes.date)>
</cfif>
<cfif NOT isDate(dtThisMonth)>
  <cfset dtThisMonth = createDate(Year(Now()),Month(Now()),1)>
</cfif>

Open in new window

0
 

Author Comment

by:Lee R Liddick Jr
ID: 36962361
Server administrator reasoning:
Using CTRY/CFCATCH/CFTHROW/CFRETHROW to capture exceptions can be useful, however, it also hides errors from the ColdFusion Application log. This creates a situation in which the server administrators have NO means of determining where a problem may lie.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36962388
> Ok, I certainly admin that this code snipped doesn't match the

Lol, can anyone decipher this?

"Ok, I certainly admit that this code snippet doesn't match..."
0
 

Author Comment

by:Lee R Liddick Jr
ID: 36962417
It's a numeric way of gathering the date.  The calendar displays defaulting to the current month and highlights the current day.  There are links to go back to the last month or go forward to the next month.  If I clicked the link for September 2011 (which would be last month), the url would look something like this:
calendar_index.cfm?viewas=month&date=40787
If I clicked the link for November 2011 (which would be next month), the url would look like this:
calendar_index.cfm?viewas=month&date=40848
You can also see a week view, so if i click for the current week, the url would be:
calendar_index.cfm?viewas=week&date=40829
And if I wanted to view just the day, the url would be:
calendar_index.cfm?viewas=day&date=40829

With what agx suggested, it works for the month...I'm just coding my week and day pages to replace those CFTRY/CFCATCH.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36962474
hmmm, I don't see how those examples correlate to the code.  The URL is always showing "date" not "month" and "year"   also, you never mentioned Request.attributes.date, where does that come into play?

I just think the code is doing more work than it needs to with all those catches anyway...

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36962484
> however, it also hides errors from the ColdFusion Application log.

Wow, that's is scary.

I use cfthrow as part of my regular validation code to trap invalid entries.   Do they want "Last Name is Required" to be logged?  

So, rather than handling any problem at the page level, they are forcing you show a "Failure" page and send the user  out of their area all together.    So, if there is a bug in one small aspect (say go-back-one-year), instead of showing the user a friendly message that "the system is not alble to satisfy their request at this time" -and allow them to continue work, you have to throw them out of the app...

I think you should post who is hosting company is so it can be avoided...

0
 
LVL 36

Expert Comment

by:SidFishes
ID: 36962697
"Using CTRY/CFCATCH/CFTHROW/CFRETHROW to capture exceptions can be useful, however, it also hides errors from the ColdFusion Application log. "

Yes errors don't show up in the log when you use CFcatch - because they -aren't errors- at that point. they are properly handled code execution

if your admins want to see log entries it's as simple as adding

cfcatch>
  <cfset URL.year = Year( REQUEST.Attributes.date ) />
  <cflog file="myAppErrors" value="This is the proper way to handle runtime errors: #cfcatch.type# - #cfcatch.message# - #cfcatch.detail#">
</cfcatch>

0
 
LVL 36

Expert Comment

by:SidFishes
ID: 36962714
"server administrators have NO means of determining where a problem may lie."

maybe server administrators need to do a little refresher course cause frankly they are just plain wrong here. And you can quote me on that too ;)
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 36962722
oops not value sb text

<cflog file="myAppErrors" TEXT="This.....
0
 

Author Comment

by:Lee R Liddick Jr
ID: 36962727
Hey SidFishes...can't use CFLOG either.  Hahahahahahaha.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 36962742
I just deal with it and work around it.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 36962799
In any case...it all works now.
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 36962814
can't use cfcatch cause errors aren't logged and can't use cflog - just nuts.

GD, agx maybe can we start an online petition so leerljr68 can use these basic tools and quit wasting time trying to work around unreasonable & silly restrictions.

yeesh
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question