# Improve wsgi error handlers feature

**URL:** <https://discuss.tryton.org/t/improve-wsgi-error-handlers-feature/1628>\
**Category:** Ideas\
**Created:** [July 31, 2019, 11:02am UTC](https://discuss.tryton.org/t/improve-wsgi-error-handlers-feature/1628 "2019-07-31T11:02:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@Dan](https://discuss.tryton.org/u/Dan)\
**Post date:** [July 31, 2019, 11:02am UTC](https://discuss.tryton.org/t/improve-wsgi-error-handlers-feature/1628/1 "2019-07-31T11:02:25Z")

</div>

The WSGI Tryton application already has an undocumented error\_handlers feature:

[https://bitbucket.org/tryton/trytond/src/9a7d559cf2cd7a92edc6efd56aafcf64909fd95b/trytond/wsgi.py#lines-113](https://bitbucket.org/tryton/trytond/src/9a7d559cf2cd7a92edc6efd56aafcf64909fd95b/trytond/wsgi.py#lines-113)

Right now, error handlers are passed a handled exception and are expected to return a response object. Yet, in order to be useful as error pages like many web frameworks have it would need some improvements:

1. Error handlers should be passed the request as parameter. This is necessary to localize the response, include some context such as the user or any request inspected information, answer in a proper content-type customized views (i.e. json/xml for rpc or user application calls, html with custom css for web browsers visiting pages like /dbname/ir/html/\*\* or anything a particular deployment might need). And very important, to be able to copy the id paramter for RPC calls, otherwise the Tryton GTK client rejects any response.

2. Error handlers would need a reference to the wsgi app. This would be convenient so as to access other methods like `make_response`.

I imagine the wsgi implementing a decorator to register error handlers like

```auto
class TrytondWSGI(object):

    def __init__ (self):
        # ...
        self.error_handlers = []

    def add_error_handler(self, handler):
        self.error_handlers.append(
            handler. __get__ (self)
        )
        return handler

    # ...

app = TrytonWSGI()

# ...

@app.add_error_handler
def _(self, request, e):
    # ....
    return self.make_response(request, some_data)

```

Does this make sense to you?  
Cheers

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [July 31, 2019, 11:16am UTC](https://discuss.tryton.org/t/improve-wsgi-error-handlers-feature/1628/2 "2019-07-31T11:16:28Z")

</div>

That’s make sense to me. Could you [submit the change](http://www.tryton.org/contribute#develop)?

---

<div class="post-metadata">

**Author:** ![Dan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@Dan](https://discuss.tryton.org/u/Dan)\
**Post date:** [August 1, 2019, 8:21am UTC](https://discuss.tryton.org/t/improve-wsgi-error-handlers-feature/1628/3 "2019-08-01T08:21:34Z")

</div>

I’ll be glad to. I should create an issue, shouldn’t I? [Improve wsgi error handlers feature (#8548) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue8548)

---

<div class="post-metadata">

**Author:** ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)\
**Post date:** [August 1, 2019, 8:33am UTC](https://discuss.tryton.org/t/improve-wsgi-error-handlers-feature/1628/4 "2019-08-01T08:33:00Z")

</div>

> [@Dan](#):
>
> I should create an issue, shouldn’t I?

Yes, and then you should [submit your proposal](http://www.tryton.org/develop)
