handle routing exceptions in API and c3nav.js
This commit is contained in:
parent
e51f790ed5
commit
f17b593f77
2 changed files with 23 additions and 3 deletions
|
@ -1,9 +1,11 @@
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework.decorators import list_route
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.viewsets import ViewSet
|
||||
|
||||
from c3nav.mapdata.models.access import AccessPermission
|
||||
from c3nav.mapdata.utils.locations import visible_locations_for_request
|
||||
from c3nav.routing.exceptions import LocationUnreachable, NoRouteFound, NotYetRoutable
|
||||
from c3nav.routing.forms import RouteForm
|
||||
from c3nav.routing.router import Router
|
||||
|
||||
|
@ -19,9 +21,22 @@ class RoutingViewSet(ViewSet):
|
|||
'errors': form.errors,
|
||||
})
|
||||
|
||||
try:
|
||||
route = Router.load().get_route(origin=form.cleaned_data['origin'],
|
||||
destination=form.cleaned_data['destination'],
|
||||
permissions=AccessPermission.get_for_request(request))
|
||||
except NotYetRoutable:
|
||||
return Response({
|
||||
'error': _('Not yet routable, try again shortly.'),
|
||||
})
|
||||
except LocationUnreachable:
|
||||
return Response({
|
||||
'error': _('Unreachable location.'),
|
||||
})
|
||||
except NoRouteFound:
|
||||
return Response({
|
||||
'error': _('No route found.'),
|
||||
})
|
||||
|
||||
return Response({
|
||||
'request': {
|
||||
|
|
|
@ -238,6 +238,11 @@ c3nav = {
|
|||
},
|
||||
_route_loaded: function(data, nofly) {
|
||||
var $route = $('#route-summary');
|
||||
if (data.error && $route.is('.loading')) {
|
||||
$route.find('span').text(data.error);
|
||||
$route.removeClass('loading');
|
||||
return;
|
||||
}
|
||||
if ($route.attr('data-origin') !== String(data.request.origin) || $route.attr('data-destination') !== String(data.request.destination)) {
|
||||
// loaded too late, information no longer needed
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue