@wemap/routers 7.2.0 → 7.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,10 @@
1
+ import { NoRouteFoundError } from '@wemap/geo';
1
2
  import { diffAngle } from '@wemap/maths';
2
3
 
3
4
  import Itinerary from '../model/Itinerary.js';
5
+ import IdfmRemoteRouterTokenError from './idfm/IdfmRemoteRouterTokenError.js';
6
+ import RemoteRouterServerUnreachable from './RemoteRouterServerUnreachable.js';
7
+ import RoutingModeCorrespondanceNotFound from './RoutingModeCorrespondanceNotFound.js';
4
8
 
5
9
 
6
10
  /**
@@ -76,3 +80,10 @@ export function dateWithTimeZone(year, month, day, hour, minute, second, timeZon
76
80
 
77
81
  return date;
78
82
  }
83
+
84
+ export function isRoutingError(e) {
85
+ return e instanceof RemoteRouterServerUnreachable
86
+ || e instanceof RoutingModeCorrespondanceNotFound
87
+ || e instanceof IdfmRemoteRouterTokenError
88
+ || e instanceof NoRouteFoundError;
89
+ }
@@ -103,14 +103,29 @@ class IdfmRemoteRouter extends RemoteRouter {
103
103
 
104
104
  const url = this.getURL(endpointUrl, mode, waypoints);
105
105
 
106
- const res = await fetch(url, {
106
+ const res = await (fetch(url, {
107
107
  method: 'GET',
108
108
  headers: { Authorization: 'Bearer ' + this.token }
109
- });
110
- if (res.status !== 200) {
109
+ }).catch(() => {
110
+ throw new RemoteRouterServerUnreachable(this.rname, url);
111
+ }));
112
+
113
+
114
+ const response = await res.json().catch(() => {
111
115
  throw new RemoteRouterServerUnreachable(this.rname, url);
116
+ });
117
+
118
+ // When IDFM failed to calculate an itinerary (ie. start or end
119
+ // point is far from network), it respond a 404 with an error message
120
+ // or a 200 with an error message
121
+ if (response && response.error) {
122
+ const routerResponse = new RouterResponse();
123
+ routerResponse.routerName = this.rname;
124
+ routerResponse.from = waypoints[0];
125
+ routerResponse.to = waypoints[1];
126
+ routerResponse.error = response.error.message || 'no details.';
127
+ return routerResponse;
112
128
  }
113
- const response = await res.json();
114
129
 
115
130
  return this.createRouterResponseFromJson(response);
116
131
  }
@@ -7,6 +7,7 @@ import Logger from '@wemap/logger';
7
7
  import IOMap from './IOMap.js';
8
8
  import WemapMetaRouterOptions from './WemapMetaRouterOptions.js';
9
9
  import RemoteRouterManager from '../remote/RemoteRouterManager.js';
10
+ import { isRoutingError } from '../remote/RemoteRouterUtils.js';
10
11
  import WemapMetaRemoteRouterOptions from '../remote/wemap-meta/WemapMetaRemoteRouterOptions.js';
11
12
  import WemapRouterOptions from '../wemap/WemapRouterOptions.js';
12
13
  import RouterResponse from '../model/RouterResponse.js';
@@ -63,6 +64,12 @@ class WemapMetaRouter {
63
64
  const start = waypoints[0];
64
65
  const end = waypoints[1];
65
66
 
67
+ const routerResponse = new RouterResponse();
68
+ routerResponse.routerName = this.rname;
69
+ routerResponse.from = start;
70
+ routerResponse.to = end;
71
+
72
+
66
73
  // Avoid cycles on remoteRouters
67
74
  const remoteRouters = options.remoteRouters.filter(
68
75
  ({ name }) => name !== WemapMetaRemoteRouterOptions.rname
@@ -101,7 +108,15 @@ class WemapMetaRouter {
101
108
  * 3 - intersection of this.maps and options.targetMaps is empty
102
109
  */
103
110
  if (!ioMapsToTest.length) {
104
- return RemoteRouterManager.getItinerariesWithFallback(remoteRouters, mode, waypoints);
111
+ try {
112
+ return await RemoteRouterManager.getItinerariesWithFallback(remoteRouters, mode, waypoints);
113
+ } catch (e) {
114
+ if (!isRoutingError(e)) {
115
+ throw e;
116
+ }
117
+ routerResponse.error = e.message;
118
+ return routerResponse;
119
+ }
105
120
  }
