I would like to stop publishing when a certain condition exists in Tridion 2011. In a Razor template I try:
throw new Exception("Exceptional condition!");
But, in Template Builder it says 'successful'.
Any ideas?
In VBScript templates we would do Err.Raise and it would throw the error back to the Publish Queue.
There's definitely not any try/catch mechanisms in the base Razor Mediator code, an error in the template should surface (as you see with the common null reference errors you probably run into a lot while razoring it up!). There's some things you should definitely check:
Is the code path with the exception even being executed? As Bart suggested, you should run it through Template Builder and see if there are any useful logs there. Throw in your own debug statements as well to locate where the code path is going.
Is your Exception in a correctly formatted razor statement?
If its a new CT, does the Page Template's code allow the rendering of your CT?
I just setup a sample TBB as follows (and added TBB to a Component Template):
<div>Testing an Error</div>
@{ throw new Exception("Exceptional condition"); }
Running through Template Builder showed the error in the Output window. Previewing the page with a component using the CT would fail and show the error. Publishing the page showed "Failed" as status, with the Processed Items detailing showing the "Exceptional condition!" message.
Hope that helps, if you are still having trouble with it feel free to post your code and I'll debug it further!
Sounds to me like the Razor Mediator is catching your exception and not re-throwing it. But a quick look at the code didn't give me a direct indication of where that would happen.
There are quite a few try, catch blocks in there (some are probably correct and expected), most are logging warnings.
Do you get anything interesting in the template builder log? If not I would try loading the Razor Mediator code and debug it while you run your Template, to see what catches your exception and if you can easily change that (good thing it's open source).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With