python - "TypeError: coercing to Unicode: need string or buffer" while raising an exception -


i'm facing weird exception on production system:

typeerror: coercing unicode: need string or buffer, invalidhttpstatuscode found

but can't reproduce locally. when log production system , try manually reproduce error works ok.

code:

import requests requests.exceptions import httperror _session = requests.session()  class httpresponseexception(httperror):   def __init__(self, response):     self.response = response    def __str__(self):     return 'http request failed status code {}.\n' \            'url {}\ncontent {}\nheaders {}'.format(                self.response.status_code,                self.response.url,                self.response.content,                self.response.headers)   class invalidhttpstatuscode(httpresponseexception):   pass   def get_request(url)   response = _session.request('get', url)   if response.status_code != 200     ex = invalidhttpstatuscode(response)     raise ex  get_request('https://my.server.com/rest/something') 

stacktrace (tried simplify it):

file '/my_request.py', line x, in get_request",     raise ex",  typeerror: coercing unicode: need string or buffer, invalidhttpstatuscode found",  

server response:

the error response web service http 401 , body html code containing unicode char "×".

my question:

i have no clue why raise ex can cause coercing unicode exception. have no unicode value in line. somehow think there serialization ongoing in depths of python cause exception (due unicode char within response.content) that's guess.

do know real reason , how can fix issue?

enviroment:

  • ubuntu 14.04.3
  • python 2.7.9 (self compiled separate directory)
  • requests 2.5.3

ok, you're raising invalidhttpstatuscode. inherits httpresponseexception. has __ str__ method formats non-unicode string returned data.

in short,

    return u'http request failed status code {}.\n' \            'url {}\ncontent {}\nheaders {}'.format(     .... 

(add u start of string.) reason having trouble reproducing because logging calls function, while aren't. bet can similar exception if explicitly call this.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -