'Return the right exception class for the error code'

@classmethod
def match_error_code(cls, error_code):

    exception_classes = cls.get_sorted_exceptions()
    error_class = None
    for class_ in exception_classes:
        codes_list = class_.codes_list()
        matching_error_class = None
        for code in codes_list:
            if isinstance(code, tuple):
                (start, stop) = code
                if (error_code and (start <= error_code <= stop)):
                    matching_error_class = class_
                    logger.info('Matched    error    on    code    %s', code)
            elif isinstance(code, (int, six.integer_types)):
                if (int(code) == error_code):
                    matching_error_class = class_
                    logger.info('Matched    error    on    code    %s', code)
            else:
                raise (ValueError, ('Dont    know    how    to    handle    %s    of    type    %s' % (code, type(code))))
        if matching_error_class:
            error_class = matching_error_class
            break
    return error_class
