diff --git a/weboob/tools/browser2/browser.py b/weboob/tools/browser2/browser.py index 5ee78837..bef5bdf7 100644 --- a/weboob/tools/browser2/browser.py +++ b/weboob/tools/browser2/browser.py @@ -39,6 +39,7 @@ except ImportError: from weboob.tools.log import getLogger from .cookies import WeboobCookieJar +from .exceptions import HTTPNotFound, ClientError, ServerError class Profile(object): @@ -318,9 +319,29 @@ class BaseBrowser(object): if allow_redirects: response = self.handle_refresh(response) + self.raise_for_status(response) + return response + + def raise_for_status(self, response): + """ + Like Response.raise_for_status but will use other classes if needed. + """ + http_error_msg = None + if 400 <= response.status_code < 500: + http_error_msg = '%s Client Error: %s' % (response.status_code, response.reason) + cls = ClientError + if response.status_code == 404: + cls = HTTPNotFound + elif 500 <= response.status_code < 600: + http_error_msg = '%s Server Error: %s' % (response.status_code, response.reason) + cls = ServerError + + if http_error_msg: + raise cls(http_error_msg, response=response) + + # in case we did not catch something that should be response.raise_for_status() - return response def build_request(self, url, referrer=None, data_encoding=None, **kwargs): """ diff --git a/weboob/tools/browser2/exceptions.py b/weboob/tools/browser2/exceptions.py new file mode 100644 index 00000000..6c5c6585 --- /dev/null +++ b/weboob/tools/browser2/exceptions.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2014 Laurent Bachelier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from requests.exceptions import HTTPError +from weboob.tools.exceptions import BrowserHTTPError, BrowserHTTPNotFound + + +class HTTPNotFound(HTTPError, BrowserHTTPNotFound): + pass + + +class ClientError(HTTPError, BrowserHTTPError): + pass + + +class ServerError(HTTPError, BrowserHTTPError): + pass