0

I have an exception that looks like this:

exc = ProtocolError('Connection Aborted', BadStatusLine('No status line received'))

how can I access the No status line received part?

Here's the example situation:

def some_function():
    raise ProtocolError('Connection Aborted', BadStatusLine('No status line received'))

def some_other_function():
    try:
        some_function()
    except Exception as exc:
        if exc.message:
            details = exc.message
        else:
            details = exc

In the code above I'm trying to check if the returned exception has a message and if so I'm supposed to write it into the database, however when I call the exc.message it returns an empty string, and when I call exc it returns:

bson.errors.InvalidDocument: cannot encode object: ProtocolError('Connection Aborted', BadStatusLine('No status linereceived',)), of type: <class 'urllib3.exceptions.ProtocolError'>

so I cant write it into the database since its a type Exception not string, what I need to do is to see if the returned Exception has another nested Exception in it and get it's message.

6
  • is this exception is in string format? Commented Jun 1, 2021 at 11:27
  • @ZainUlAbidin type: <class 'urllib3.exceptions.ProtocolError'> Commented Jun 1, 2021 at 11:30
  • can you provide the code to reproduce the problem . so that it can be quicly answered and solved Commented Jun 1, 2021 at 11:33
  • done, check it out please Commented Jun 1, 2021 at 11:41
  • ProtocolError is not defined . .from where you are using or importing this function? urllib3? Commented Jun 1, 2021 at 11:53

1 Answer 1

1

I wasn't able to find the exact optimal way to fetch the inner message or exception but for quick assistance i wrote a utility function which by using regular expressions will return you inner exceptions or messages complete code is following

from urllib3.exceptions import ProtocolError
from http.client import BadStatusLine
import re

def FetchInnerExceptions(exc):
    result = []
    messages = str(exc).split(',')
    for msg in messages:
        m = re.search('''(?<=')\s*[^']+?\s*(?=')''', msg)
        if m is not None or m != '':
            result.append(m.group().strip())
    return result

def some_function():
    raise ProtocolError('Connection Aborted', BadStatusLine('No status line received'))

def some_other_function():
    try:
        some_function()
    except Exception as exc:
        e = FetchInnerExceptions(exc)
        print(e) #dumps all array or use index e[1] for your required message

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

2 Comments

Thank you mate, I think I made it work, i just used details = str(exc) in the last line and grabbed the whole record for the database, but I will check out your solution and maybe use it if that doesn't work :)
ok just in case if it worked then don't forget to mark this as accepted to help others

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.