I have a situation where a user may provide bad data to get to a datapoint that may not exist. Currently, the error types raised when these problems happen is not clear or straightforward.
In my API, I intend to create a error to catch in the UI (or I'll let the UI framework handle it by reporting it straight to the user, which I've tested and know works.):
class DataError(ValueError):
'''
raise this error with a message of what was wrong,
will also be caught when catching general ValueErrors
or other Exceptions that ValueError subclasses
'''
And usage:
if user_input not in accepted_values:
raise DataError('bad user_input: {0}'.format(user_input))
I do get rather opinionated about my approach, and I want rather unassailable code. I'm aware of the upsides, as I can catch this specific exception without hiding others. But I can't think of any downsides.
What are the downsides of doing this as opposed to just raising my error message with ValueError?
DataErrordifferently from aValueError.requests.exceptions.DataError. If it's good enough for Mike, it's good enough for me.