Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #45 from Esri/feat/script-onerror
Browse files Browse the repository at this point in the history
handle script load errors
  • Loading branch information
tomwayson authored Nov 7, 2017
2 parents d4ad4c2 + 326da91 commit bb706f1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- handle script load errors

## [1.3.0]

### Added
Expand Down
12 changes: 12 additions & 0 deletions src/esri-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function isLoaded() {

// load the ArcGIS API on the page
export function bootstrap(callback?: (error: Error, dojoRequire?: any) => void, options: IBootstrapOptions = {}) {
// default options
if (!options.url) {
options.url = 'https://js.arcgis.com/4.5/';
}
Expand Down Expand Up @@ -72,6 +73,17 @@ export function bootstrap(callback?: (error: Error, dojoRequire?: any) => void,
}
};

// handle any script loading errors
const onScriptError = (e) => {
if (callback) {
// pass the error to the callback
callback(e.error || new Error(`There was an error attempting to load ${script.src}`));
}
// remove this event listener
script.removeEventListener('error', onScriptError, false);
};
script.addEventListener('error', onScriptError, false);

// load the script
document.body.appendChild(script);
}
Expand Down
42 changes: 29 additions & 13 deletions test/esri-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,19 @@ describe('esri-loader', function () {
var context = {
bootstrapCallback: function () {}
};
describe('w/ a callback', function () {
beforeAll(function () {
spyOn(document.body, 'appendChild').and.callFake(function (el) {
// hold onto script element for assertions below
scriptEl = el;
// call the onload callback
el.onload();
});
spyOn(document, 'querySelector').and.callFake(function() {
console.log('querySelector', scriptEl);
return scriptEl;
});
spyOn(context, 'bootstrapCallback');
beforeAll(function () {
spyOn(document.body, 'appendChild').and.callFake(function (el) {
// hold onto script element for assertions below
scriptEl = el;
// call the onload callback
el.onload();
});
spyOn(document, 'querySelector').and.callFake(function() {
return scriptEl;
});
spyOn(context, 'bootstrapCallback');
});
describe('w/ a callback', function () {
it('should pass an error to the callback', function () {
esriLoader.bootstrap(context.bootstrapCallback, {
url: 'https://js.arcgis.com/3.20'
Expand All @@ -124,6 +123,23 @@ describe('esri-loader', function () {
});
});
});
describe('when loading an invalid url', function () {
it('should pass an error to the callback', function (done) {
esriLoader.bootstrap((err) => {
expect(err.message.indexOf('There was an error attempting to load')).toEqual(0);
done();
}, {
url: 'not a valid url'
});
});
afterAll(function () {
// remove script tag
const script = document.querySelector('script[data-esri-loader]');
if (script) {
script.parentElement.removeChild(script);
}
});
});
});

describe('when loading modules', function () {
Expand Down

0 comments on commit bb706f1

Please sign in to comment.