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,11 +54,10 @@ 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,
@ -69,12 +68,13 @@ def capture(
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;
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 // TODO handle json parsing defensively here
var response = JSON.parse(request.responseText); const response = JSON.parse(rtext);
const path = response.path;
console.log(`[background] success: ${response}`); console.log(`[background] success: ${response}`);
showNotification(`OK: ${response}`); showNotification(`OK: captured to ${path}`);
} else { } catch (err) {
// TODO more error context? had_error = true;
console.log(`[background] ERROR: ${request.status}`); error_message = error_message.concat(String(err));
console.error(err);
var explain = "";
if (request.status === 0) {
explain = `Unavaiable: ${capture_url()}`;
} }
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");