1

I'm catching 3 diffferent exceptions and the error is manipulated differently in all of them as below.

 except IntegrityError as e: error_objects.append( { "Row Number": index + 1, "Error": parse_integrity_error(e), "data": user_given_data, } ) except ValidationError as e: error_objects.append( { "Row Number": index + 1, "Error": e.detail[0], "data": user_given_data, } ) except Exception as e: error_objects.append( { "Row Number": index + 1, "Error": str(e), "data": user_given_data, } ) 

Is there a clean/neat/pythonic way to do it?

3
  • Try changing the last element after inserting it with only row number and data Commented Aug 27, 2021 at 7:04
  • Honestly the way you're doing it is probably the most natural way. You could combine all the cases into the except Exception case with a later typecheck to figure out how to produce the Error value, but I think it's a lot clearer the way you have it now. Commented Aug 27, 2021 at 7:26
  • 1
    @Blckknght There are a lot of duplications in the OP's code so I wouldn't say it is the most natural way. Commented Aug 27, 2021 at 8:19

2 Answers 2

2

I would register exceptions in a function(or even a dictionary outside the function) like this(This is an example) :

def exception_return_value(e): type_e = type(e) if type_e == IndexError: error = 'IndexError Value' elif type_e == ValueError: error = 'ValueError Value' else: error = 'Exception Error' return {"Row Number": 10, "Error": error, "data": 20} try: raise ValueError() except (IndexError, ValueError, Exception) as e: return_value = exception_return_value(e) print(return_value) 

Because I didn't have your code, I couldn't run it, but you can rewrite it like :

def exception_return_value(e): type_e = type(e) if type_e == IntegrityError: error = parse_integrity_error(e) elif type_e == ValidationError: error = e.detail[0] else: error = str(e) return {"Row Number": index + 1, "Error": error, "data": user_given_data} try: ... except (IntegrityError, ValidationError, Exception) as e: return_value = exception_return_value(e) error_objects.append(return_value) 
Sign up to request clarification or add additional context in comments.

Comments

1

Since all of your exception handlers do the same thing and differ only in how they convert an exception object to a string, you can add a __str__ method to each of your custom exception classes (IntegrityError and ValidationError in your example) so that their instances can be formatted consistently with a call to the str function, just like how you convert a regular Exception object to string:

class IntegrityError(Exception): def __str__(self): return parse_integrity_error(self) class ValidationError(Exception): def __str__(self): return self.detail[0] try: ... except Exception as e: error_objects.append( { "Row Number": index + 1, "Error": str(e), "data": user_given_data, } ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.