Skip to content

Commit e16074f

Browse files
author
James William Pye
committed
Relocate the error __str__ implementation into postgresql.sys.
While users could just monkey patch it, it would likely be a bit more preferable to have an explicitly designated place to override the method's implementation.
1 parent 0397f4d commit e16074f

2 files changed

Lines changed: 56 additions & 24 deletions

File tree

postgresql/exceptions.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
from functools import partial
3535
from operator import attrgetter
3636
from . import api as pg_api
37-
from .python.element import format_element
38-
from .python.string import indent
37+
from . import sys as pg_sys
3938

4039
PythonException = Exception
4140
class Exception(Exception):
@@ -92,25 +91,8 @@ class Error(pg_api.Message, Exception):
9291
code = ''
9392

9493
def __str__(self):
95-
it = self._e_metas()
96-
if self.creator is not None:
97-
# Protect against element traceback failures.
98-
try:
99-
after = os.linesep + format_element(self.creator)
100-
except PythonException:
101-
after = 'Element Traceback of %r caused exception:%s' %(
102-
type(self.creator).__name__,
103-
os.linesep
104-
)
105-
after += indent(traceback.format_exc())
106-
after = os.linesep + indent(after).rstrip()
107-
else:
108-
after = ''
109-
return next(it)[1] \
110-
+ os.linesep + ' ' \
111-
+ (os.linesep + ' ').join(
112-
k + ': ' + v for k, v in it
113-
) + after
94+
'Call .sys.errformat(self)'
95+
return pg_sys.errformat(self)
11496

11597
@property
11698
def fatal(self):

postgresql/sys.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,49 @@
1010
1111
``libpath``
1212
The local file system paths that contain query libraries.
13+
14+
Overridable Functions
15+
---------------------
16+
17+
excformat
18+
Information that makes up an exception's displayed "body".
19+
Effectively, the implementation of `postgresql.exception.Error.__str__`
20+
21+
msghook
22+
Display a message.
1323
"""
1424
import sys
1525
import os
26+
import traceback
1627
from .python.element import format_element
28+
from .python.string import indent
1729

1830
libpath = []
1931

32+
def default_errformat(val):
33+
"""
34+
Built-in error formatter. DON'T TOUCH!
35+
"""
36+
it = val._e_metas()
37+
if val.creator is not None:
38+
# Protect against element traceback failures.
39+
try:
40+
after = os.linesep + format_element(val.creator)
41+
except Exception:
42+
after = 'Element Traceback of %r caused exception:%s' %(
43+
type(val.creator).__name__,
44+
os.linesep
45+
)
46+
after += indent(traceback.format_exc())
47+
after = os.linesep + indent(after).rstrip()
48+
else:
49+
after = ''
50+
return next(it)[1] \
51+
+ os.linesep + ' ' \
52+
+ (os.linesep + ' ').join(
53+
k + ': ' + v for k, v in it
54+
) + after
55+
2056
def default_msghook(msg, format_message = format_element):
2157
"""
2258
Built-in message hook. DON'T TOUCH!
@@ -31,14 +67,28 @@ def default_msghook(msg, format_message = format_element):
3167
# gasp.
3268
pass
3369

34-
def msghook(msg):
70+
def errformat(*args, **kw):
71+
"""
72+
Raised Database Error formatter pointing to default_excformat.
73+
74+
Override if you like. All postgresql.exceptions.Error's are formatted using
75+
this function.
76+
"""
77+
return default_errformat(*args, **kw)
78+
79+
def msghook(*args, **kw):
3580
"""
3681
Message hook pointing to default_msghook.
3782
38-
Overload if you like. All untrapped messages raised by
83+
Override if you like. All untrapped messages raised by
3984
driver connections come here to be printed to stderr.
4085
"""
41-
return default_msghook(msg)
86+
return default_msghook(*args, **kw)
87+
88+
def reset_errformat(with_func = errformat):
89+
'restore the original excformat function'
90+
global errformat
91+
errformat = with_func
4292

4393
def reset_msghook(with_func = msghook):
4494
'restore the original msghook function'

0 commit comments

Comments
 (0)