javascript - Leaflet - Possible to catch exception thrown from application -
i'm working on app user can upload kml file , renders paths on screen using leaflet, togeojson & angular-leaflet-directive.
i use togeojson.kml()
transform kml text geojson object , pass leaflet rendering. works great if kml valid. however, if kml malformed, doesn't work great. togeojson
continues render data object, ignoring errors occur.
var pathdata = togeojson.kml(kmldom); var latlng = []; (var feature in pathdata.features) { var paths = pathdata.features[feature].geometry.coordinates; if (paths.length > 1) { (var path in paths) { var prospectivecoords = pathdata.features[feature].geometry.coordinates[path]; var coordinate = l.geojson.coordstolatlng(prospectivecoords); latlng.push(coordinate); } } } try { map.fitbounds(latlng); //instance of l.getmap() angular.extend($scope, { geojson : { data : pathdata, style : { stroke : true, weight : 5 } } }); } catch (e) { $log.error(e.message); $window.alert(e.message); }
in specific issue, if there error in pathdata
variable, triggers exception in latlng line 7:
l.latlng = function (lat, lng, alt) { if (isnan(lat) || isnan(lng)) { throw new error('invalid latlng object: (' + lat + ', ' + lng + ')'); } ...
unfortunately, error not trapped catch block , leaflet continues render map tiles without path overlay, , notice user gets inside console. goal have more visible user notice when invalid file uploaded.
is possible intercept error @ application level?
is code inside callback function or so? geojson data through ajax call? asynchronous callback methods error trapping tricky , not in order expected. previous post interesting: catch statement not catch thrown error
Comments
Post a Comment