On Thu, Dec 13, 2012 at 11:03 AM, Micky Hulse <rgmi...@gmail.com> wrote:

> Hello Russ! Thank you so much for your pro help and informative reply.
> I really appreciate it. :)
>
> No worries at all. Glad I could be of assistance.


> I've been wondering if I could just add a simple try/catch, like this code
> here:
>
> <
> https://github.com/registerguard/django-ad-manager/blob/57f77dd0b5b59351ad50629f72c9fbb2083b1dd7/ad_manager/decorators.py#L37-L44
> >
>
> <snip>
>
> try:
>     data = simplejson.dumps(objects)
> except:
>     data = simplejson.dumps(str(objects))
>
> </snip>
>
> Note where str() is being used... Though, my co-worker has told me
> that using "except" without an argument is bad style and frowned upon.


Can you use a try-catch to flag encoding problems? Sure - that's a
completely reasonable solution.

Is a blanket try-catch a bad idea? Yes, because it hides all sins. The
exceptions you're expecting are quite predictable -- if dumps() can't
encode the object, you're going to get a TypeError. Therefore it makes
sense to catch the TypeError, and provide a response that indicates that
encoding was the problem. If you catch *all* exceptions, and just assume
that encoding was the problem, then the "once in a million that happens 9
times out of 10" will be some other error generated by the serialisation of
the object that *isn't* a type encoding error (an attribute error in your
serializer, for example). If you're masking those errors, they're much
harder to find.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to