문제

I am customizing exceptions in my python code. I have inherited exception class in to other and now defining some custom errors as classes derived from my custom exception class like this:

class DataCollectorError(Exception): pass
class ParamNullError(DataCollectorError) : pass
class ParamInvalidTypeError(DataCollectorError) : pass

I am raising these exceptions in my python function like:

def READ_METER_DATA (regIndex, numRegisters, slaveUnit):
    try:
        if not regIndex:
            raise ParamNullError, "register index is null"

        if not numRegisters:
            raise ParamNullError, "number of registers should not be null"

        if not slaveUnit:
            raise ParamNullError, "Meter Id should not be null"

and logging error like :

except DataCollectorError as d:
    lgr.error('DataCollector Error(READ_METER_DATA): '+d.args[0])
    print 'DataCollector Error:(READ_METER_DATA)', d.args[0]
except:
    lgr.error('Unexpected Error: ', sys.exc_info())
    print 'Unexpected Error: ', sys.exc_info()
    pass

but this defeats purpose of unit testing script as it doesn't whether exception raised bcz it is being catched by my catch block before my unit test script knows it. so i wanted to log these errors in base class itself -

Class ParamNullError(DataCollectorError):
    <----here----------->
    pass 

can anybody tell me how to fetch that string passed while raising exception?

도움이 되었습니까?

해결책

Simply extend your error-class with an __init__ and an __str__ method.

Example:

class DataCollectorError(Exception):
    def __init__(self, msg=''):
        self.msg = msg
        log(msg)  # use your logging things here

    def __str__(self):
        return self.msg

Use msg='' because then you don't need to always specify a message.

다른 팁

Don't.

Factor out the calls you need to unit test, and move your exception handler out:

try:
     testableFunctionCall()
except:
     lgr.exception('Unexpected Error')

and test testableFunctionCall().

Alternatively, use the testfixtures library to test the logging itself:

from testfixtures import LogCapture
with LogCapture() as l:
    callFunctionUnderTest()

l.check(
     ('packagename', 'ERROR', 'DataCollector Error(READ_METER_DATA): foobar'),
)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top