From c4cd4db30eea37b2a5fdee92b249c4e06e08e537 Mon Sep 17 00:00:00 2001 From: meejah Date: Wed, 22 Jun 2016 01:25:47 -0600 Subject: [PATCH] Correct exit code when no twistd.pid file --- src/wormhole/errors.py | 7 ++++++- src/wormhole/server/cmd_server.py | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/wormhole/errors.py b/src/wormhole/errors.py index 4782830..f994659 100644 --- a/src/wormhole/errors.py +++ b/src/wormhole/errors.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals import functools +import click class ServerError(Exception): def __init__(self, message, relay): @@ -47,7 +48,11 @@ class KeyFormatError(Exception): class ReflectionAttack(Exception): """An attacker (or bug) reflected our outgoing message back to us.""" -class UsageError(Exception): +# Click needs to receive click.UsageError instances to "do the right +# thing", which is print the error and exit -- perhaps it would be +# better just to re-export click.UsageError here? Or use +# click.UsageError throughout the codebase? +class UsageError(click.UsageError, Exception): """The programmer did something wrong.""" class WormholeClosedError(UsageError): diff --git a/src/wormhole/server/cmd_server.py b/src/wormhole/server/cmd_server.py index d149b82..9f9583d 100644 --- a/src/wormhole/server/cmd_server.py +++ b/src/wormhole/server/cmd_server.py @@ -1,5 +1,6 @@ from __future__ import print_function, unicode_literals import os, time +from ..errors import UsageError from twisted.python import usage from twisted.scripts import twistd @@ -38,8 +39,9 @@ def kill_server(): try: f = open("twistd.pid", "r") except EnvironmentError: - print("Unable to find twistd.pid . Is this really a server directory?") - return + raise UsageError( + "Unable to find 'twistd.pid' -- is this really a server directory?" + ) pid = int(f.read().strip()) f.close() os.kill(pid, 15)