Skip to content

Commit afe2855

Browse files
committed
make handing over of hints in JSON to error message object possible; renamed CannotFindPointException to more common PointNotFoundException
1 parent b50ac75 commit afe2855

File tree

11 files changed

+162
-21
lines changed

11 files changed

+162
-21
lines changed

core/src/main/java/com/graphhopper/routing/template/RoundTripRoutingTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.graphhopper.util.Parameters.Algorithms.RoundTrip;
3636
import com.graphhopper.util.PathMerger;
3737
import com.graphhopper.util.Translation;
38-
import com.graphhopper.util.exceptions.CannotFindPointException;
38+
import com.graphhopper.util.exceptions.PointNotFoundException;
3939
import com.graphhopper.util.shapes.GHPoint;
4040

4141
import java.util.ArrayList;
@@ -80,7 +80,7 @@ public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
8080
EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
8181
QueryResult startQR = locationIndex.findClosest(start.lat, start.lon, edgeFilter);
8282
if (!startQR.isValid())
83-
throw new CannotFindPointException("Cannot find point 0: " + start, 0);
83+
throw new PointNotFoundException("Cannot find point 0: " + start, 0);
8484

8585
queryResults.add(startQR);
8686

core/src/main/java/com/graphhopper/routing/template/ViaRoutingTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.graphhopper.util.PathMerger;
3232
import com.graphhopper.util.StopWatch;
3333
import com.graphhopper.util.Translation;
34-
import com.graphhopper.util.exceptions.CannotFindPointException;
34+
import com.graphhopper.util.exceptions.PointNotFoundException;
3535
import com.graphhopper.util.shapes.GHPoint;
3636

3737
import java.util.ArrayList;
@@ -67,7 +67,7 @@ public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
6767
GHPoint point = points.get(placeIndex);
6868
QueryResult res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
6969
if (!res.isValid())
70-
ghResponse.addError(new CannotFindPointException("Cannot find point " + placeIndex + ": " + point, placeIndex));
70+
ghResponse.addError(new PointNotFoundException("Cannot find point " + placeIndex + ": " + point, placeIndex));
7171

7272
queryResults.add(res);
7373
}

core/src/main/java/com/graphhopper/util/PathMerger.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import com.graphhopper.PathWrapper;
2121
import com.graphhopper.routing.Path;
22+
import com.graphhopper.util.exceptions.ConnectionNotFoundException;
2223

2324
import java.util.ArrayList;
25+
import java.util.Collections;
2426
import java.util.List;
2527

