librelist archives

« back to archive

Catch all exceptions

Catch all exceptions

From:
Bryan Vicknair
Date:
2012-08-11 @ 13:43
I use the errorhandler decorator to give the user nice error pages
when my views raise HTTPExceptions.  How can I catch all
non-HTTPExceptions so that I can control what the user sees?

I can't predict what exceptions might be raised, so using the
errorhandler decorator won't work.

I tried listening to the got_request_exception signal, but I wasn't
able to return a request, and raising an HTTPException from within my
signal handler didn't trigger my registered error handler functions
for HTTPExceptions.

Bryan

Re: [flask] Catch all exceptions

From:
Shriram Kunchanapalli
Date:
2012-08-11 @ 14:46
http://librelist.com/browser//flask/2012/7/12/default-error-handler/

On 8/11/12, Bryan Vicknair <bryanvick@gmail.com> wrote:
> I use the errorhandler decorator to give the user nice error pages
> when my views raise HTTPExceptions.  How can I catch all
> non-HTTPExceptions so that I can control what the user sees?
>
> I can't predict what exceptions might be raised, so using the
> errorhandler decorator won't work.
>
> I tried listening to the got_request_exception signal, but I wasn't
> able to return a request, and raising an HTTPException from within my
> signal handler didn't trigger my registered error handler functions
> for HTTPExceptions.
>
> Bryan
>

Re: [flask] Catch all exceptions

From:
Adrian Ratnapala
Date:
2012-08-14 @ 05:08
You can still use the decorator and give the class "Exception" as an
error code.  This will catch all non HTTPException errors.

After that, depending on what you do, there is some weird behaviour
which I can't remember exactly.  (See for example
https://github.com/mitsuhiko/flask/issues/552)

On 11 August 2012 16:46, Shriram Kunchanapalli <kshriram18@gmail.com> wrote:
> http://librelist.com/browser//flask/2012/7/12/default-error-handler/
>
> On 8/11/12, Bryan Vicknair <bryanvick@gmail.com> wrote:
>> I use the errorhandler decorator to give the user nice error pages
>> when my views raise HTTPExceptions.  How can I catch all
>> non-HTTPExceptions so that I can control what the user sees?
>>
>> I can't predict what exceptions might be raised, so using the
>> errorhandler decorator won't work.
>>
>> I tried listening to the got_request_exception signal, but I wasn't
>> able to return a request, and raising an HTTPException from within my
>> signal handler didn't trigger my registered error handler functions
>> for HTTPExceptions.
>>
>> Bryan
>>



-- 
Adrian Ratnapala
mobile: +49 1515 277 0000

NGBitS GmbH
Rainer Strasse 7
83104 Ostermünchen

Web: www.ngbits.com

Re: [flask] Catch all exceptions

From:
Mark Haase
Date:
2012-08-17 @ 16:53
Actually, I don't think that's true. Flask checks for error handlers by the
name of the exception class only; it does not take into account the
inheritance tree of the exception class. See my previous posting on this
subject:


http://librelist.com/browser//flask/2012/7/12/default-error-handler/#17f8dd068eedc207fadfb1725705c6cd

If you have evidence to the contrary, please correct me.

On Tue, Aug 14, 2012 at 1:08 AM, Adrian Ratnapala <
adrian.ratnapala@gmail.com> wrote:

> You can still use the decorator and give the class "Exception" as an
> error code.  This will catch all non HTTPException errors.
>
> After that, depending on what you do, there is some weird behaviour
> which I can't remember exactly.  (See for example
> https://github.com/mitsuhiko/flask/issues/552)
>
> On 11 August 2012 16:46, Shriram Kunchanapalli <kshriram18@gmail.com>
> wrote:
> > http://librelist.com/browser//flask/2012/7/12/default-error-handler/
> >
> > On 8/11/12, Bryan Vicknair <bryanvick@gmail.com> wrote:
> >> I use the errorhandler decorator to give the user nice error pages
> >> when my views raise HTTPExceptions.  How can I catch all
> >> non-HTTPExceptions so that I can control what the user sees?
> >>
> >> I can't predict what exceptions might be raised, so using the
> >> errorhandler decorator won't work.
> >>
> >> I tried listening to the got_request_exception signal, but I wasn't
> >> able to return a request, and raising an HTTPException from within my
> >> signal handler didn't trigger my registered error handler functions
> >> for HTTPExceptions.
> >>
> >> Bryan
> >>
>
>
>
> --
> Adrian Ratnapala
> mobile: +49 1515 277 0000
>
> NGBitS GmbH
> Rainer Strasse 7
> 83104 Ostermünchen
>
> Web: www.ngbits.com
>



-- 
Mark E. Haase
CISSP
Sr. Security Software Engineer
www.lunarline.com
3300 N Fairfax Drive, Suite 308, Arlington, VA 22201
571-334-8408

"Solutions Built on Security" TM
Lunarline, Inc. is an ISO 9001 and CMMI Level 2 Certified SDVOSB
Information Assurance\ Cyber Security Services Company.

Re: [flask] Catch all exceptions

From:
Smartboy
Date:
2012-08-17 @ 17:22
I think that it does some extra checking if the exception inherits from
Flask's built-in HTTPException in order to map those to a status code like
404. Non-HTTPException classes could still be caught by using
@app.errorhandler(Exception). I do this in a project I'm working on to
catch server errors and send a JSON string rather than the default HTML
page.

Smartboy

On Fri, Aug 17, 2012 at 9:53 AM, Mark Haase <mark.haase@lunarline.com>wrote:

