From 94c0c27c25965479eccc9f4eae24f9e313d44220 Mon Sep 17 00:00:00 2001 From: Frank Ploss Date: Sat, 19 Dec 2015 13:43:55 +0100 Subject: [PATCH] Handle errors a little more gracefully --- extension.js | 28 +++++++++++++++++++--------- tor_control_client.js | 12 +++++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/extension.js b/extension.js index d2bc6a4..8036ab5 100644 --- a/extension.js +++ b/extension.js @@ -4,6 +4,7 @@ const Lang = imports.lang; const St = imports.gi.St; const Main = imports.ui.main; +const Notify = imports.gi.Notify; const PanelMenu = imports.ui.panelMenu; const Me = imports.misc.extensionUtils.getCurrentExtension(); @@ -11,8 +12,8 @@ const TorControlClient = Me.imports.tor_control_client.TorControlClient; const TorIcon = 'tor'; -let torButton; -let torControlClient; +let torButton = null; +let torControlClient = null; const TorButton = new Lang.Class({ Name: 'TorButton', @@ -43,14 +44,23 @@ function init(extensionMeta) { } function enable() { - torControlClient = new TorControlClient(); - torButton = new TorButton(); - Main.panel.addToStatusArea(torButton.Name, torButton); + try { + torControlClient = new TorControlClient(); + torButton = new TorButton(); + Main.panel.addToStatusArea(torButton.Name, torButton); + } catch (e) { + Main.notifyError('Error starting extension: ' + e); + disable(); + } } function disable() { - torButton.destroy(); - torButton = null; - torControlClient.close(); - torControlClient = null; + if (torButton !== null) { + torButton.destroy(); + torButton = null; + } + if (torControlClient !== null) { + torControlClient.close(); + torControlClient = null; + } } diff --git a/tor_control_client.js b/tor_control_client.js index fc0f4ea..f119d00 100644 --- a/tor_control_client.js +++ b/tor_control_client.js @@ -14,15 +14,17 @@ const TorControlClient = new Lang.Class({ }, close: function() { - this._outputStream.close(null); - this._inputStream.close(null); + if (this._connection.is_connected()) { + this._outputStream.close(null); + this._inputStream.close(null); + } }, _connect: function() { var socketClient = new Gio.SocketClient(); - var connection = socketClient.connect_to_host('127.0.0.1:9051', null, null); - this._inputStream = new Gio.DataInputStream({base_stream: connection.get_input_stream()}); - this._outputStream = new Gio.DataOutputStream({base_stream: connection.get_output_stream()}); + this._connection = socketClient.connect_to_host('127.0.0.1:9051', null, null); + this._inputStream = new Gio.DataInputStream({base_stream: this._connection.get_input_stream()}); + this._outputStream = new Gio.DataOutputStream({base_stream: this._connection.get_output_stream()}); }, _updateProtocolInfo: function() {