From 1f21f8dc327d8c39c253ee2c9d5615cf21db0c8d Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 7 Jan 2019 23:56:06 +0000 Subject: [PATCH] better error handling for extension --- server/grasp_server.py | 38 +++++++++++++++++++------------------- src/js/background.js | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/server/grasp_server.py b/server/grasp_server.py index c8808b5..b53b296 100755 --- a/server/grasp_server.py +++ b/server/grasp_server.py @@ -54,27 +54,27 @@ def capture( ]) body = None if len(parts) == 0 else '\n'.join(parts) - # TODO format response on extension site response = { - 'file': str(capture_path), + 'path': str(capture_path), } - try: - append_org_entry( - capture_path, - heading=heading, - body=body, - tags=tags, - todo=False, - ) - response.update({ - 'status': 'ok', - }) - except Exception as e: - log(str(e)) - response.update({ - 'status': 'error', - 'error' : str(e), - }) + # try: + append_org_entry( + capture_path, + heading=heading, + body=body, + tags=tags, + todo=False, + ) + response.update({ + 'status': 'ok', + }) + # just rely on setting 500 instead + # except Exception as e: + # log(str(e)) + # response.update({ + # 'status': 'error', + # 'error' : str(e), + # }) return json.dumps(response).encode('utf8') diff --git a/src/js/background.js b/src/js/background.js index e5a66fe..3323ded 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -25,26 +25,38 @@ function makeCaptureRequest( if (request.readyState != 4) { return; } - console.log('[background] status:', request.status); - if (request.status >= 200 && request.status < 400) { // success - // TODO handle json parsing defensively here - var response = JSON.parse(request.responseText); - console.log(`[background] success: ${response}`); - showNotification(`OK: ${response}`); - } else { - // TODO more error context? - console.log(`[background] ERROR: ${request.status}`); - - var explain = ""; - if (request.status === 0) { - explain = `Unavaiable: ${capture_url()}`; + const status = request.status; + const rtext = request.responseText; + var had_error = false; + var error_message = `status ${status}, response ${rtext}`; + console.log(`[background] status: ${status}, response: ${rtext}`); + if (status >= 200 && status < 400) { // success + try { + // TODO handle json parsing defensively here + const response = JSON.parse(rtext); + const path = response.path; + console.log(`[background] success: ${response}`); + showNotification(`OK: captured to ${path}`); + } catch (err) { + had_error = true; + error_message = error_message.concat(String(err)); + console.error(err); } - showNotification(`ERROR: ${request.status}: ${explain}`, 1); + } else { + had_error = true; + if (status == 0) { + error_message = error_message.concat(` ${capture_url()} must be unavailable `); + } + } + + if (had_error) { + console.error(`[background] ERROR: ${error_message}`); + showNotification(`ERROR: ${error_message}`, 1); // TODO crap, doesn't really seem to respect urgency... } }; request.onerror = () => { - console.log(request); + console.error(request); }; request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");