diff --git a/cps/kobo.py b/cps/kobo.py index 34bf5be4..182bf21d 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -106,24 +106,29 @@ def make_request_to_kobo_store(sync_token=None): return store_response -def redirect_or_proxy_request(): +def redirect_or_proxy_request(auth=False): if config.config_kobo_proxy: - if request.method == "GET": - return redirect(get_store_url_for_current_request(), 307) - else: - # The Kobo device turns other request types into GET requests on redirects, - # so we instead proxy to the Kobo store ourselves. - store_response = make_request_to_kobo_store() + try: + if request.method == "GET": + alfa = redirect(get_store_url_for_current_request(), 307) + return alfa + else: + # The Kobo device turns other request types into GET requests on redirects, + # so we instead proxy to the Kobo store ourselves. + store_response = make_request_to_kobo_store() - response_headers = store_response.headers - for header_key in CONNECTION_SPECIFIC_HEADERS: - response_headers.pop(header_key, default=None) + response_headers = store_response.headers + for header_key in CONNECTION_SPECIFIC_HEADERS: + response_headers.pop(header_key, default=None) - return make_response( - store_response.content, store_response.status_code, response_headers.items() - ) - else: - return make_response(jsonify({})) + return make_response( + store_response.content, store_response.status_code, response_headers.items() + ) + except Exception as e: + log.error("Failed to receive or parse response from Kobo's endpoint: {}".format(e)) + if auth: + return make_calibre_web_auth_response() + return make_response(jsonify({})) def convert_to_kobo_timestamp_string(timestamp): @@ -1042,7 +1047,7 @@ def HandleAuthRequest(): log.debug('Kobo Auth request') if config.config_kobo_proxy: try: - return redirect_or_proxy_request() + return redirect_or_proxy_request(auth=True) except Exception: log.error("Failed to receive or parse response from Kobo's auth endpoint. Falling back to un-proxied mode.") return make_calibre_web_auth_response()