@@ -675,13 +675,21 @@ MapboxGeocoder.prototype = {
675675 switch ( requestType ) {
676676 case GEOCODE_REQUEST_TYPE . REVERSE : {
677677 // expected to be coordinates in the form `lat, lon`
678- var coords = search . split ( spacesOrCommaRgx ) . map ( function ( c ) {
678+ var coords = search . trim ( ) . split ( spacesOrCommaRgx ) . map ( function ( c ) {
679679 return parseFloat ( c , 10 ) ;
680680 } )
681681 if ( ! self . options . flipCoordinates ) {
682+ // flip order to `lon, lat`
682683 coords . reverse ( ) ;
683684 }
684685
686+ // validate coordinates in range before proceeding
687+ if ( coords [ 0 ] < - 180 || coords [ 0 ] > 180 ) {
688+ throw new Error ( 'Longitude out of range' ) ;
689+ } else if ( coords [ 1 ] < - 90 || coords [ 1 ] > 90 ) {
690+ throw new Error ( 'Latitude out of range' ) ;
691+ }
692+
685693 config = extend ( config ,
686694 this . options . version === 'v6' ? { longitude : coords [ 0 ] , latitude : coords [ 1 ] } : { query : coords } ,
687695 { limit : 1 }
@@ -725,7 +733,18 @@ MapboxGeocoder.prototype = {
725733 this . _eventEmitter . emit ( 'loading' , { query : searchInput } ) ;
726734
727735 const requestType = this . _requestType ( this . options , searchInput ) ;
728- const config = this . _setupConfig ( requestType , searchInput ) ;
736+
737+ let config ;
738+ // setup request parameters config and pre-validate values
739+ try {
740+ config = this . _setupConfig ( requestType , searchInput ) ;
741+ } catch ( err ) {
742+ this . _hideLoadingIcon ( ) ;
743+ this . _hideAttribution ( ) ;
744+ this . _typeahead . selected = null ;
745+ this . _renderCustomError ( err . message ) ;
746+ return Promise . resolve ( ) ;
747+ }
729748
730749 var request ;
731750 switch ( requestType ) {
@@ -942,6 +961,11 @@ MapboxGeocoder.prototype = {
942961 var errorMessage = "<div class='mapbox-gl-geocoder--error'>There was an error reaching the server</div>"
943962 this . _renderMessage ( errorMessage ) ;
944963 } ,
964+
965+ _renderCustomError : function ( message ) {
966+ var errorMessage = `<div class='mapbox-gl-geocoder--error'>${ message } </div>`
967+ this . _renderMessage ( errorMessage ) ;
968+ } ,
945969
946970 _renderLocationError : function ( ) {
947971 var errorMessage = "<div class='mapbox-gl-geocoder--error'>A location error has occurred</div>"
0 commit comments