I've created a wrapper to the decorator that catch, rollback transaction and reraise the exception:-
http://tech.marimore.co.jp/2012/08/django-transactioncommit_manually-wrapper.html
This has happened number of times for me. There's a 3 year old ticket on this actually but since I want to be quick and no time yet to look into the ticket, the solution is to wrap all my function with a decorator that can catch the exception and exit immediately.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transact(func): | |
""" | |
Usage: | |
@transaction.commit_manually | |
@transact | |
def some_function(a, b): | |
do_something() | |
""" | |
def decorated(*args, **kwargs): | |
try: | |
func(*args, **kwargs) | |
except Exception, e: | |
print e | |
transaction.rollback() | |
sys.exit() | |
return decorated |
I'm doing this in a batch script so what I want is either the script succeed or fail immediately, showing the error. YMMV.
No comments:
Post a Comment