From 94f1dc3be5f597e1e091fc1668fd334e0b48b829 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Mon, 17 Feb 2014 10:23:37 -0800 Subject: [PATCH] cleanup wbexceptions, remove unused --- pywb/archivalrouter.py | 1 - pywb/indexreader.py | 11 +++++----- pywb/replay_views.py | 11 +++++----- pywb/views.py | 1 - pywb/wbapp.py | 14 ++++++------- pywb/wbexceptions.py | 43 --------------------------------------- tests/test_integration.py | 2 +- 7 files changed, 18 insertions(+), 65 deletions(-) diff --git a/pywb/archivalrouter.py b/pywb/archivalrouter.py index eecd3d9e..354edddd 100644 --- a/pywb/archivalrouter.py +++ b/pywb/archivalrouter.py @@ -1,6 +1,5 @@ import urlparse import re -import wbexceptions from wbrequestresponse import WbRequest, WbResponse from pywb.rewrite.url_rewriter import UrlRewriter diff --git a/pywb/indexreader.py b/pywb/indexreader.py index 959e9f67..236b83f0 100644 --- a/pywb/indexreader.py +++ b/pywb/indexreader.py @@ -1,6 +1,7 @@ import urllib import urllib2 -import wbexceptions + +from wbexceptions import NotFoundException from itertools import chain from pprint import pprint @@ -30,15 +31,13 @@ class IndexReader(object): params.update(wbrequest.custom_params) params['url'] = wburl.url - try: - cdxlines = self.load_cdx(output='raw', **params) - except CDXException: - raise wbexceptions.BadUrlException('Bad Request Url: ' + wburl.url) + + cdxlines = self.load_cdx(output='raw', **params) cdxlines = self.peek_iter(cdxlines) if cdxlines is None: - raise wbexceptions.NotFoundException('WB Does Not Have Url: ' + wburl.url) + raise NotFoundException('No Captures found for: ' + wburl.url) return cdxlines diff --git a/pywb/replay_views.py b/pywb/replay_views.py index 53b60fc4..f5f9c504 100644 --- a/pywb/replay_views.py +++ b/pywb/replay_views.py @@ -4,7 +4,8 @@ from pywb.rewrite.url_rewriter import UrlRewriter from pywb.utils.bufferedreaders import ChunkedDataReader from wbrequestresponse import WbResponse -import wbexceptions +from wbexceptions import CaptureException, InternalRedirect +from pywb.warc.recordloader import ArchiveLoadFailed #================================================================= @@ -64,7 +65,7 @@ class ReplayView: return response - except wbexceptions.CaptureException as ce: + except (CaptureException, ArchiveLoadFailed) as ce: import traceback traceback.print_exc() last_e = ce @@ -73,7 +74,7 @@ class ReplayView: if last_e: raise last_e else: - raise wbexceptions.UnresolvedArchiveFileException() + raise WbException('No Content Loaded for: ' + wbrequest.wb_url) @staticmethod def stream_to_iter(stream): @@ -141,7 +142,7 @@ class ReplayView: def _redirect_if_needed(self, wbrequest, cdx): if self.redir_to_exact and not wbrequest.is_proxy and cdx and (cdx['timestamp'] != wbrequest.wb_url.timestamp): new_url = wbrequest.urlrewriter.get_timestamp_url(cdx['timestamp'], cdx['original']) - raise wbexceptions.InternalRedirect(new_url) + raise InternalRedirect(new_url) return None @@ -153,5 +154,5 @@ class ReplayView: #TODO: canonicalize before testing? if (UrlRewriter.strip_protocol(request_url) == UrlRewriter.strip_protocol(location_url)): - raise wbexceptions.CaptureException('Self Redirect: ' + str(cdx)) + raise CaptureException('Self Redirect: ' + str(cdx)) diff --git a/pywb/views.py b/pywb/views.py index 681f6c97..f693d1e6 100644 --- a/pywb/views.py +++ b/pywb/views.py @@ -1,7 +1,6 @@ import pywb.utils.timeutils as timeutils import wbrequestresponse -import wbexceptions import urlparse import time diff --git a/pywb/wbapp.py b/pywb/wbapp.py index 5fef353e..0befa172 100644 --- a/pywb/wbapp.py +++ b/pywb/wbapp.py @@ -1,7 +1,8 @@ -import wbexceptions - +from wbexceptions import WbException, NotFoundException, InternalRedirect from wbrequestresponse import WbResponse, StatusAndHeaders + from pywb.cdx.cdxserver import CDXException +from pywb.warc.recordloader import ArchiveLoadFailed import os import importlib @@ -49,17 +50,14 @@ def create_wb_app(wb_router): response = wb_router(env) if not response: - raise wbexceptions.NotFoundException('No handler for "{0}"'.format(env['REL_REQUEST_URI'])) + raise NotFoundException('No handler for "{0}"'.format(env['REL_REQUEST_URI'])) - except wbexceptions.InternalRedirect as ir: + except InternalRedirect as ir: response = WbResponse(StatusAndHeaders(ir.status, ir.httpHeaders)) - except (wbexceptions.NotFoundException, wbexceptions.AccessException) as e: + except (WbException, CDXException, ArchiveLoadFailed) as e: response = handle_exception(env, wb_router.error_view, e, False) - except (wbexceptions.WbException, CDXException) as wbe: - response = handle_exception(env, wb_router.error_view, wbe, False) - except Exception as e: response = handle_exception(env, wb_router.error_view, e, True) diff --git a/pywb/wbexceptions.py b/pywb/wbexceptions.py index 06be06bf..8796db76 100644 --- a/pywb/wbexceptions.py +++ b/pywb/wbexceptions.py @@ -2,26 +2,6 @@ class WbException(Exception): pass -class RequestParseException(WbException): - def __init__(self, string, to_parse): - WbException.__init__(self, string + to_parse) - self.to_parse = to_parse - - def status(_): - return '400 Bad Request' - -class BadUrlException(WbException): - def status(_): - return '400 Bad Request' - -class AccessException(WbException): - def status(_): - return '403 Forbidden' - -class InvalidCDXException(WbException): - def status(_): - return '500 Internal Server Error' - class NotFoundException(WbException): def status(_): return '404 Not Found' @@ -31,29 +11,6 @@ class CaptureException(WbException): def status(_): return '500 Internal Server Error' -class UnresolvedArchiveFileException(CaptureException): - pass - -class UnknownArchiveFormatException(CaptureException): - pass - -class UnknownLoaderProtocolException(CaptureException): - pass - -class InvalidArchiveRecordException(CaptureException): - def __init__(self, msg, errList = None): - super(InvalidArchiveRecordException, self).__init__(msg) - self.errList = errList - -class ArchiveLoadFailed(CaptureException): - def __init__(self, filename, reason): - super(ArchiveLoadFailed, self).__init__(filename + ':' + str(reason)) - self.filename = filename - self.reason = reason - - def status(_): - return '503 Service Unavailable' - class InternalRedirect(WbException): def __init__(self, location, status = '302 Internal Redirect'): WbException.__init__(self, 'Redirecting -> ' + location) diff --git a/tests/test_integration.py b/tests/test_integration.py index 9047e184..e639163b 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -160,7 +160,7 @@ class TestWb: def test_error(self): resp = self.testapp.get('/pywb/?abc', status = 400) assert resp.status_int == 400 - assert 'Bad Request Url: http://?abc' in resp.body + assert 'Invalid Url: http://?abc' in resp.body # Reporter callback for replay view def print_reporter(wbrequest, cdx, response):