Skip to content

Commit

Permalink
improve ajax error messages
Browse files Browse the repository at this point in the history
A failed GeoJSON load now logs
`AJAXError: Not Found (404): http://example.com/asdf.geojson`
instead of
`Error`
  • Loading branch information
ansis committed Feb 8, 2018
1 parent b88ec9d commit c71e823
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit c71e823

Please sign in to comment.