http://coldfusion.com logo
#cfml-general
Title
# cfml-general
j

Jason Ryan

02/10/2022, 9:21 PM
Is there any way to programmatically ignore the
Application.cfc
onError
function? I have an
isProduction()
function and was hoping I could only enable the error-trapping and re-routing functionality conditionally based on that. It doesn't quite seem to work that way when NOT in prod. It just swallows up errors and doesn't display anything on the page, unless the function is commented out.
t

Tim

02/10/2022, 9:29 PM
I've never tried this so it probably doesn't work, but what if you did
Copy code
onError(any exception) {
    if( !isProduction() ) {
      rethrow;
    }
}
In reality, what I have is more like:
Copy code
onError(any exception) {
    log(exception);
    if( !isProduction() ) {
        writeDump(exception);
    }
}
j

Jason Ryan

02/10/2022, 9:30 PM
I'll give that a shot and let ya know!
Yeah, it doesn't work. I am curious what your writeDump() call does? I was hoping that would output the error on the current template that I'm calling but it doesn't work that way on my end, I see no errors.
m

Matt Dyer

02/10/2022, 9:52 PM
You could try aborting after the writeDump. I think that works in a tag based application.cfc.
j

Jason Ryan

02/10/2022, 10:10 PM
Mine is cfscript based so unfortunately the dump after didn't work 😞
a

Adam Cameron

02/10/2022, 10:29 PM
It doesn't quite seem to work that way when NOT in prod.
It's pretty hard to comment on why your code is not working the way you want if we're just left to guess what it is 😉 Also:
Yeah, it doesn't work.
What does "doesn't work" mean in this context? What does it do? And how does that vary from what you expected?
If
onError
receives an exception object, then
rethrow
is not gonna work I think, as that's pretty much designed to work in a
try
/
catch
construct. If you have an exception object, you can just throw it.
Copy code
onError(any exception) {
    if( !isProduction() ) {
      throw(object=exception);
    }
}
That should work? If it doesn't can you pls give us more to go on than "Yeah, it doesn't work" this time around :-p
t

Tim

02/10/2022, 11:02 PM
What adam's got was going to be my second guess, but the docs said that was java exceptions, and I wasn't sure if CF exceptions were also java exceptions or not. As for what my code does -- technically I'm not using a writeDump. I'm parsing the exception to json for nicer logging, passing that to a View.cfc, and writeOutput-ting the html that the view generates. And it displays a page when there's an error. I believe writeDump isn't doing anything substantially different from that, so I figured it would output the dump of the exception in place of the blank page you're getting now.
j

Jason Ryan

02/11/2022, 3:22 PM
Gentlemen, thank you for your assistance. The writeDump ended up working on the blank error page. It turns out I had an error when trying to log the exception.
a

Adam Cameron

02/11/2022, 3:46 PM
Make sure to have your code in the
onError
handler itself `try`/`catch`ed with just a final fallback to just a
writeLog
entry, and a
rethrow
j

Jason Ryan

02/11/2022, 8:53 PM
Good call. I was meaning to find the correct log function to put in my catch, but thanks to you it just fell in my lap. Appreciate it 🙂
m

Michael Gillespie

02/11/2022, 11:46 PM
This is what I have in mine and it works pretty good
getPageContext().getcfoutput().clearall(); savecontent variable="errorText"{ writeDump(var=arguments, format="text"); } WriteLog(type='Error', application='yes', file='riicms', text=errorText); if (request.env eq 'dev'){ writeDump(arguments) } else{ include "/error.cfm"; }
2 Views