1

I'm using older versions for one of my servers (because some of dependencies). I setup my server in docker container and its working fine until today. When i rebuild my image and re-run the service then its getting this error:

Exception on /messages [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1379, in handle_user_exception
    return handler(e)
  File "/opt/sync-engine/inbox/api/ns_api.py", line 226, in handle_input_error
    request.environ['log_context']['error'] = error.__class__.__name__
KeyError: 'log_context'
{"module": "inbox.api.srv:28", "level": "error", "event": "Uncaught error thrown by Flask/Werkzeug", "timestamp": "2019-03-27T10:36:55.760581Z", "greenlet_id": 140055343249040}

srv.py (inbox.api.srv):

...
def default_json_error(ex):
    """ Exception -> flask JSON responder """
    logger = get_logger()
    logger.error('Uncaught error thrown by Flask/Werkzeug', exc_info=ex)
    response = jsonify(message=str(ex), type='api_error')
    response.status_code = (ex.code
                            if isinstance(ex, HTTPException)
                            else 500)
    return response
...

ns_api.py:

...
@app.errorhandler(APIException)
def handle_input_error(error):
    # these "errors" are normal, so we don't need to save a traceback
    request.environ['log_context']['error'] = error.__class__.__name__
    request.environ['log_context']['error_message'] = error.message
    response = flask_jsonify(message=error.message,
                             type='invalid_request_error')
    response.status_code = error.status_code
    return response
...

1 Answer 1

2

The main reason:

there is no log_context key in the request.environ

To fix this just initialize the log_context key

request.environ.setdefault('log_context', dict())
# etc...
request.environ['log_context']['error'] = error.__class__.__name__

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.