Solved

Coldfusion - notify administrator via email on error

Posted on 2011-02-24
12
705 Views
Last Modified: 2012-05-11
Is it possible to be notified by an email whenenver a coldfusion error occurs ... specifically when there is an email error ... EG: an undeliverable email? ... but also if an error occurs on a page.  I've searched through the administrator and google, but cannot find out how to do this ... if it is even possible.

Max
0
Comment
Question by:MaxwellTurner
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 15

Accepted Solution

by:
myselfrandhawa earned 300 total points
Comment Utility
If you are using Application.cfc you can use the the Following

<cffunction name="onError" returnType="void" output="true">
      <cfargument name="exception" required="true">
      <cfargument name="eventname" type="string" required="true">
      <cfset var errortext = "">

      <cflog file="myapperrorlog" text="#arguments.exception.message#">
      
      <cfsavecontent variable="errortext">
      <cfoutput>
      An error occurred: http://#cgi.server_name##cgi.script_name#?#cgi.query_string#<br />
      Time: #dateFormat(now(), "short")# #timeFormat(now(), "short")#<br />
      
      <cfdump var="#arguments.exception#" label="Error">
      <cfdump var="#form#" label="Form">
      <cfdump var="#url#" label="URL">
      
      </cfoutput>
      </cfsavecontent>
      
      <cfmail to="webster@urdomain.com" from="webster@urdomain.com" subject="Error: #arguments.exception.message#" type="html">
            #errortext#
      </cfmail>
      
      <cflocation url="error.cfm">
      
</cffunction>

The Error.cfm page, you can beautify this and show a custom message to the use that error has occurred and will be fixed asap and you will be notified whenever the error comes up

The above will handle all kinds of error in CF

if you a mailing system and you want to find such error and email you
in that case you can use the cftry cfcatch block and use the cfcatch.detail or cfcatch.message to catch the error and redirect the user to another Page!
0
 
LVL 1

Author Comment

by:MaxwellTurner
Comment Utility
I am not using Application.cfc but I will look into this ... I do know abit about cftry/cfcatch  ... do I just place my cfmail block inside the try & catch tags ... ?

[cftry]
      [cfmail] ...[/cfmail] - to user
      [cfcatch]
          [cfmail] ... [/cfmail] - to administrator
      [/cfcatch]
[/cftry]

Max
0
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
Yes the way you defined, You have to do like that, and everything will work according to you

You can also use the cflog atg inside the cfcatch statement to log all the rrors in a text fiel or you can use the DB to add the entries in the table and later check on that entries
0
 

Expert Comment

by:AltControl
Comment Utility
How to get rid of EE's junk mail? I tried to get rid of my account but there's no delete button. So I changed my e-mail to Experts-Sexchange@mailinator.com, only to find they don't send mail there. So I used a throw away Gmail account instead. So now I am relieved of their shit, I am going to send them some of mine!
0
 
LVL 4

Assisted Solution

by:Hammo777
Hammo777 earned 200 total points
Comment Utility
Most of the email errors I've seen fail silently.  Email goes to the spool folder and if they fail (bad email address) then they go to the undeliverable folder.  We check our undeliverable folder each day.  I believe you would have to write some code to check the undeliverable folder and notify you when a file appears there.  Sometimes email fails because the SMTP server is unavailable.  In that instance I have seen people write code that watches the undeliverable folder and moves mail back to the spool folder so a second attempt can be made to send it.
0
 
LVL 1

Author Comment

by:MaxwellTurner
Comment Utility
@AltControl ... wtf??
@Hammo777 ... hmmm, that made me think ... I can import my mailsent log into a dbase a few times a day via windows scheduled task and then run a cf scheduled task to run a page periodically that will email me if new entries have been added.
@myselfrandhawa ... hmmm that also made think ... can I wrap my entire page in a cftry\catch to catch the other non-email errors (so I don't have to use Application.cfc)?  

Max
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 52

Expert Comment

by:_agx_
Comment Utility
no points..

can I wrap my entire page in a cftry\catch.... (so I don't have to use Application.cfc)?  

You could, but why not use an Application.cfc? Then all your error handling code would be one place
rather than scattered across different pages.

to catch the other non-email errors
As @Hammo777 said you can't catch email errors with cftry/cfcatch anyway.  Mail is spooled and sent by a different thread. So 99% of problems won't be reported to your cfm page, meaning you can't use cftry/cfcatch on them.
0
 
LVL 1

Author Comment

by:MaxwellTurner
Comment Utility
@aqx ... I see the sense in that.  I just don't know much about Application, but I'll have to start learning ... I wasn't 100% sure if 'silent errors' meant that cftry/cfcatch would not catch the error - thanks for confirming that..

Thanks to everyone.  I think I know what I have to do now.

Max
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Setting one up for error handling only shouldn't be too hard. Here's a good intro
http://www.coldfusionjedi.com/index.cfm/2007/11/9/Applicationcfc-Methods-and-Example-Uses

0
 
LVL 1

Author Comment

by:MaxwellTurner
Comment Utility
Just realized that Application.cfc was introduced in v.7 ... I am working with v6.1  and Application.cfm ... does that matter for my purpose?  

@aqx ... thanks for the resource, but I'm at the stage where I am not really sure what Application.xxx is and how/where to use it on my site.,  I wiill try find a beginner to application.cfm, but if you have any suggested reading, I'm all ears.  

Max
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Then you can't use Application.cfc / onError then.  You'd have to use an Application.cfm file instead.

The basics are you create a file named "Application.cfm" and stick in the root folder of your application.  It'll apply to any cfm file in that folder AND any of it's subfolders.  If an error occurs in any of those cfm pages, the error handler in your Application.cfm file will handle it.

Here's a simple example from Ray Camden's site. It's probably where myselfrandhawa got his example ;-)
http://www.coldfusionjedi.com/index.cfm/2007/12/5/The-Complete-Guide-to-Adding-Error-Handling-to-Your-ColdFusion-Application

FILE: Application.cfm
------------------------
<cfapplication name="rayrules">
<cferror type="exception" template="MyErrorHandler.cfm">	

FILE: MyErrorHandler.cfm  (place in same directory as "Application.cfm")
--------------------------
<!--- 
     display a user friendly message first
--->
<h1>My Error Handler</h1>
We are so sorry. Something went wrong. We are working on it now.

<!--- 
     next capture details about the error 
--->
<cfsavecontent variable="errortext">
	<cfoutput>
	An error occurred: http://#cgi.server_name##cgi.script_name#?#cgi.query_string#<br />
	Time: #dateFormat(now(), "short")# #timeFormat(now(), "short")#<br />
	
	<cfdump var="#error#" label="Error">
	<cfdump var="#form#" label="Form">
	<cfdump var="#url#" label="URL">
	
	</cfoutput>
</cfsavecontent>

<!--- 
     finally email yourself the error details
--->
<cfmail to="youremail@yourcompany.com" from="errors@yourcompany.com" subject="Error: #error.message#" type="html">
	#errortext#
</cfmail>

File:   test.cfm    (creates an error to test it out ...)
---------------------
<cfset a = 1>
<cfset b = a / 0>

Open in new window

     

0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
if you have any suggested reading, I'm all ears.

Also check the definitive source of course. Not sure if the 6.1 docs are still on line, but here's v8
http://livedocs.adobe.com/coldfusion/8/Errors_01.html
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now