JSONDecodeError and TypeError: can only concatenate str (not "bytes") to str error encountered when _do_web_request responses with status < 200 or status >= 300

Bug #2074367 reported by Navaneet Kumar
10
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pyghmi
Fix Committed
Undecided
Jarrod Johnson

Bug Description

Pyghmi failed to provide actual exception when response status matches res[1] < 200 or res[1] >= 300 which leads consumer to unaware of actual problem

Details as in comments
def _do_web_request(self, url, payload=None, method=None, cache=True, etag=None):
        # Removed few lines
        try:
            res = wc.grab_json_response_with_status(url, payload, method=method) # return res[0] as a byte string and status 400
        finally:
            if 'If-Match' in wc.stdheaders: del wc.stdheaders['If-Match']
        if res[1] < 200 or res[1] >= 300: # response status matched this condition
            try:
                info = json.loads(res[0]) # Failed and throw exception json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
                # removed few lines
            except (ValueError, KeyError): # Catches the exception
                raise exc.PyghmiException(str(url) + ":" + res[0]) # Failed to concatenate the string and throw exception TypeError: can only concatenate str (not "bytes") to str

When Added, raise exc.RedfishError("Res: %s" %res) just next in condition if res[1] < 200 or res[1] >= 300:
I see below exception message as
Res: (b\'<?xml version="1.0" encoding="iso-8859-1"?>\\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\\n "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\\n <head>\\n <title>400 Bad Request</title>\\n </head>\\n <body>\\n <h1>400 Bad Request</h1>\\n </body>\\n</html>\\n\', 400)')

Using python 3.9.18 and with invalid redfish endpoint like

Revision history for this message
Navaneet Kumar (navaneetkumar) wrote :

To fix this probably we should check type before returning if condition not matched below

if rsp.status >= 200 and rsp.status < 300:
            if body and not isinstance(body, type(u'')):
                try:
                    body = body.decode('utf8')
                except Exception:
                    body = body.decode('iso-8859-1')
            return json.loads(body) if body else {}, rsp.status

return body, rsp.status
in grab_json_response_with_status of pyghmi/util/webclient.py L:273

Revision history for this message
Jarrod Johnson (jbjohnso) wrote :

https://opendev.org/x/pyghmi/commit/20a8dc83e954cb7d69ad2345b7ac5a8fb269ea64

Is my best thought on addressing it. Since the return is not vaguely compliant to redfish standards, best I can do is pass through the opaque error and avoid the str/bytes concatenation.

Changed in pyghmi:
assignee: nobody → Jarrod Johnson (jbjohnso)
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.