Merge lp:~townsend/libertine/catch-runtime-errors into lp:libertine

Proposed by Christopher Townsend
Status: Merged
Approved by: Larry Price
Approved revision: 205
Merged at revision: 206
Proposed branch: lp:~townsend/libertine/catch-runtime-errors
Merge into: lp:libertine
Diff against target: 141 lines (+50/-17)
2 files modified
python/libertine/Libertine.py (+37/-13)
tools/libertine-container-manager (+13/-4)
To merge this branch: bzr merge lp:~townsend/libertine/catch-runtime-errors
Reviewer Review Type Date Requested Status
Larry Price Approve
Libertine CI Bot continuous-integration Approve
Review via email: mp+292158@code.launchpad.net

Commit message

Catch runtime errors raised when starting a container so a crash file is not generated and to tell the GUI that there was an error so the user can be presented a proper notification.

To post a comment you must log in.
Revision history for this message
Libertine CI Bot (libertine-ci-bot) wrote :
review: Needs Fixing (continuous-integration)
205. By Christopher Townsend

Fix remove-package return values to fix broken test.

Revision history for this message
Libertine CI Bot (libertine-ci-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Larry Price (larryprice) wrote :

This feels like a lot of redundant code with a try-catch in every throwable method. Have you considered making a class or helper function that takes in and calls a lambda wrapped in the try-catch? Something like:

def try_container_action(self, func):
    try:
        with ContainerRunning(self.container):
            func()
    except RuntimeError as e:
        return handle_runtime_error(e)

Where "func" is a lambda for the one-liner in each method. You could also pull the contents of handle_runtime_error(e) into that method. Your thoughts?

Revision history for this message
Larry Price (larryprice) wrote :

> This feels like a lot of redundant code with a try-catch in every throwable
> method. Have you considered making a class or helper function that takes in
> and calls a lambda wrapped in the try-catch? Something like:
>
> def try_container_action(self, func):
> try:
> with ContainerRunning(self.container):
> func()
> except RuntimeError as e:
> return handle_runtime_error(e)
>
> Where "func" is a lambda for the one-liner in each method. You could also pull
> the contents of handle_runtime_error(e) into that method. Your thoughts?

We talked about this and decided to move on with our lives.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'python/libertine/Libertine.py'
2--- python/libertine/Libertine.py 2016-04-13 18:02:56 +0000
3+++ python/libertine/Libertine.py 2016-04-18 15:21:37 +0000
4@@ -59,6 +59,11 @@
5 return '/usr/bin/apt ' + apt_args_for_verbosity_level(verbosity) + ' --option Apt::Cmd::Disable-Script-Warning=true '
6
7
8+def handle_runtime_error(error):
9+ print("%s" % error)
10+ return False
11+
12+
13 class BaseContainer(metaclass=abc.ABCMeta):
14 """
15 An abstract base container to provide common functionality for all
16@@ -311,15 +316,21 @@
17 """
18 Updates the contents of the container.
19 """
20- with ContainerRunning(self.container):
21- self.container.update_packages(verbosity)
22+ try:
23+ with ContainerRunning(self.container):
24+ self.container.update_packages(verbosity)
25+ except RuntimeError as e:
26+ return handle_runtime_error(e)
27
28 def install_package(self, package_name, verbosity=1):
29 """
30 Installs a package in the container.
31 """
32- with ContainerRunning(self.container):
33- return self.container.install_package(package_name, verbosity)
34+ try:
35+ with ContainerRunning(self.container):
36+ return self.container.install_package(package_name, verbosity)
37+ except RuntimeError as e:
38+ return handle_runtime_error(e)
39
40 def remove_package(self, package_name, verbosity=1):
41 """
42@@ -328,9 +339,13 @@
43 :param package_name: The name of the package to be removed.
44 :param verbosity: The verbosity level of the progress reporting.
45 """
46- with ContainerRunning(self.container):
47- self.container.run_in_container(apt_command_prefix(verbosity) + "purge '" + package_name + "'") == 0
48- self.container.run_in_container(apt_command_prefix(verbosity) + "autoremove --purge") == 0
49+ try:
50+ with ContainerRunning(self.container):
51+ if not self.container.run_in_container(apt_command_prefix(verbosity) + "purge '" + package_name + "'") == 0:
52+ return False
53+ return self.container.run_in_container(apt_command_prefix(verbosity) + "autoremove --purge") == 0
54+ except RuntimeError as e:
55+ return handle_runtime_error(e)
56
57 def search_package_cache(self, search_string):
58 """
59@@ -339,8 +354,11 @@
60 :param search_string: the (regex) to use to search the package cache.
61 The regex is quoted to sanitize it.
62 """
63- with ContainerRunning(self.container):
64- self.container.run_in_container("/usr/bin/apt-cache search '" + search_string + "'")
65+ try:
66+ with ContainerRunning(self.container):
67+ self.container.run_in_container("/usr/bin/apt-cache search '" + search_string + "'")
68+ except RuntimeError as e:
69+ return handle_runtime_error(e)
70
71 def launch_application(self, app_exec_line):
72 """
73@@ -380,9 +398,15 @@
74 example, 'apt-cache policy package-foo'
75 :rtype: The output of the given command.
76 """
77- with ContainerRunning(self.container):
78- return self.container.run_in_container(exec_line)
79+ try:
80+ with ContainerRunning(self.container):
81+ return self.container.run_in_container(exec_line)
82+ except RuntimeError as e:
83+ return handle_runtime_error(e)
84
85 def configure_command(self, command, *args):
86- with ContainerRunning(self.container):
87- return self.container.configure_command(command, *args)
88+ try:
89+ with ContainerRunning(self.container):
90+ return self.container.configure_command(command, *args)
91+ except RuntimeError as e:
92+ return handle_runtime_error(e)
93
94=== modified file 'tools/libertine-container-manager'
95--- tools/libertine-container-manager 2016-04-14 17:05:21 +0000
96+++ tools/libertine-container-manager 2016-04-18 15:21:37 +0000
97@@ -421,7 +421,10 @@
98 container = LibertineContainer(args.id)
99 update_package_install_status(args.id, args.package, "removing")
100
101- container.remove_package(args.package, args.verbosity)
102+ if not container.remove_package(args.package, args.verbosity):
103+ update_package_install_status(args.id, package, "installed")
104+ sys.exit(1)
105+
106 update_package_install_status(args.id, args.package, "removed")
107
108 delete_package(args.id, args.package)
109@@ -435,7 +438,8 @@
110 args.id = get_default_container_id()
111
112 container = LibertineContainer(args.id)
113- container.search_package_cache(args.search_string)
114+ if not container.search_package_cache(args.search_string):
115+ sys.exit(1)
116
117
118 def update(args):
119@@ -448,7 +452,10 @@
120 container = LibertineContainer(args.id)
121
122 update_container_install_status(args.id, "updating")
123- container.update_libertine_container(args.verbosity)
124+ if not container.update_libertine_container(args.verbosity):
125+ update_container_install_status(args.id, "ready")
126+ sys.exit(1)
127+
128 update_container_install_status(args.id, "ready")
129
130
131@@ -500,7 +507,9 @@
132 print("i386 multiarch support is already %s" % multiarch)
133 sys.exit(1)
134
135- container.configure_command('multiarch', args.multiarch)
136+ if not container.configure_command('multiarch', args.multiarch):
137+ sys.exit(1)
138+
139 update_container_multiarch_support(args.id, multiarch)
140
141 elif args.add_archive:

Subscribers

People subscribed via source and target branches