> Actually, I don't think that's true. Flask checks for error handlers by
> the name of the exception class only; it does not take into account the
> inheritance tree of the exception class. See my previous posting on this
> subject:
>
>
> 
http://librelist.com/browser//flask/2012/7/12/default-error-handler/#17f8dd068eedc207fadfb1725705c6cd
>
> If you have evidence to the contrary, please correct me.
>
> On Tue, Aug 14, 2012 at 1:08 AM, Adrian Ratnapala <
> adrian.ratnapala@gmail.com> wrote:
>
>> You can still use the decorator and give the class "Exception" as an
>> error code.  This will catch all non HTTPException errors.
>>
>> After that, depending on what you do, there is some weird behaviour
>> which I can't remember exactly.  (See for example
>> https://github.com/mitsuhiko/flask/issues/552)
>>
>> On 11 August 2012 16:46, Shriram Kunchanapalli <kshriram18@gmail.com>
>> wrote:
>> > http://librelist.com/browser//flask/2012/7/12/default-error-handler/
>> >
>> > On 8/11/12, Bryan Vicknair <bryanvick@gmail.com> wrote:
>> >> I use the errorhandler decorator to give the user nice error pages
>> >> when my views raise HTTPExceptions.  How can I catch all
>> >> non-HTTPExceptions so that I can control what the user sees?
>> >>
>> >> I can't predict what exceptions might be raised, so using the
>> >> errorhandler decorator won't work.
>> >>
>> >> I tried listening to the got_request_exception signal, but I wasn't
>> >> able to return a request, and raising an HTTPException from within my
>> >> signal handler didn't trigger my registered error handler functions
>> >> for HTTPExceptions.
>> >>
>> >> Bryan
>> >>
>>
>>
>>
>> --
>> Adrian Ratnapala
>> mobile: +49 1515 277 0000
>>
>> NGBitS GmbH
>> Rainer Strasse 7
>> 83104 Ostermünchen
>>
>> Web: www.ngbits.com
>>
>
>
>
> --
> Mark E. Haase
> CISSP
> Sr. Security Software Engineer
> www.lunarline.com
> 3300 N Fairfax Drive, Suite 308, Arlington, VA 22201
> 571-334-8408
>
> "Solutions Built on Security" TM
> Lunarline, Inc. is an ISO 9001 and CMMI Level 2 Certified SDVOSB
> Information Assurance\ Cyber Security Services Company.
>
>

Re: [flask] Catch all exceptions

From:
Adrian Ratnapala
Date:
2012-08-19 @ 11:51
On 17 August 2012 18:53, Mark Haase <mark.haase@lunarline.com> wrote:
> Actually, I don't think that's true. Flask checks for error handlers by the
> name of the exception class only; it does not take into account the
> inheritance tree of the exception class. See my previous posting on this
> subject:

Yes-and-no, Flask cares whether your exception is an `HTTPException`,
beyond that it it's not bothered.

From `Flask.handle_user_exception`:

      if isinstance(e, HTTPException) and not self.trap_http_exception(e):
            return self.handle_http_exception(e)

This check is why you had to what you said in your previous post: if
not for it, defining a handler for  `Exception` would have been
enough.  But instead, you had to do something special for
`HTTPExceptions`.

And finally, there's my personal bugbear:
https://github.com/mitsuhiko/flask/issues/552

-- 
Adrian Ratnapala
mobile: +49 1515 277 0000

NGBitS GmbH
Rainer Strasse 7
83104 Ostermünchen

Web: www.ngbits.com

Re: [flask] Catch all exceptions

From:
Mark Haase
Date:
2012-08-27 @ 20:42
As a followup:

I have noticed that in production, generic exceptions (e.g. raise
Exception("FOO") ) do get handled by my error handler, even though I did
not explicitly register it as such.

I think that an uncaught exception is considered by Flask to be a 500
server error in production mode (WSGI) but not in development mode
(built-in server), but I haven't debugged far enough to be 100% certain. To
confuse things further, the error handler receives the original exception,
not an HTTPException, so any logic that checks for HTTP error codes will
not work.

It's a bit confusing. The manual could probably use some work explaining
this a little better. I'd offer to help but I'm still a bit lost myself.

On Sun, Aug 19, 2012 at 7:51 AM, Adrian Ratnapala <
adrian.ratnapala@gmail.com> wrote:

> On 17 August 2012 18:53, Mark Haase <mark.haase@lunarline.com> wrote:
> > Actually, I don't think that's true. Flask checks for error handlers by
> the
> > name of the exception class only; it does not take into account the
> > inheritance tree of the exception class. See my previous posting on this
> > subject:
>
> Yes-and-no, Flask cares whether your exception is an `HTTPException`,
> beyond that it it's not bothered.
>
> From `Flask.handle_user_exception`:
>
>       if isinstance(e, HTTPException) and not self.trap_http_exception(e):
>             return self.handle_http_exception(e)
>
> This check is why you had to what you said in your previous post: if
> not for it, defining a handler for  `Exception` would have been
> enough.  But instead, you had to do something special for
> `HTTPExceptions`.
>
> And finally, there's my personal bugbear:
> https://github.com/mitsuhiko/flask/issues/552
>
> --
> Adrian Ratnapala
> mobile: +49 1515 277 0000
>
> NGBitS GmbH
> Rainer Strasse 7
> 83104 Ostermünchen
>
> Web: www.ngbits.com
>



-- 
Mark E. Haase
CISSP
Sr. Security Software Engineer
www.lunarline.com
3300 N Fairfax Drive, Suite 308, Arlington, VA 22201
571-334-8408

"Solutions Built on Security" TM
Lunarline, Inc. is an ISO 9001 and CMMI Level 2 Certified SDVOSB
Information Assurance\ Cyber Security Services Company.