librelist archives

« back to archive

Default error handler?

Default error handler?

From:
Mark Haase
Date:
2012-07-12 @ 20:22
Is there a way to set a default error handler? I have a couple specific
error handlers (404 and 500), but I'd like a catchall for all other
possible errors.

    def handle_http_exception(self, e):
        """Handles an HTTP exception.  By default this will invoke the
        registered error handlers and fall back to returning the
        exception as response.

        .. versionadded:: 0.3
        """
        handlers = self.error_handler_spec.get(request.blueprint)
        if handlers and e.code in handlers:
            handler = handlers[e.code]
        else:
            handler = self.error_handler_spec[None].get(e.code)
        if handler is None:
            return e
        return handler(e)

Looking at this code, it looks like there is no way to set a default
error handler, but I just want to double check.

Re: [flask] Default error handler?

From:
Daniel Arbanas
Date:
2012-07-12 @ 20:50
Hi,

On Thu, Jul 12, 2012 at 10:22 PM, Mark Haase <mark.haase@lunarline.com>wrote:

> Is there a way to set a default error handler? I have a couple specific
> error handlers (404 and 500), but I'd like a catchall for all other
> possible errors.
>
>
>
 @app.errorhandler(Exception)
 def catch_all(e):
     ...

Something like that?

Re: [flask] Default error handler?

From:
Mark Haase
Date:
2012-07-12 @ 21:58
Daniel,

I tried doing that, but it doesn't do what I want. E.g.:

@app.errorhandler(Exception)
def generic_error_handler(error):
    return render_template("errors/generic.html", **view), error.code

@app.route("/test")
def test():
    abort(404)


If I go to the /test URL, I see Flask's built-in 404 error handler, not my
own error handler.

I ended up doing this instead:

def generic_error_handler(error):
    return render_template("errors/generic.html", **view), error.code

for error in range(400, 420) + range(500,506):
    app.error_handler_spec[None][error] = generic_error_handler


This works well enough for my needs.

On Thu, Jul 12, 2012 at 4:50 PM, Daniel Arbanas <daniel.arbanas@gmail.com>wrote:

> Hi,
>
> On Thu, Jul 12, 2012 at 10:22 PM, Mark Haase <mark.haase@lunarline.com>wrote:
>
>> Is there a way to set a default error handler? I have a couple specific
>> error handlers (404 and 500), but I'd like a catchall for all other
>> possible errors.
>>
>>
>>
>>
>  @app.errorhandler(Exception)
>  def catch_all(e):
>      ...
>
> Something like that?
>



-- 
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] Default error handler?

From:
Shriram Kunchanapalli
Date:
2012-07-18 @ 03:22
Hello Mark,

Thanks for the this snippet.
I don't have a blueprint

According to description below:
http://flask.pocoo.org/docs/api/#flask.Flask.error_handler_spec
does it still work ?

In the generic.html, do you then redirect to the appropriate error page
based on error code or how does it work ?

Shriram

On Fri, Jul 13, 2012 at 3:28 AM, Mark Haase <mark.haase@lunarline.com>wrote:

> Daniel,
>
> I tried doing that, but it doesn't do what I want. E.g.:
>
> @app.errorhandler(Exception)
> def generic_error_handler(error):
>     return render_template("errors/generic.html", **view), error.code
>
> @app.route("/test")
> def test():
>     abort(404)
>
>
> If I go to the /test URL, I see Flask's built-in 404 error handler, not my
> own error handler.
>
> I ended up doing this instead:
>
> def generic_error_handler(error):
>     return render_template("errors/generic.html", **view), error.code
>
> for error in range(400, 420) + range(500,506):
>     app.error_handler_spec[None][error] = generic_error_handler
>
>
> This works well enough for my needs.
>
> On Thu, Jul 12, 2012 at 4:50 PM, Daniel Arbanas <daniel.arbanas@gmail.com>wrote:
>
>> Hi,
>>
>> On Thu, Jul 12, 2012 at 10:22 PM, Mark Haase <mark.haase@lunarline.com>wrote:
>>
>>> Is there a way to set a default error handler? I have a couple specific
>>> error handlers (404 and 500), but I'd like a catchall for all other
>>> possible errors.
>>>
>>>
>>>
>>>
>>>
>>  @app.errorhandler(Exception)
>>  def catch_all(e):
>>      ...
>>
>>  Something like that?
>>
>
>
>
> --
> 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] Default error handler?

From:
Mark Haase
Date:
2012-07-18 @ 13:22
Shriram,

I actually removed some of my code to make my point clear. What I am
actually doing is creating a directory of error pages, like
errors/404.html, errors/500.html, etc. I also have a errors/generic.html
that is used if an error occurs and there is no template for that error
code. Here's what I am actually using:

def generic_error_handler(error):
    view = {
        "code": error.code,
        "description": error.description,
        "name": error.name,
        "page_title": "Error %d %s" % (error.code, error.name),
    }

    try:
        return render_template("errors/%d.html" % error.code, **view),
error.code
    except:
        return render_template("errors/generic.html", **view), error.code

for error in range(400, 420) + range(500,506):
    app.error_handler_spec[None][error] = generic_error_handler


I hope this helps.

On Tue, Jul 17, 2012 at 11:22 PM, Shriram Kunchanapalli <
kshriram18@gmail.com> wrote:

> Hello Mark,
>
> Thanks for the this snippet.
> I don't have a blueprint
>
> According to description below:
> http://flask.pocoo.org/docs/api/#flask.Flask.error_handler_spec
> does it still work ?
>
> In the generic.html, do you then redirect to the appropriate error page
> based on error code or how does it work ?
>
> Shriram
>
>
> On Fri, Jul 13, 2012 at 3:28 AM, Mark Haase <mark.haase@lunarline.com>wrote:
>
>> Daniel,
>>
>> I tried doing that, but it doesn't do what I want. E.g.:
>>
>> @app.errorhandler(Exception)
>> def generic_error_handler(error):
>>     return render_template("errors/generic.html", **view), error.code
>>
>> @app.route("/test")
>> def test():
>>     abort(404)
>>
>>
>> If I go to the /test URL, I see Flask's built-in 404 error handler, not
>> my own error handler.
>>
>> I ended up doing this instead:
>>
>> def generic_error_handler(error):
>>     return render_template("errors/generic.html", **view), error.code
>>
>> for error in range(400, 420) + range(500,506):
>>     app.error_handler_spec[None][error] = generic_error_handler
>>
>>
>> This works well enough for my needs.
>>
>> On Thu, Jul 12, 2012 at 4:50 PM, Daniel Arbanas <daniel.arbanas@gmail.com
>> > wrote:
>>
>>> Hi,
>>>
>>> On Thu, Jul 12, 2012 at 10:22 PM, Mark Haase <mark.haase@lunarline.com>wrote:
>>>
>>>> Is there a way to set a default error handler? I have a couple specific
>>>> error handlers (404 and 500), but I'd like a catchall for all other
>>>> possible errors.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>  @app.errorhandler(Exception)
>>>  def catch_all(e):
>>>      ...
>>>
>>>  Something like that?
>>>
>>
>>
>>
>> --
>> 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] Default error handler?

From:
Simon Sapin
Date:
2012-07-13 @ 06:11
Le 12/07/2012 23:58, Mark Haase a écrit :
>
> I ended up doing this instead:
>
>     def generic_error_handler(error):
>          return render_template("errors/generic.html", **view), error.code
>
>     for error in range(400, 420) + range(500,506):
>          app.error_handler_spec[None][error] = generic_error_handler
>
>

You could also make a subclass of Flask and override handle_http_exception.

-- 
Simon Sapin