Skip to content

Commit 21ff168

Browse files
committed
improved exception handling
1 parent 2b5d7ea commit 21ff168

File tree

5 files changed

+34
-54
lines changed

5 files changed

+34
-54
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from distutils.core import setup
77

88
# Basic dependencies
9-
install_requires = ["pg8000", "port-for"]
9+
install_requires = ["pg8000", "port-for", "six"]
1010

1111
# Add compatibility enum class
1212
if sys.version_info < (3, 4):

testgres/backup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import shutil
55
import tempfile
66

7+
from six import raise_from
8+
79
from .consts import \
810
DATA_DIR as _DATA_DIR, \
911
BACKUP_LOG_FILE as _BACKUP_LOG_FILE, \
@@ -14,8 +16,7 @@
1416
from .utils import \
1517
get_bin_path, \
1618
default_username as _default_username, \
17-
execute_utility as _execute_utility, \
18-
explain_exception as _explain_exception
19+
execute_utility as _execute_utility
1920

2021

2122
class NodeBackup(object):
@@ -103,7 +104,7 @@ def _prepare_dir(self, destroy):
103104
# Copy backup to new data dir
104105
shutil.copytree(data1, data2)
105106
except Exception as e:
106-
raise BackupException(_explain_exception(e))
107+
raise_from(BackupException('Failed to copy files'), e)
107108
else:
108109
dest_base_dir = self.base_dir
109110

testgres/cache.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import shutil
66
import tempfile
77

8+
from six import raise_from
9+
810
from .config import TestgresConfig
911

1012
from .exceptions import \
@@ -13,8 +15,7 @@
1315

1416
from .utils import \
1517
get_bin_path, \
16-
execute_utility as _execute_utility, \
17-
explain_exception as _explain_exception
18+
execute_utility as _execute_utility
1819

1920

2021
def cached_initdb(data_dir, initdb_logfile, initdb_params=[]):
@@ -27,7 +28,7 @@ def call_initdb(initdb_dir):
2728
_params = [get_bin_path("initdb"), "-D", initdb_dir, "-N"]
2829
_execute_utility(_params + initdb_params, initdb_logfile)
2930
except ExecUtilException as e:
30-
raise InitNodeException(_explain_exception(e))
31+
raise_from(InitNodeException("Failed to run initdb"), e)
3132

3233
def rm_cached_data_dir(cached_data_dir):
3334
shutil.rmtree(cached_data_dir, ignore_errors=True)
@@ -57,4 +58,4 @@ def rm_cached_data_dir(cached_data_dir):
5758
# Copy cached initdb to current data dir
5859
shutil.copytree(cached_data_dir, data_dir)
5960
except Exception as e:
60-
raise InitNodeException(_explain_exception(e))
61+
raise_from(InitNodeException("Failed to copy files"), e)

testgres/node.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99

1010
from enum import Enum
11+
from six import raise_from
1112

1213
from .cache import cached_initdb as _cached_initdb
1314

@@ -40,8 +41,7 @@
4041
reserve_port as _reserve_port, \
4142
release_port as _release_port, \
4243
default_username as _default_username, \
43-
execute_utility as _execute_utility, \
44-
explain_exception as _explain_exception
44+
execute_utility as _execute_utility
4545

4646

4747
class NodeStatus(Enum):
@@ -110,7 +110,7 @@ def utils_log_name(self):
110110

111111
@property
112112
def pg_log_name(self):
113-
return os.path.join(self.data_dir, _PG_LOG_FILE)
113+
return os.path.join(self.logs_dir, _PG_LOG_FILE)
114114

115115
def _create_recovery_conf(self, username, master):
116116
# yapf: disable
@@ -148,37 +148,25 @@ def _maybe_stop_logger(self):
148148
self._logger.stop()
149149

150150
def _format_verbose_error(self):
151-
# choose log_filename
152-
log_filename = self.pg_log_name
153-
154-
# choose conf_filename
155-
conf_filename = os.path.join(self.data_dir, "postgresql.conf")
156-
157-
# choose hba_filename
158-
hba_filename = os.path.join(self.data_dir, "pg_hba.conf")
151+
# list of important files
152+
files = [
153+
os.path.join(self.data_dir, "postgresql.conf"),
154+
os.path.join(self.data_dir, "recovery.conf"),
155+
os.path.join(self.data_dir, "pg_hba.conf"),
156+
self.pg_log_name # main log file
157+
]
159158

160-
# choose recovery_filename
161-
recovery_filename = os.path.join(self.data_dir, "recovery.conf")
159+
error_text = ""
162160

163-
def print_node_file(node_file):
164-
if os.path.exists(node_file):
165-
try:
166-
with io.open(node_file, "r") as f:
167-
return f.read().decode('utf-8')
168-
except Exception:
169-
pass
170-
return "### file not found ###\n"
161+
for f in files:
162+
# skip missing files
163+
if not os.path.exists(f):
164+
continue
171165

172-
# yapf: disable
173-
error_text = (
174-
u"{}:\n----\n{}\n" # log file, e.g. postgresql.log
175-
u"{}:\n----\n{}\n" # postgresql.conf
176-
u"{}:\n----\n{}\n" # pg_hba.conf
177-
u"{}:\n----\n{}\n" # recovery.conf
178-
).format(log_filename, print_node_file(log_filename),
179-
conf_filename, print_node_file(conf_filename),
180-
hba_filename, print_node_file(hba_filename),
181-
recovery_filename, print_node_file(recovery_filename))
166+
# append contents
167+
with io.open(f, "r") as _f:
168+
lines = _f.read()
169+
error_text += u"{}:\n----\n{}\n".format(f, lines)
182170

183171
return error_text
184172

@@ -410,12 +398,12 @@ def start(self, params=[]):
410398

411399
try:
412400
_execute_utility(_params, self.utils_log_name)
413-
except ExecUtilException:
401+
except ExecUtilException as e:
414402
msg = (
415403
u"Cannot start node\n"
416404
u"{}\n" # pg_ctl log
417405
).format(self._format_verbose_error())
418-
raise StartNodeException(msg)
406+
raise_from(StartNodeException(msg), e)
419407

420408
self._maybe_start_logger()
421409

@@ -468,12 +456,12 @@ def restart(self, params=[]):
468456

469457
try:
470458
_execute_utility(_params, self.utils_log_name)
471-
except ExecUtilException:
459+
except ExecUtilException as e:
472460
msg = (
473461
u"Cannot restart node\n"
474462
u"{}\n" # pg_ctl log
475463
).format(self._format_verbose_error())
476-
raise StartNodeException(msg)
464+
raise_from(StartNodeException(msg), e)
477465

478466
self._maybe_start_logger()
479467

@@ -837,7 +825,7 @@ def catchup(self, dbname='postgres', username=None):
837825
query=wait_lsn.format(lsn),
838826
max_attempts=0) # infinite
839827
except Exception as e:
840-
raise CatchUpException(_explain_exception(e))
828+
raise_from(CatchUpException('Failed to catch up'), e)
841829

842830
def pgbench(self, dbname='postgres', stdout=None, stderr=None, options=[]):
843831
"""

testgres/utils.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ def default_username():
4747
return pwd.getpwuid(os.getuid())[0]
4848

4949

50-
def explain_exception(e):
51-
"""
52-
Use this function instead of str(e).
53-
"""
54-
55-
import traceback # used only here
56-
lines = traceback.format_exception_only(type(e), e)
57-
return ''.join(lines)
58-
59-
6050
def execute_utility(args, logfile):
6151
"""
6252
Execute utility (pg_ctl, pg_dump etc).

0 commit comments

Comments
 (0)