maplibre / maplibre-gl-directions

A plugin to show routing directions on a MapLibre GL JS map
https://maplibre.org/maplibre-gl-directions/
MIT License
72 stars 16 forks source link

Improve error handling #196

Closed smellyshovel closed 1 year ago

smellyshovel commented 1 year ago

Here and below the "error" means any server response with code different from "Ok".

The issue with the existing implementation is that sometimes the error is handled and sometimes it's not. Therefore, sometimes it bubbles to the consumer code and sometimes that doesn't happen. That brings inconsistencies to the plugin's behavior.

Proposed solution: don't allow errors to ever bubble up to the consuming code. All the invocations of fetchDirections should be done inside try-catch blocks and, when necessary (currently there's only 1 such occasion), the error should be handled appropriately.

Also, updated the first example to display an error message (if any) using the plugin's events interface. To get an error, try e.g. to build a route from North America to Africa.