Developer forum

Forum » CMS - Standard features » Handling Razor Parsing and runtime exceptions

Handling Razor Parsing and runtime exceptions

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

We're seeing more and more issues with errors in Razor templates; either because of a parser error (which we can usually fix ourselves) or a runtime error. As an example, I attached a SQL time out error that happened when accessing an area item.

Unfortunately, these errors are sent back with a 200 OK status so there isn't anything we can do to capture them other than wrap all of our code in try/catch blocks.

Could this be resolved somehow? I see a few options:

1. Return a proper 500 Server error code. This would allow us to intercept the exception and display a custom error page and log the error.

2. Provide a "On Razor Exception" type of notification subscriber. This would make it easy to change the behavior of how Razor works without affecting existing solutions. In the subscriber we could log the error and then send the user to an appropriate error page.

Any thoughts?

Imar

Error.png

Replies

 
Nicolai Høeg Pedersen
Reply

Hi Imar

We could give you a checkbox (yeah!) to return 500 on razor exceptions?

BR  Nicolai

 
Lars Larsen
Reply

Would be nice if runtime error could be handled and the user redirected to an error page.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yeah, would you agree a checkbox makes the most sense? Or maybe a radio between 200 and 500 to have fewer checkboxes? ;-)

 
Frederik Gadegaard
Reply

Hello, 

I know this is an old post. 

But did you end up enabling the handling of errors happening in razor

If I force an error I get the following error, and I'm not reaching "Application_Error" in GlobalAsax.cs which I think is the desired outcome? 

PS. Im currently using a DW version 8.8.1.17.

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Frederik

Yes, this thread is 4 years old and Dynamicweb can be configured to throw a 500 exception on template and module errors. See dump. But this is DW9 only.

Your solution is maybe 6 years old and does not have that option.

BR Nicolai

Capture.JPG
Votes for this answer: 1
 
Frederik Gadegaard
Reply

Thank you for the fast reply! 

We are in the middle of an upgrade - so this is great news :) 

 

Kind regards 

Frederik

 
Maik Van Sark
Reply

Hi Nicolai,

Is there any reason why it returns a StatusCode 302 instead of 500? Am I missing a setting?

 
Nicolai Pedersen
Reply

That is your own (custom) web.config?

That there, is a 302 redirect, so remove that section.

 
Maik Van Sark
Reply

Ah I found the solution. Indeed the custom errors were causing the 302. I had to use httpErrors together with de DW settings. Thanks!

1. Turn off custom errors

<customErrors mode="Off" />

2. Replace error pages

<httpErrors errorMode="Custom" existingResponse="Replace">

  <error statusCode="500" path="Error500.html" responseMode="File" />

</httpErrors>

 

You must be logged in to post in the forum