2628
/**
@@ -126,7 +128,7 @@ public void doWork(PathWrapper altRsp, List<Path> paths, Translation tr) {
126128
altRsp.setInstructions(fullInstructions);
127129

128130
if (!allFound)
129-
altRsp.addError(new RuntimeException("Connection between locations not found"));
131+
altRsp.addError(new ConnectionNotFoundException("Connection between locations not found", Collections.<String, Object>emptyMap()));
130132

131133
altRsp.setDescription(description).
132134
setPoints(fullPoints).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to GraphHopper GmbH under one or more contributor
3+
* license agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* GraphHopper GmbH licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except in
8+
* compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.graphhopper.util.exceptions;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* If a route cannot be found due to disconnected graphs.
24+
*
25+
* @author Peter Karich
26+
*/
27+
public class ConnectionNotFoundException extends DetailedIllegalArgumentException {
28+
public ConnectionNotFoundException(String var1, Map<String, Object> details) {
29+
super(var1, details);
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2016 peterk.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.graphhopper.util.exceptions;
17+
18+
import java.util.Map;
19+
20+
/**
21+
* @author Peter Karich
22+
*/
23+
public class DetailedIllegalArgumentException extends IllegalArgumentException implements GHException {
24+
25+
private final Map<String, Object> details;
26+
27+
public DetailedIllegalArgumentException(String var1, Map<String, Object> details) {
28+
super(var1);
29+
this.details = details;
30+
}
31+
32+
@Override
33+
public Map<String, Object> getDetails() {
34+
return details;
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2016 peterk.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.graphhopper.util.exceptions;
17+
18+
import java.util.Map;
19+
20+
/**
21+
* @author Peter Karich
22+
*/
23+
public class DetailedRuntimeException extends RuntimeException implements GHException {
24+
25+
private final Map<String, Object> details;
26+
27+
public DetailedRuntimeException(String var1, Map<String, Object> details) {
28+
super(var1);
29+
this.details = details;
30+
}
31+
32+
@Override
33+
public Map<String, Object> getDetails() {
34+
return details;
35+
}
36+
}

core/src/main/java/com/graphhopper/util/exceptions/GHException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
* @author Robin Boldt
2626
*/
2727
public interface GHException {
28-
public abstract Map<String, String> getDetails();
28+
public abstract Map<String, Object> getDetails();
2929
}

core/src/main/java/com/graphhopper/util/exceptions/CannotFindPointException.java renamed to core/src/main/java/com/graphhopper/util/exceptions/PointNotFoundException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
*
2727
* @author Robin Boldt
2828
*/
29-
public class CannotFindPointException extends IllegalArgumentException implements GHException {
29+
public class PointNotFoundException extends IllegalArgumentException implements GHException {
3030

3131
protected final int pointIndex;
3232

33-
public CannotFindPointException(String var1, int pointIndex) {
33+
public PointNotFoundException(String var1, int pointIndex) {
3434
super(var1);
3535
this.pointIndex = pointIndex;
3636
}
@@ -40,7 +40,7 @@ public int getPointIndex() {
4040
}
4141

4242
@Override
43-
public Map<String, String> getDetails() {
44-
return Collections.singletonMap("point_index", String.valueOf(pointIndex));
43+
public Map<String, Object> getDetails() {
44+
return Collections.<String, Object>singletonMap("point_index", pointIndex);
4545
}
4646
}

core/src/main/java/com/graphhopper/util/exceptions/PointOutOfBoundsException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @author Robin Boldt
77
*/
8-
public class PointOutOfBoundsException extends CannotFindPointException {
8+
public class PointOutOfBoundsException extends PointNotFoundException {
99
public PointOutOfBoundsException(String var1, int pointIndex) {
1010
super(var1, pointIndex);
1111
}

web/src/main/java/com/graphhopper/http/GraphHopperWeb.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@
2222
import com.graphhopper.GraphHopperAPI;
2323
import com.graphhopper.PathWrapper;
2424
import com.graphhopper.util.*;
25-
import com.graphhopper.util.exceptions.CannotFindPointException;
25+
import com.graphhopper.util.exceptions.ConnectionNotFoundException;
26+
import com.graphhopper.util.exceptions.DetailedIllegalArgumentException;
27+
import com.graphhopper.util.exceptions.DetailedRuntimeException;
28+
import com.graphhopper.util.exceptions.PointNotFoundException;
2629
import com.graphhopper.util.exceptions.PointOutOfBoundsException;
2730
import com.graphhopper.util.shapes.GHPoint;
2831
import org.json.JSONArray;
2932
import org.json.JSONObject;
3033

3134
import java.util.ArrayList;
35+
import java.util.HashMap;
3236
import java.util.HashSet;
3337
import java.util.List;
38+
import java.util.Map;
3439
import java.util.Map.Entry;
3540
import java.util.Set;
41+
import org.json.JSONException;
3642

3743
/**
3844
* Main wrapper of the GraphHopper Directions API for a simple and efficient usage.
@@ -156,8 +162,36 @@ public static PathWrapper createPathWrapper(JSONObject path,
156162
return pathWrapper;
157163
}
158164

165+
// Credits to: http://stackoverflow.com/a/24012023/194609
166+
private static Map<String, Object> toMap(JSONObject object) throws JSONException {
167+
Map<String, Object> map = new HashMap<>(object.keySet().size());
168+
for (String key : object.keySet()) {
169+
Object value = object.get(key);
170+
if (value instanceof JSONArray) {
171+
value = toList((JSONArray) value);
172+
} else if (value instanceof JSONObject) {
173+
value = toMap((JSONObject) value);
174+
}
175+
map.put(key, value);
176+
}
177+
return map;
178+
}
179+
180+
private static List<Object> toList(JSONArray array) throws JSONException {
181+
List<Object> list = new ArrayList<>();
182+
for (Object value : array) {
183+
if (value instanceof JSONArray) {
184+
value = toList((JSONArray) value);
185+
} else if (value instanceof JSONObject) {
186+
value = toMap((JSONObject) value);
187+
}
188+
list.add(value);
189+
}
190+
return list;
191+
}
192+
159193
public static List<Throwable> readErrors(JSONObject json) {
160-
List<Throwable> errors = new ArrayList<Throwable>();
194+
List<Throwable> errors = new ArrayList<>();
161195
JSONArray errorJson;
162196

163197
if (json.has("message")) {
@@ -184,19 +218,21 @@ public static List<Throwable> readErrors(JSONObject json) {
184218
else if (exClass.equals(IllegalStateException.class.getName()))
185219
errors.add(new IllegalStateException(exMessage));
186220
else if (exClass.equals(RuntimeException.class.getName()))
187-
errors.add(new RuntimeException(exMessage));
221+
errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
188222
else if (exClass.equals(IllegalArgumentException.class.getName()))
189-
errors.add(new IllegalArgumentException(exMessage));
190-
else if (exClass.equals(CannotFindPointException.class.getName())) {
223+
errors.add(new DetailedIllegalArgumentException(exMessage, toMap(error)));
224+
else if (exClass.equals(ConnectionNotFoundException.class.getName())) {
225+
errors.add(new ConnectionNotFoundException(exMessage, toMap(error)));
226+
} else if (exClass.equals(PointNotFoundException.class.getName())) {
191227
int pointIndex = error.getInt("point_index");
192-
errors.add(new CannotFindPointException(exMessage, pointIndex));
228+
errors.add(new PointNotFoundException(exMessage, pointIndex));
193229
} else if (exClass.equals(PointOutOfBoundsException.class.getName())) {
194230
int pointIndex = error.getInt("point_index");
195231
errors.add(new PointOutOfBoundsException(exMessage, pointIndex));
196232
} else if (exClass.isEmpty())
197-
errors.add(new RuntimeException(exMessage));
233+
errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
198234
else
199-
errors.add(new RuntimeException(exClass + " " + exMessage));
235+
errors.add(new DetailedRuntimeException(exClass + " " + exMessage, toMap(error)));
200236
}
201237

202238
if (json.has("message") && errors.isEmpty())

web/src/main/java/com/graphhopper/http/SimpleRouteSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public Map<String, Object> toJSON(GHResponse rsp,
5252

5353
if (rsp.hasErrors()) {
5454
json.put("message", getMessage(rsp.getErrors().get(0)));
55-
List<Map<String, String>> errorHintList = new ArrayList<Map<String, String>>();
55+
List<Map<String, Object>> errorHintList = new ArrayList<>();
5656
for (Throwable t : rsp.getErrors()) {
57-
Map<String, String> map = new HashMap<String, String>();
57+
Map<String, Object> map = new HashMap<>();
5858
map.put("message", getMessage(t));
5959
map.put("details", t.getClass().getName());
6060
if (t instanceof GHException) {

0 commit comments

Comments
 (0)