How much sanity checking is required for function inputs?

Ethan Furman ethan at stoneleaf.us
Wed Apr 20 02:09:39 EDT 2016


On 04/19/2016 10:51 PM, Stephen Hansen wrote:

> But that's a fuzzy question, there's no solid and clear answer. Did you
> see Ethan's response? I largely agree with his trinity:
>
> On Sun, Apr 17, 2016, at 10:26 PM, Ethan Furman wrote:
>> I sanity check for three reasons:
>>
>> 1) raise a nicer error message
>>
>> 2) keep the point of failure close to the error
>>
>> 3) the consequences of bad data are Bad Bad Things (tm)
>
> With a 4)th that exceptions aren't for users, only programmers. But
> ultimately, I'd rather a user see an exception if something weird goes
> wrong because they can send it to me and I can diagnose it and push an
> update. So I also:
>
> 4) Attempt to make sure all user errors result in user error messages,
> not exceptions.
>
> Note, 1) doesn't mean I always raise a nicer message, it means if
> "KeyError" is ambiguious or odd, I raise a better and more informative
> one. But you're getting nothing swapping out KeyError for
> Exception(lotsofwords).
>
> I use 1) more to be less 'nicer' and more, er, 'more specific'. Since I
> don't like exceptions to rise to the user level where niceness is
> needed.

Yeah, that's a better phrasing for (1); I meant more appropriate or 
informative, such as swapping an internal error (such as KeyError) for a 
more meaningful FieldNotFound error (or whatever) -- largely library 
code concerns.

--
~Ethan~




More information about the Python-list mailing list