Reputation: 21
I have one custom template tag function.
I try raise custom exception and catch it in custom middleware, but template engine process exception and raise TemplateSyntaxError.
Upvotes: 1
Views: 1641
Reputation: 12195
That's because the template engine runs before your response/post-rendering middleware, and is not tolerant of exceptions in template tags -- and nor should it be. How would it know there's middleware waiting in the response cycle that will catch it?
The simplest way forward is probably to pre-check in the view for the condition that would cause the template tag to explode (not seeing any code, I don't know whether this is really possible) and issue a suitable HttpResponse/exception handling change of flow there.
Upvotes: 1