librelist archives

« back to archive

Pass parameter with HTTPException

Pass parameter with HTTPException

From:
Vladimir Mihailenco
Date:
2010-10-26 @ 13:15
Hello.

What's the easiest way to pass parameter with HTTPException to the exception
handler? I want something like this:

flask.abort(404, my_param='value')

Re: [flask] Pass parameter with HTTPException

From:
Armin Ronacher
Date:
2010-10-26 @ 13:33
Hi,

On 10/26/10 3:15 PM, Vladimir Mihailenco wrote:
> What's the easiest way to pass parameter with HTTPException to the
> exception handler? I want something like this:
You could raise the exception yourself and attach something ahead of time:

from werkzeug.exceptions import NotFound
error = NotFound()
error.some_value = 'foo'
raise error

If you want that as a general interface I recommend writing a function 
for you that creates the exception, attaches something and raises 
similar to how abort works.


Regards,
Armin

Re: [flask] Pass parameter with HTTPException

From:
Vladimir Mihailenco
Date:
2010-10-26 @ 13:55
Thank you, I wrote this:

def abort(status_code, description=None, **kwargs):
    try:
        flask.abort(status_code, description)
    except werkzeug.exceptions.HTTPException, exc:
        exc.__dict__.update(kwargs)
        raise

I forgot to mention that I am using it like this to get access to the route
param in error handler:

@app.route('/<param>')
def my_view(param):
    utils.abort(404, my_param=param)

@app.errorhandler(404)
def not_found(error):
    error.my_param

Is it ok?

On Tue, Oct 26, 2010 at 4:33 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 10/26/10 3:15 PM, Vladimir Mihailenco wrote:
> > What's the easiest way to pass parameter with HTTPException to the
> > exception handler? I want something like this:
> You could raise the exception yourself and attach something ahead of time:
>
> from werkzeug.exceptions import NotFound
> error = NotFound()
> error.some_value = 'foo'
> raise error
>
> If you want that as a general interface I recommend writing a function
> for you that creates the exception, attaches something and raises
> similar to how abort works.
>
>
> Regards,
> Armin
>