From 46f1fd2cd0650be590dcb9f8c017405552860cdd Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 21 Sep 2015 23:21:26 -0700 Subject: [PATCH] factor error classes into a common file --- src/wormhole/blocking/transcribe.py | 25 +++---------------------- src/wormhole/errors.py | 22 +++++++++++++++++++++- src/wormhole/twisted/transcribe.py | 14 ++------------ 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index d61558b..bdd4643 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -1,5 +1,5 @@ from __future__ import print_function -import os, sys, time, re, requests, json, textwrap +import os, sys, time, re, requests, json from binascii import hexlify, unhexlify from spake2 import SPAKE2_Symmetric from nacl.secret import SecretBox @@ -8,32 +8,13 @@ from nacl import utils from .eventsource import EventSourceFollower from .. import __version__ from .. import codes -from ..errors import ServerError +from ..errors import (ServerError, Timeout, WrongPasswordError, + ReflectionAttack, UsageError) from ..util.hkdf import HKDF SECOND = 1 MINUTE = 60*SECOND -class Timeout(Exception): - pass - -class WrongPasswordError(Exception): - """ - Key confirmation failed. Either you or your correspondent typed the code - wrong, or a would-be man-in-the-middle attacker guessed incorrectly. You - could try again, giving both your correspondent and the attacker another - chance. - """ - # or the data blob was corrupted, and that's why decrypt failed - def explain(self): - return textwrap.dedent(self.__doc__) - -class ReflectionAttack(Exception): - """An attacker (or bug) reflected our outgoing message back to us.""" - -class UsageError(Exception): - """The programmer did something wrong.""" - # relay URLs are: # GET /list -> {channel-ids: [INT..]} # POST /allocate/SIDE -> {channel-id: INT} diff --git a/src/wormhole/errors.py b/src/wormhole/errors.py index 2e8de3e..41d23ad 100644 --- a/src/wormhole/errors.py +++ b/src/wormhole/errors.py @@ -1,4 +1,4 @@ -import functools +import functools, textwrap class ServerError(Exception): def __init__(self, message, relay): @@ -16,3 +16,23 @@ def handle_server_error(func): print("Server error (from %s):\n%s" % (e.relay, e.message)) return 1 return _wrap + +class Timeout(Exception): + pass + +class WrongPasswordError(Exception): + """ + Key confirmation failed. Either you or your correspondent typed the code + wrong, or a would-be man-in-the-middle attacker guessed incorrectly. You + could try again, giving both your correspondent and the attacker another + chance. + """ + # or the data blob was corrupted, and that's why decrypt failed + def explain(self): + return textwrap.dedent(self.__doc__) + +class ReflectionAttack(Exception): + """An attacker (or bug) reflected our outgoing message back to us.""" + +class UsageError(Exception): + """The programmer did something wrong.""" diff --git a/src/wormhole/twisted/transcribe.py b/src/wormhole/twisted/transcribe.py index a7eaaad..616b881 100644 --- a/src/wormhole/twisted/transcribe.py +++ b/src/wormhole/twisted/transcribe.py @@ -13,20 +13,10 @@ from spake2 import SPAKE2_Symmetric from .eventsource_twisted import ReconnectingEventSource from .. import __version__ from .. import codes -from ..errors import ServerError +from ..errors import (ServerError, WrongPasswordError, + ReflectionAttack, UsageError) from ..util.hkdf import HKDF -class WrongPasswordError(Exception): - """ - Key confirmation failed. - """ - -class ReflectionAttack(Exception): - """An attacker (or bug) reflected our outgoing message back to us.""" - -class UsageError(Exception): - """The programmer did something wrong.""" - @implementer(IBodyProducer) class DataProducer: def __init__(self, data):