Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs / errors are lost due to wsgi.errors not extracted #51

Closed
pupssman opened this issue Jan 11, 2018 · 4 comments · Fixed by #52
Closed

Logs / errors are lost due to wsgi.errors not extracted #51

pupssman opened this issue Jan 11, 2018 · 4 comments · Fixed by #52
Labels

Comments

@pupssman
Copy link
Contributor

Hey there and thanks for a fancy tool!

I've noticed that interceptor puts a io.BytestIO to wsgi.errors.
That causes some trouble:

  • it is never read -- so logs from my flask app crashes are lost
  • flask attempts to write a unicode there and fails with
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/logging/__init__.py", line 882, in emit
    stream.write(fs % msg)
TypeError: 'unicode' does not have the buffer interface
Logged from file app.py, line 1560

IMO putting a sys.stderr there would actually improve the situation.

If it is a viable option I can go ahead and produce a pull request.

@cdent
Copy link
Owner

cdent commented Jan 11, 2018

Thanks for the report. I'll need to investigate the history of why wsgi.errors is set to that. sys.stderr may very well be the right thing, but I'll need to do some remembering first. If it proves to be the right thing a pull request would be great.

If you have a minimal test case that demonstrates the problem that you can share that would be great.

@cdent
Copy link
Owner

cdent commented Jan 11, 2018

The use of BytesIO pre-dates my maintenance of wsgi-intercept and its migration to become simpler and support Python 3. So I can't find any history as to why it is that, other than "it's a good way to swallow stuff".

Setting it to sys.stderr looks like the right thing to do, especially given pep 3333's implication that that it is the norm.

So if you have the time to make a pull request that would be great and I'll get it merged and released asap.

@cdent cdent added the bug label Jan 11, 2018
@pupssman
Copy link
Contributor Author

Here is the MVE:

import flask
import requests

from wsgi_intercept.interceptor import RequestsInterceptor


app = flask.Flask(__name__)


@app.route('/')
def foo():
    # cause a KeyError
    return flask.jsonify({}['foo'])


def test_foo():
    with RequestsInterceptor(lambda: app, host='foo.com', port=80) as url:
        r = requests.get(url)

        assert r.status_code == 200

Running it causes the error:

(py27) pupssman@pupssman-ub:~/git/wsgi_interseptor_51_mve $ py.test test.py 
===================================================================================================================================================== test session starts =====================================================================================================================================================
platform linux2 -- Python 2.7.12, pytest-2.9.0, py-1.4.34, pluggy-0.3.1
rootdir: /home/pupssman/git/wsgi_interseptor_51_mve, inifile: 
collected 1 items 

test.py F

========================================================================================================================================================== FAILURES ===========================================================================================================================================================
__________________________________________________________________________________________________________________________________________________________ test_foo ___________________________________________________________________________________________________________________________________________________________

    def test_foo():
        with RequestsInterceptor(lambda: app, host='foo.com', port=80) as url:
            r = requests.get(url)
    
>           assert r.status_code == 200
E           assert 500 == 200
E            +  where 500 = <Response [500]>.status_code

test.py:20: AssertionError
---------------------------------------------------------------------------------------------------------------------------------------------------- Captured stderr call -----------------------------------------------------------------------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 882, in emit
    stream.write(fs % msg)
TypeError: 'unicode' does not have the buffer interface
Logged from file app.py, line 1560
================================================================================================================================================== 1 failed in 0.12 seconds ===================================================================================================================================================
(py27) pupssman@pupssman-ub:~/git/wsgi_interseptor_51_mve $ 

@pupssman
Copy link
Contributor Author

Will go on with PR soon!

pupssman added a commit to pupssman/wsgi-intercept that referenced this issue Jan 11, 2018
@cdent cdent closed this as completed in #52 Jan 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants