From dc52200af89db6ea1161d52edcf9a2aa6210ef17 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 11 Nov 2019 11:52:35 +0000 Subject: [PATCH] handle .connect() failure in bitbotctl --- bitbotctl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bitbotctl b/bitbotctl index 2753d92d..86790c0f 100755 --- a/bitbotctl +++ b/bitbotctl @@ -16,9 +16,9 @@ arg_parser.add_argument("command") args, unknown = arg_parser.parse_known_args() -def _die(s): +def _die(code: int, s: str): sys.stderr.write("%s\n" % s) - sys.exit(1) + sys.exit(code) SIMPLE = ["rehash", "reload", "stop"] if args.command == "log": @@ -27,13 +27,10 @@ if args.command == "log": elif args.command in SIMPLE: pass else: - _die("Unknown command '%s'" % args.command) + _die(1, "Unknown command '%s'" % args.command) args = arg_parser.parse_args() -if not os.path.exists(args.socket): - _die("Failed to connect to BitBot instance") - import json, socket, signal def _sigint(_1, _2): @@ -41,9 +38,12 @@ def _sigint(_1, _2): sys.exit(0) signal.signal(signal.SIGINT, _sigint) - sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) -sock.connect(args.socket) + +try: + sock.connect(args.socket) +except ConnectionRefusedError: + _die(2, "Failed to connect to BitBot instance. Is bitbotd running?") def _send(s): sock.send(("%s\n" % s).encode("utf8"))