better error handling for extension

This commit is contained in:
Dima Gerasimov 2019-01-07 23:56:06 +00:00
parent dc568f4785
commit 1f21f8dc32
2 changed files with 46 additions and 34 deletions

View file

@ -54,27 +54,27 @@ def capture(
]) ])
body = None if len(parts) == 0 else '\n'.join(parts) body = None if len(parts) == 0 else '\n'.join(parts)
# TODO format response on extension site
response = { response = {
'file': str(capture_path), 'path': str(capture_path),
} }
try: # try:
append_org_entry( append_org_entry(
capture_path, capture_path,
heading=heading, heading=heading,
body=body, body=body,
tags=tags, tags=tags,
todo=False, todo=False,
) )
response.update({ response.update({
'status': 'ok', 'status': 'ok',
}) })
except Exception as e: # just rely on setting 500 instead
log(str(e)) # except Exception as e:
response.update({ # log(str(e))
'status': 'error', # response.update({
'error' : str(e), # 'status': 'error',
}) # 'error' : str(e),
# })
return json.dumps(response).encode('utf8') return json.dumps(response).encode('utf8')

View file

@ -25,26 +25,38 @@ function makeCaptureRequest(
if (request.readyState != 4) { if (request.readyState != 4) {
return; return;
} }
console.log('[background] status:', request.status); const status = request.status;
if (request.status >= 200 && request.status < 400) { // success const rtext = request.responseText;
// TODO handle json parsing defensively here var had_error = false;
var response = JSON.parse(request.responseText); var error_message = `status ${status}, response ${rtext}`;
console.log(`[background] success: ${response}`); console.log(`[background] status: ${status}, response: ${rtext}`);
showNotification(`OK: ${response}`); if (status >= 200 && status < 400) { // success
} else { try {
// TODO more error context? // TODO handle json parsing defensively here
console.log(`[background] ERROR: ${request.status}`); const response = JSON.parse(rtext);
const path = response.path;
var explain = ""; console.log(`[background] success: ${response}`);
if (request.status === 0) { showNotification(`OK: captured to ${path}`);
explain = `Unavaiable: ${capture_url()}`; } 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... // TODO crap, doesn't really seem to respect urgency...
} }
}; };
request.onerror = () => { request.onerror = () => {
console.log(request); console.error(request);
}; };
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");