From 0ab996d01cecbe2e6a6567c16f65510d785a8e4b Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Mon, 22 Jan 2018 14:08:58 -0500 Subject: [PATCH] improve ajax error messages A failed GeoJSON load now logs `AJAXError: Not Found (404): http://example.com/asdf.geojson` instead of `Error` --- src/util/ajax.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/util/ajax.js b/src/util/ajax.js index 04ffa8e1580..f39cb83dc40 100644 --- a/src/util/ajax.js +++ b/src/util/ajax.js @@ -42,9 +42,19 @@ export type RequestParameters = { class AJAXError extends Error { status: number; - constructor(message: string, status: number) { + url: string; + constructor(message: string, status: number, url: string) { super(message); this.status = status; + this.url = url; + + // work around for https://github.com/Rich-Harris/buble/issues/40 + this.name = this.constructor.name; + this.message = message; + } + + toString() { + return `${this.name}: ${this.message} (${this.status}): ${this.url}`; } } @@ -75,7 +85,7 @@ exports.getJSON = function(requestParameters: RequestParameters, callback: Callb } callback(null, data); } else { - callback(new AJAXError(xhr.statusText, xhr.status)); + callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url)); } }; xhr.send(); @@ -100,7 +110,7 @@ exports.getArrayBuffer = function(requestParameters: RequestParameters, callback expires: xhr.getResponseHeader('Expires') }); } else { - callback(new AJAXError(xhr.statusText, xhr.status)); + callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url)); } }; xhr.send();