Tag wrapping made easy!
The other day I was in a state of head banging frustration while debugging an issue with a component invoked via a CF Gateway that was peppered with calls to the ImageCR CFX, and seemed to be failing at random points for random reasons (though through no fault of ImageCR, it should be noted). My examples will use ImageCR for reference, but this should be a method you can apply to any ColdFusion Tag, Custom Tag, or CFX.
I spent a good hour racking my brain, trying to write a custom tag that would allow me to wrap any code in a try/catch block that would email me a CFDump of the CFCatch struct, like so:
[viewcode] src="cfx_imagecr3_support1.cfm.txt" showsyntax=no geshi=cfm[/viewcode]
This would have made it very easy to wrap any block of code in a try/catch that will email me on error. As it turns out, this isn't possible without some clever use of CFFile and Regex to read the contents of the calling template and locate the block of code between the start and end tag of the custom tag. But I'm sure you're already thinking about how inefficient that would be, especially in my case where there are 20, 30, maybe more instances. And I agree with you.
Then, it hit me like a ton of bricks: Take one step back -- just the one tag causing my grief, not a general code block -- and write a wrapper for that one tag. And to make things even simpler: Use AttributeCollection! I can pass easily everything sent to my wrapper on to the CFX as an AttributeCollection, without knowing what it is.
So I slammed together a quick wrapper custom tag (File name: "/customtags/wrappers/cfx_imagecr3.cfm") as this:
[viewcode] src="cfx_imagecr3.cfm.txt" showsyntax=no geshi=cfm[/viewcode]
Notice that after I save the contents of my special attributes (trymailto and trysubject), I delete them from the attributes struct. Different tags and CFXes will react differently to unexpected attributes, so it's best not to include them.
After simply importing my new tag wrapper:
[viewcode] src="cfx_imagecr3_support.cfm.txt" showsyntax=no lines=1 geshi=cfm[/viewcode]
This tag:
[viewcode] src="cfx_imagecr3_support.cfm.txt" showsyntax=no lines=2 geshi=cfm[/viewcode]
Becomes this tag:
[viewcode] src="cfx_imagecr3_support.cfm.txt" showsyntax=no lines=3 geshi=cfm[/viewcode]
I can customize the to address and subject of the email per-usage, if I like, and I get an email any time one of them fails.
in ColdFusion Posted 2007-12-18 07:55





