Git Product home page Git Product logo

weberror's Introduction

NOTICE

This software is not actively maintained. Simple bugfixes and other patches will be accepted, and released.

Introduction

WebError provides WSGI middleware for the debugging and handling of errors inside of WSGI applications.

Usage

There are two primary WSGI middleware components:

weberror.errormiddleware.make_error_middleware

This middleware should be used for production deployed applications and is used to track extra information regarding errors that occur. These error entries can additionally be emailed to a given email address using the error_email option. Example usage:

from weberror.errormiddleware import make_error_middleware
app = make_error_middleware(app, global_conf)

weberror.evalexception.make_eval_exception

This middleware is used to help debug errors in wsgi applications during development and should not be used in production. Example usage:

from weberror.evalexception import make_eval_exception
app = make_eval_exception(app, global_conf)

weberror's People

Contributors

bbangert avatar brondsem avatar digitalresistor avatar f0rk avatar ianb avatar jayvdb avatar jnpkrn avatar jurov avatar jxtx avatar mcdonc avatar mmerickel avatar pjenvey avatar rockyburt avatar sbrunner avatar skrul avatar stephan-hof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

weberror's Issues

Example for make_error_middleware shows make_eval_exception

The readme.md and the writeup on pypi describe make_error_middleware and make_eval_exception, but the examples given are identical and show the use of make_eval_exception, with no mention of make_error_middleware. I posted this on the pylons G+ forum, but it was never released, so I assume this is where this needs to go.

New release 0.11?

Hi,
I see many commits since the last release. It would be cool to release a new version. Thanks.

WebError#evalerror can't handle malformed URLs

If you have the evalerror defined in you WSGI pipeline and than access a URL with wrongly encoded characters there will be an uncatched UnicodeDecodeError (thrown by WebOb).

Suppose you have this pipleline config:

pipeline =
    egg:WebError#evalerror
    YOURAPP

and then access an invalid URL like http://localhost/%F6 you will get a error trace similar to this:

Traceback (most recent call last):
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py", line 1068, in process_request_in_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 638, in __init__
    self.handle()
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py", line 442, in handle
    BaseHTTPRequestHandler.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py", line 437, in handle_one_request
    self.wsgi_execute()
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py", line 287, in wsgi_execute
    self.wsgi_start_response)
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/WebError-0.10.3-py2.7.egg/weberror/evalexception.py", line 232, in __call__
    if req.path_info_peek() == '_debug':
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/WebOb-1.3.1-py2.7.egg/webob/request.py", line 560, in path_info_peek
    path = self.path_info
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/WebOb-1.3.1-py2.7.egg/webob/descriptors.py", line 68, in fget
    return req.encget(key, encattr=encattr)
  File "/home/vagrant/demo/local/lib/python2.7/site-packages/WebOb-1.3.1-py2.7.egg/webob/request.py", line 178, in encget
    return val.decode(encoding)
  File "/home/vagrant/demo/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 1: invalid start byte

While I agree that there is nothing you can do about malformed URLs the code inside evalexception.py should still try to catch the error and at least return an standard 400 Bad Request response. This way, at least the wsgi thread won't die.

Production deployment fails because of docstring manipulation.

In an environment with PYTHONOPTIMIZE=2 set in the environment, or where Python is otherwise invoked with the -OO (optimize & strip docstrings) option as distinct from -O (just optimize) WebError explodes gloriously upon reaching the end of errormiddleware.py:

doc_lines = (ErrorMiddleware.__doc__ or '').splitlines(True)
for i in range(len(doc_lines)):
    if doc_lines[i].strip().startswith('Settings'):
        make_error_middleware.__doc__ = ''.join(doc_lines[i:])
        break
del i, doc_lines

The reason is due to i never having been initialized if the string is empty, which it is with this level of optimization enabled. Instead, I would recommend:

if ErrorMiddleware.__doc__:
    doc_lines = ErrorMiddleware.__doc__.splitlines(True)
    for i in range(len(doc_lines)):
        if doc_lines[i].strip().startswith('Settings'):
            make_error_middleware.__doc__ = ''.join(doc_lines[i:])
            break
    del i, doc_lines

A quick glance indicates this is a current problem (master branch) and has been a problem since at least 0.10.3.

view source error in debug view, got TypeError: You cannot set Response.body to a unicode object (use Response.unicode_body)

Module formencode.validators:1941 in __init__ view

when click 'view, got 500 Internal Server Error, and got:

Exception happened during processing of request from ('127.0.0.1', 60894)
Traceback (most recent call last):
  File "e:\works\pylons\devenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\h
ttpserver.py", line 1068, in process_request_in_thread
    self.finish_request(request, client_address)
  File "D:\Py27\Lib\SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "D:\Py27\Lib\SocketServer.py", line 639, in __init__
    self.handle()
  File "e:\works\pylons\devenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\h
ttpserver.py", line 442, in handle
    BaseHTTPRequestHandler.handle(self)
  File "D:\Py27\Lib\BaseHTTPServer.py", line 337, in handle
    self.handle_one_request()
  File "e:\works\pylons\devenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\h
ttpserver.py", line 437, in handle_one_request
    self.wsgi_execute()
  File "e:\works\pylons\devenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\h
ttpserver.py", line 287, in wsgi_execute
    self.wsgi_start_response)
  File "e:\works\pylons\devenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\c
ascade.py", line 130, in __call__
    return self.apps[-1](environ, start_response)
  File "e:\works\pylons\devenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\r
