Skip to content

Commit c20548e

Browse files
authored
Merge pull request #10 from SimpliRoute/better-handling-osrmproxy-httpsstatuses
Improve handling for osrm proxy errors
2 parents 7a0ec35 + b94b8a2 commit c20548e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.osrm.client;
2+
3+
public class DistanceMatrixResponseException extends RuntimeException {
4+
public DistanceMatrixResponseException(String message) {
5+
super(message);
6+
}
7+
}

src/main/java/com/osrm/client/OSRMClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, doubl
6161

6262
try {
6363
Response response = client.newCall(request).execute();
64+
65+
if (!response.isSuccessful()) {
66+
UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse.fromJSON(response.body().string());
67+
throw new DistanceMatrixResponseException("OSRM Error: " + unsuccessfulResponse.getMessage());
68+
}
69+
6470
osrmDistanceResponse = OSRMDistanceResponse.fromJSON(response.body().string());
6571
}
6672
catch(Exception e) {
6773
System.out.print(e.getMessage());
68-
throw new OptimizationDistanceMatrixException("Error while connecting to osrm server");
74+
throw new OptimizationDistanceMatrixException("Error while connecting to OSRM Server");
6975
}
7076

7177
return osrmDistanceResponse;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.osrm.client;
2+
3+
import com.google.gson.Gson;
4+
5+
public class UnsuccessfulResponse {
6+
private String message;
7+
private String statusCode;
8+
private String error;
9+
10+
public UnsuccessfulResponse(String message, String statusCode, String error) {
11+
this.message = message;
12+
this.statusCode = statusCode;
13+
this.error = error;
14+
}
15+
16+
public String getMessage() {
17+
return message;
18+
}
19+
20+
public String getStatusCode() {
21+
return statusCode;
22+
}
23+
24+
public String getError() {
25+
return error;
26+
}
27+
28+
public static UnsuccessfulResponse fromJSON (String json) {
29+
return new Gson().fromJson(json, UnsuccessfulResponse.class);
30+
}
31+
}

0 commit comments

Comments
 (0)