In Python there's a good, if lesser-used pattern that you can follow. Say you've just developed this neat Frobnosticator library. You might do something like this:
exc.py
class FrobnosticatorError(Exception): pass
class FrobnosticateConfigNotFoundError(FrobnosticatorError, FileNotFoundError): pass
... # whatever other kinds of errors you want.
What this allows you to do is wrap your entire application in something like this:
try:
do_all_the_things()
except FrobnosticatorError:
print('Oh noes! We forgot to catch an error somewhere!')
except:
print("Ugh. Something broke, but at least it wasn't something we threw!")
Right now I'm working on a node+coffeescript app that's both server and client side code. Unfortunately we're a bit legacy, running Node v6.4.0
and Coffescript v1, if that makes a difference.
I would love to use this kind of pattern within our code - it would make a lot of things a lot cleaner.
I've searched for several different things like javascript custom exception handling
, but I haven't found any good examples of people doing this.
I was picturing making something like a lib/client/Exceptions.coffee
that housed all of these exceptions that we would then use throughout our codebase.
Does this seem like a reasonable plan, or is this a terrible thing to do in Javascript? Is there a better way to design our app so that we can specifically tell which errors came from our code, and which errors came from other libraries that we're using?
Aucun commentaire:
Enregistrer un commentaire