mapbox.js icon indicating copy to clipboard operation
mapbox.js copied to clipboard

TypeError: Cannot read property 'sources' of null

Open rarkins opened this issue 9 years ago • 3 comments

Mapbox version: 2.4.0 Leaflet version: 0.7.7

I am using a client-side exception-catching service, and see a small percentage of cases with the following exception:

TypeError: Cannot read property 'sources' of null
  File "node_modules/mapbox.js/src/style_layer.js", line 32, in e.<anonymous>
                for (var id in style.sources) {
  File "node_modules/leaflet/dist/leaflet-src.js", line 54, in [anonymous]
                        return fn.apply(obj, args || arguments);
  File "node_modules/mapbox.js/src/request.js", line 28, in XMLHttpRequest.r
            callback(err, resp);
  File "node_modules/corslite/corslite.js", line 65, in XMLHttpRequest.r.onerror
            callback.call(this, evt || true, null);

The impacted code corresponds to https://github.com/mapbox/mapbox.js/blob/mb-pages/src/style_layer.js#L26-L35:

        request(styleURL, L.bind(function(err, style) {
            if (err) {
                util.log('could not load Mapbox style at ' + styleURL);
                this.fire('error', {error: err});
            }
            var sources = [];
            for (var id in style.sources) {
                var source = style.sources[id].url.split('mapbox://')[1];
                sources.push(source);
            }

I do not see any 'could not load Mapbox style' log from the users prior to the null sources error, so either that util.log is suppressed or perhaps err is null?

rarkins avatar Sep 06 '16 07:09 rarkins

cc @bsudekum

jfirebaugh avatar Sep 06 '16 16:09 jfirebaugh

Looking at the code, it looks like we're not safe guarding against styles with no sources.

Will put together a test case for this.

bsudekum avatar Sep 06 '16 16:09 bsudekum

The code also doesn't exit early if an error is found, so even if err is defined and style is null, it'll still try to iterate over them. @bsudekum do you have bandwidth to get a fix in?

tmcw avatar Sep 21 '16 02:09 tmcw