egistry.py", line 379, in __call__
    app_iter = self.application(environ, start_response)
  File "e:\works\pylons\devenv\lib\site-packages\pylons-1.0-py2.7.egg\pylons\mid
dleware.py", line 150, in __call__
    self.app, environ, catch_exc_info=True)
  File "e:\works\pylons\devenv\lib\site-packages\pylons-1.0-py2.7.egg\pylons\uti
l.py", line 48, in call_wsgi_application
    app_iter = application(environ, start_response)
  File "e:\works\pylons\devenv\lib\site-packages\weberror-0.10.3-py2.7.egg\weber
ror\evalexception.py", line 233, in __call__
    return self.debug(req)(environ, start_response)
  File "e:\works\pylons\devenv\lib\site-packages\weberror-0.10.3-py2.7.egg\weber
ror\evalexception.py", line 245, in debug
    return method(req)
  File "e:\works\pylons\devenv\lib\site-packages\weberror-0.10.3-py2.7.egg\weber
ror\evalexception.py", line 391, in source_code
    res.body = 'The module %s does not have an entry in sys.modules
' % module_name
  File "build\bdist.win32\egg\webob\response.py", line 298, in _body__set
    "You cannot set Response.body to a unicode object (use Response.unicode_body
)")
TypeError: You cannot set Response.body to a unicode object (use Response.unicod
e_body)

and in weberror\evalexception.py , I found this:

    def source_code(self, req):
        location = req.params['location']  # when use print, I find location is a unicode obj
        module_name, lineno = location.split(':', 1)
        module = sys.modules.get(module_name)
        if module is None:
            # Something weird indeed
            res = Response(content_type='text/html', charset='utf8')
            res.body = 'The module %s does not have an entry in sys.modules' % module_name
            return res
 

in python we know "foo%sfoo" % u"bar" gives a unicode obj,
so maybe we need a location = srt(req.params['location']) ?

[was issue#8@Bitbucket] md5-sha fallback in FIPS mode

Hello Ben,

some time ago, when the primary home for WebError used to be
Bitbucket, there was an issue 8 opened [1][](no longer works)
regarding run-time adaptation to FIPS-enabled system, i.e.,
to situation Python's md5 (as implemented by OpenSSL) is not
allowed by default.

Admittedly, such limiting mode is not a common concern, but
when enabled (e.g., for Fedora, see [2]), breakage is
imminent... This is also the case of WebError vs. md5.
While that issue 8 [1] hadn't been preserved publicly,
the original item in Red Hat Bugzilla was [3][](and I am now
to tackle it as a blocker preventing another component from
working properly in FIPS mode; and no, hadn't known about
that bug before, which is frankly unfortunate).

Both reports come from @jaredjennings (noticed his account
here) and he even stepped up with his own patchset [4].
I have no idea what was the outcome back then around the
issue 8 [1], if any discussion happened at all. But would
like to see this aged issue resolved and am willing to lend
my hands into it too, if needed.

As a starter, does the patchset [4] look as an acceptable
solution? Looked good to me, incl. in-comment explanation
of some magic numbers behind. Btw. are there any known
non-transient consumers of the respective output hashes?
Wasn't able to find any consumer at all, but there can be
some in private codebases, indeed.

Then, @jaredjennings, would you endorse sticking with your
changeset, Jared?

I can prepare pull request etc. if needed, but wanted to
solicit feedback first. Preferrably I'd like to keep
upstream bits and backported ones in RHEL in sync.

[1] https://bitbucket.org/bbangert/weberror/issue/8/
[2] https://bugzilla.redhat.com/show_bug.cgi?id=805538#c0
[3] https://bugzilla.redhat.com/show_bug.cgi?id=746118
[4] https://bitbucket.org/jaredj/fips-compatibility/commits/all

Jan

UnicodeDecodeError in HTMLFormatter.quote with extended ASCII characters

I am unable to see tracebacks for a character encoding error in a Flask app I am debugging. Flask uses weberror to show tracebacks and there appears to be a character encoding issue somewhere along the way.

Either the character encoding issue is in weberror as illustrated by the test case below, or weberror has an implied contract on the encoding it expects and this is not being respected by Flask.

$ pip show weberror

---
Metadata-Version: 2.0
Name: WebError
Version: 0.13.1
from weberror import formatter

hf = formatter.HTMLFormatter()

#s = "<Request 'http://example.com?abc=def\xc5' [GET]>"
s = "\xc5"
hf.quote(s)
Traceback (most recent call last):
  File "weberror_test_case.py", line 7, in <module>
    hf.quote(s)
  File "/home/vagrant/envs/web/local/lib/python2.7/site-packages/weberror/formatter.py", line 296, in quote
    s = s.encode('latin1', 'htmlentityreplace')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)

Quick hack fix weberror/formatter.py:

class HTMLFormatter(TextFormatter):

    def quote(self, s):
        if isinstance(s, str) and hasattr(self, 'frame'):
            s = s.decode(self.frame.source_encoding, 'replace')
        #s = s.encode('latin1', 'htmlentityreplace')
        s = s.decode('latin1').encode('latin1', 'htmlentityreplace')
        return html_quote(s)

Adding the .decode('latin1') works for my purposes in that I'm now able to see tracebacks in Flask.

It's not clear to me what the expected encoding is at this point so I don't know whether this would be a correct fix for the general case, or even whether this error is expected behavior (perhaps Flask should be decoding before passing to weberror).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.