My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
''' Helpers that trap and log errors.
Copyright (c) 2006-2007, PreFab Software Inc. '''


from __future__ import with_statement
from contextlib import contextmanager
from functools import wraps

import logging

@contextmanager
def error_trapping(ident=None):
''' A context manager that traps and logs exception in its block.
Usage:
with error_trapping():
might_raise_exception()
this_will_always_be_called()
'''
try:
yield None
except Exception:
if ident:
logging.error('Error in %s' % ident, exc_info=True)
else:
logging.error('Error', exc_info=True)


def trap_errors(f):
''' A decorator to trap and log exceptions '''
@wraps(f)
def wrapper(*args, **kwds):
with error_trapping(f.__name__):
return f(*args, **kwds)
return wrapper

Show details Hide details

Change log

r89 by ken...@tds.net on Apr 15, 2008   Diff
Add and reformat comments to work better
with epydoc
Go to: 
Project members, sign in to write a code review

Older revisions

r7 by ken...@tds.net on Dec 11, 2007   Diff
Sort out initial import
r3 by ken...@tds.net on Dec 11, 2007   Diff
Initial import of code from PreFab
Software
All revisions of this file

File info

Size: 892 bytes, 35 lines

File properties

svn:eol-style
native
Hosted by Google Code