106
121
 
107
122
 
@@ -114,10 +129,6 @@ class WemapMetaRouter {
114
129
  *
115
130
  */
116
131
 
117
- const routerResponse = new RouterResponse();
118
- routerResponse.from = start;
119
- routerResponse.to = end;
120
-
121
132
  /** @type {Itinerary} */
122
133
  let ioMapItinerary;
123
134
 
@@ -139,7 +150,7 @@ class WemapMetaRouter {
139
150
  try {
140
151
  ioMapItinerary = mapWithStart.getItineraryInsideMap(start, end, wemapRouterOptions);
141
152
  } catch (e) {
142
- if (!(e instanceof NoRouteFoundError)) {
153
+ if (!isRoutingError(e)) {
143
154
  throw e;
144
155
  }
145
156
  routerResponse.error = `${e.message} - on map ${mapWithStart.name}.`;
@@ -156,19 +167,27 @@ class WemapMetaRouter {
156
167
  // Note: At this step, mapWithEnd is necessarily different from mapWithStart
157
168
  const mapWithEnd = ioMapsToTest.find(map => map.isPointInside(end));
158
169
 
170
+
171
+ /** @type {RouterResponse} */
172
+ let remoteRouterResponse;
173
+
159
174
  /*
160
175
  * Case 2
161
176
  *
162
177
  * If no io map have been found for "start" and "end", therefore use remote router.
163
178
  */
164
179
  if (!mapWithStart && !mapWithEnd) {
165
- return RemoteRouterManager.getItinerariesWithFallback(remoteRouters, mode, waypoints);
180
+ try {
181
+ return await RemoteRouterManager.getItinerariesWithFallback(remoteRouters, mode, waypoints);
182
+ } catch (e) {
183
+ if (!isRoutingError(e)) {
184
+ throw e;
185
+ }
186
+ routerResponse.error = e.message;
187
+ return routerResponse;
188
+ }
166
189
  }
167
190
 
168
- /** @type {RouterResponse} */
169
- let remoteRouterResponse;
170
-
171
-
172
191
  /**
173
192
  * Case 3
174
193
  *
@@ -192,23 +211,21 @@ class WemapMetaRouter {
192
211
 
193
212
  try {
194
213
  ioMapItinerary = mapWithStart.getBestItineraryFromStartToEntryPoints(start, end, wemapRouterOptions);
214
+ remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
215
+ remoteRouters, mode, [ioMapItinerary.to, end]
216
+ );
217
+ if (!remoteRouterResponse.itineraries.length) {
218
+ throw new NoRouteFoundError(ioMapItinerary.to, end, remoteRouterResponse.error);
219
+ }
195
220
  } catch (e) {
196
- if (!(e instanceof NoRouteFoundError)) {
221
+ if (!isRoutingError(e)) {
197
222
  throw e;
198
223
  }
199
- routerResponse.error = `${e.message} - on map ${mapWithStart.name}.`;
200
- return routerResponse;
201
- }
202
- remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
203
- remoteRouters, mode, [ioMapItinerary.to, end]
204
- );
205
-
206
-
207
- if (!remoteRouterResponse.itineraries.length) {
208
- routerResponse.error = `Tried to calculate an itinerary from "start"
209
- to "entrypoints" using wemap router on local map "${mapWithStart.name}" and
210
- an itinerary from "entrypoints" to "end" using remote routers
211
- (${remoteRouters.map(r => r.name).join(', ')}), but failed.`;
224
+ routerResponse.error = 'Tried to calculate an itinerary from "start" '
225
+ + `to "entrypoints" using wemap router on local map "${mapWithStart.name}" and `
226
+ + 'an itinerary from "entrypoints" to "end" using remote routers '
227
+ + `(${remoteRouters.map(r => r.name).join(', ')}), but failed. `
228
+ + `Details: ${e.message}.`;
212
229
  return routerResponse;
213
230
  }
214
231
 
@@ -248,22 +265,21 @@ class WemapMetaRouter {
248
265
  */
249
266
  try {
250
267
  ioMapItinerary = mapWithEnd.getBestItineraryFromEntryPointsToEnd(start, end, wemapRouterOptions);
268
+ remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
269
+ remoteRouters, mode, [start, ioMapItinerary.from]
270
+ );
271
+ if (!remoteRouterResponse.itineraries.length) {
272
+ throw new NoRouteFoundError(start, ioMapItinerary.from, remoteRouterResponse.error);
273
+ }
251
274
  } catch (e) {
252
- if (!(e instanceof NoRouteFoundError)) {
275
+ if (!isRoutingError(e)) {
253
276
  throw e;
254
277
  }
255
- routerResponse.error = `${e.message} - on map ${mapWithEnd.name}.`;
256
- return routerResponse;
257
- }
258
- remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
259
- remoteRouters, mode, [start, ioMapItinerary.from]
260
- );
261
-
262
- if (!remoteRouterResponse.itineraries.length) {
263
- routerResponse.error = `Tried to calculate an itinerary from "start" to "entrypoints"
264
- using remote routers (${remoteRouters.map(r => r.name).join(', ')}) and an
265
- itinerary from "entrypoints" to "end" using wemap router on local map
266
- "${mapWithEnd.name}", but failed.`;
278
+ routerResponse.error = 'Tried to calculate an itinerary from "start" to "entrypoints" '
279
+ + `using remote routers (${remoteRouters.map(r => r.name).join(', ')}) and an `
280
+ + 'itinerary from "entrypoints" to "end" using wemap router on local map '
281
+ + `"${mapWithEnd.name}", but failed. `
282
+ + `Details: ${e.message}.`;
267
283
  return routerResponse;
268
284
  }
269
285
 
@@ -306,32 +322,23 @@ class WemapMetaRouter {
306
322
  let ioMapItinerary1, ioMapItinerary2;
307
323
  try {
308
324
  ioMapItinerary1 = mapWithStart.getBestItineraryFromStartToEntryPoints(start, end, wemapRouterOptions);
309
- } catch (e) {
310
- if (!(e instanceof NoRouteFoundError)) {
311
- throw e;
312
- }
313
- routerResponse.error = `${e.message} - on map ${mapWithStart.name}.`;
314
- return routerResponse;
315
- }
316
- try {
317
325
  ioMapItinerary2 = mapWithEnd.getBestItineraryFromEntryPointsToEnd(start, end, wemapRouterOptions);
326
+ remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
327
+ remoteRouters, mode, [ioMapItinerary1.to, ioMapItinerary2.from]
328
+ );
329
+ if (!remoteRouterResponse.itineraries.length) {
330
+ throw new NoRouteFoundError(ioMapItinerary1.to, ioMapItinerary2.from, remoteRouterResponse.error);
331
+ }
318
332
  } catch (e) {
319
- if (!(e instanceof NoRouteFoundError)) {
333
+ if (!isRoutingError(e)) {
320
334
  throw e;
321
335
  }
322
- routerResponse.error = `${e.message} - on map ${mapWithEnd.name}.`;
323
- return routerResponse;
324
- }
325
- remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
326
- remoteRouters, mode, [ioMapItinerary1.to, ioMapItinerary2.from]
327
- );
328
-
329
- if (!remoteRouterResponse.itineraries.length) {
330
- routerResponse.error = `Tried to calculate an itinerary from "start" to "entrypoints1"
331
- using wemap router on local map "${mapWithStart.name}", an itinerary from
332
- "entrypoints1" to "entrypoints2" using remote routers
333
- (${remoteRouters.map(r => r.name).join(', ')}) and an itinerary from "entrypoints2"
334
- to "end" using wemap router on local map "${mapWithEnd.name}", but failed.`;
336
+ routerResponse.error = 'Tried to calculate an itinerary from "start" to "entrypoints1" '
337
+ + `using wemap router on local map "${mapWithStart.name}", an itinerary from `
338
+ + '"entrypoints1" to "entrypoints2" using remote routers '
339
+ + `(${remoteRouters.map(r => r.name).join(', ')}) and an itinerary from "entrypoints2" `
340
+ + `to "end" using wemap router on local map "${mapWithEnd.name}", but failed. `
341
+ + `Details: ${e.message}.`;
335
342
  return routerResponse;
336
343
  }
337
344