Skip to content

Commit

Permalink
Cleanup request error handling.
Browse files Browse the repository at this point in the history
Now using a custom error class to highlight errors that we handle vs.
unexpected ones we don't.
  • Loading branch information
Fizzadar committed Jul 8, 2021
1 parent adf1ff4 commit 7bf6d8b
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions kanmail/client/util/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ export function newCriticalRequestNonce() {
}


class RequestError extends Error {
name = 'RequestError';
isInternalError = true;
}


function handleReponse(response, method, options) {
const criticalRequestNonce = options.criticalRequestNonce;
if (criticalRequestNonce && criticalRequestNonce !== currentCriticalRequestNonce) {
const error = new Error(`Blocked due to old critical request nonce (current=${currentCriticalRequestNonce}, response=${criticalRequestNonce}, url=${response.url})!`);
const error = new RequestError(`Blocked due to old critical request nonce (current=${currentCriticalRequestNonce}, response=${criticalRequestNonce}, url=${response.url})!`);
error.isCriticalRequestNonceFailure = true;
throw error;
}
Expand All @@ -43,15 +49,16 @@ function handleReponse(response, method, options) {
data.jsonError = e;
}

const error = new RequestError(`Error fetching: ${method} ${response.url}`);
error.data = data;

if (response.status == 503) {
error.isNetworkResponseFailure = true;
requestStore.addNetworkError(data);
} else if (!options.ignoreStatus || !_.includes(options.ignoreStatus, response.status)) {
requestStore.addRequestError(data);
}

const error = new Error(`Error fetching: ${method} ${response.url}`);
error.data = data;
error.isNetworkResponseFailure = true;
throw error;
});
}
Expand All @@ -64,6 +71,26 @@ function handleReponse(response, method, options) {
}


/*
Handle unexpected network request errors (timeout, etc)
*/
function handleError(url, error) {

if (error.isInternalError) {
throw error;
}

requestStore.addNetworkError({
url: url,
status: 'unknown',
errorName: 'unknown',
errorMessage: error.message,
});
error.isNetworkResponseFailure = true;
throw error;
}


function get_or_delete(method, url, query={}, options={}) {
const uri = URI(url);

Expand All @@ -79,6 +106,7 @@ function get_or_delete(method, url, query={}, options={}) {
}
})
.then(response => handleReponse(response, method, options))
.catch(error => handleError(url, error))
);
}

Expand All @@ -104,6 +132,7 @@ function post_or_put(method, url, data, options={}) {
},
})
.then(response => handleReponse(response, method, options))
.catch(error => handleError(url, error))
);
}

Expand Down

0 comments on commit 7bf6d8b

Please sign in to comment.