From 8849468dee05baefef90a3a74f8e9326e6fb0ea6 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 18 Nov 2014 15:46:34 -0700 Subject: [PATCH] Invoke error callback if browser throws exception on download (fixes #110) --- papaparse.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/papaparse.js b/papaparse.js index c5ef226..701747b 100644 --- a/papaparse.js +++ b/papaparse.js @@ -453,12 +453,15 @@ } xhr = new XMLHttpRequest(); + if (!IS_WORKER) { xhr.onload = chunkLoaded; xhr.onerror = chunkError; } + xhr.open("GET", url, !IS_WORKER); + if (config.step) { var end = start + configCopy.chunkSize - 1; // minus one because byte range is inclusive @@ -466,7 +469,14 @@ end = fileSize; xhr.setRequestHeader("Range", "bytes="+start+"-"+end); } - xhr.send(); + + try { + xhr.send(); + } + catch (err) { + chunkError(err.message); + } + if (IS_WORKER && xhr.status == 0) chunkError(); else @@ -535,15 +545,16 @@ nextChunk(); } - function chunkError() + function chunkError(errorMessage) { + var errorText = xhr.statusText || errorMessage; if (isFunction(config.error)) - config.error(xhr.statusText); + config.error(errorText); else if (IS_WORKER && config.error) { global.postMessage({ workerId: Papa.WORKER_ID, - error: xhr.statusText, + error: errorText, finished: false }); }