Skip to content

Commit 9e41892

Browse files
committed
feat: return custom exceptions
1 parent 40c6b0a commit 9e41892

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Add the Maven Dependency to your `pom.xml`
1616
<dependency>
1717
<groupId>be.nicholasmeyers</groupId>
1818
<artifactId>skoda-api-client</artifactId>
19-
<version>1.1.0</version>
19+
<version>1.2.0</version>
2020
</dependency>
2121
```
2222

src/main/java/be/nicholasmeyers/skoda/api/client/CarService.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public CarService(String email, String password) {
6161
*
6262
* @param vin The VIN of the car.
6363
* @return A {@link CarCoolingInfo} object containing the cooling information of the car.
64-
* @throws RuntimeException If an error occurs while retrieving the cooling status.
64+
* @throws CarServiceException If an error occurs while retrieving the cooling status.
6565
*/
6666
public CarCoolingInfo getCooling(String vin) {
6767
try {
@@ -71,7 +71,7 @@ public CarCoolingInfo getCooling(String vin) {
7171
cooling.getClimatisationDuration(), cooling.getStartMode(), cooling.getHeaterMode(), mapToCarReport(cooling.getReport()),
7272
cooling.getTimers().stream().map(this::mapToCarCoolingTimer).toList());
7373
} catch (ApiException e) {
74-
throw new RuntimeException(e);
74+
throw new CarServiceException("Failed to get cooling information", e.getMessage());
7575
}
7676
}
7777

@@ -81,11 +81,11 @@ public CarCoolingInfo getCooling(String vin) {
8181
* @param vin The VIN of the car.
8282
* @param duration The duration to flash the lights (in seconds).
8383
* @return The status of the request.
84-
* @throws RuntimeException If the duration exceeds the maximum allowed or if an error occurs.
84+
* @throws CarServiceException If the duration exceeds the maximum allowed or if an error occurs.
8585
*/
8686
public String flash(String vin, Integer duration) {
8787
if (duration > 30) {
88-
throw new RuntimeException("maximum duration is 30");
88+
throw new CarServiceException("Failed to flash lights", "duration limit exceeded, max 30 minutes");
8989
}
9090
CarLocation location = getLocation(vin);
9191
FlashWebRequestResource flashWebRequestResource = new FlashWebRequestResource();
@@ -96,7 +96,7 @@ public String flash(String vin, Integer duration) {
9696
try {
9797
return flashApi.flash(vin, flashWebRequestResource).getStatus();
9898
} catch (ApiException e) {
99-
throw new RuntimeException(e);
99+
throw new CarServiceException("Failed to flash lights", e.getMessage());
100100
}
101101
}
102102

@@ -106,11 +106,11 @@ public String flash(String vin, Integer duration) {
106106
* @param vin The VIN of the car.
107107
* @param duration The duration to honk the horn (in seconds).
108108
* @return The status of the request.
109-
* @throws RuntimeException If the duration exceeds the maximum allowed or if an error occurs.
109+
* @throws CarServiceException If the duration exceeds the maximum allowed or if an error occurs.
110110
*/
111111
public String honk(String vin, Integer duration) {
112112
if (duration > 30) {
113-
throw new RuntimeException("maximum duration is 30");
113+
throw new CarServiceException("Failed to honk horn", "duration limit exceeded, max 30 minutes");
114114
}
115115
CarLocation location = getLocation(vin);
116116
HonkWebRequestResource honkWebRequestResource = new HonkWebRequestResource();
@@ -121,7 +121,7 @@ public String honk(String vin, Integer duration) {
121121
try {
122122
return honkApi.honk(vin, honkWebRequestResource).getStatus();
123123
} catch (ApiException e) {
124-
throw new RuntimeException(e);
124+
throw new CarServiceException("Failed to honk horn", e.getMessage());
125125
}
126126
}
127127

@@ -131,14 +131,14 @@ public String honk(String vin, Integer duration) {
131131
* @param vin The VIN of the car.
132132
* @param id The id of the request.
133133
* @return The status of the request.
134-
* @throws RuntimeException If an error occurs while retrieving the request status.
134+
* @throws CarServiceException If an error occurs while retrieving the request status.
135135
*/
136136
public String getRequest(String vin, String id) {
137137
try {
138138
RequestWebResponseResource request = requestApi.getRequest(vin, id);
139139
return request.getStatus();
140140
} catch (ApiException e) {
141-
throw new RuntimeException(e);
141+
throw new CarServiceException("Failed to get request", e.getMessage());
142142
}
143143
}
144144

@@ -147,14 +147,14 @@ public String getRequest(String vin, String id) {
147147
*
148148
* @param vin The VIN of the car.
149149
* @return A {@link CarStatus} object containing the status information of the car.
150-
* @throws RuntimeException If an error occurs while retrieving the car status.
150+
* @throws CarServiceException If an error occurs while retrieving the car status.
151151
*/
152152
public CarStatus getStatus(String vin) {
153153
try {
154154
StatusWebResponseResource status = statusApi.getStatus(vin);
155155
return new CarStatus(status.getVin(), status.getData().stream().map(this::mapToCarData).toList());
156156
} catch (ApiException e) {
157-
throw new RuntimeException(e);
157+
throw new CarServiceException("Failed to get status", e.getMessage());
158158
}
159159
}
160160

@@ -165,11 +165,11 @@ public CarStatus getStatus(String vin) {
165165
* @param pin The pin used to authenticate the ventilator action.
166166
* @param duration The duration to run the ventilator (in seconds).
167167
* @return The ventilator status ID.
168-
* @throws RuntimeException If the duration exceeds the maximum allowed or if an error occurs.
168+
* @throws CarServiceException If the duration exceeds the maximum allowed or if an error occurs.
169169
*/
170170
public String startVentilator(String vin, String pin, Integer duration) {
171171
if (duration > 30) {
172-
throw new RuntimeException("maximum duration is 30");
172+
throw new CarServiceException("Failed to start ventilator", "duration limit exceeded, max 30 minutes");
173173
}
174174

175175
VentilatorWebRequestResource ventilatorWebRequestResource = new VentilatorWebRequestResource();
@@ -180,7 +180,7 @@ public String startVentilator(String vin, String pin, Integer duration) {
180180
VentilatorWebResponseResource ventilatorStatus = ventilatorApi.startVentilator(vin, ventilatorWebRequestResource);
181181
return ventilatorStatus.getId();
182182
} catch (ApiException e) {
183-
throw new RuntimeException(e);
183+
throw new CarServiceException("Failed to start ventilator", e.getMessage());
184184
}
185185
}
186186

@@ -190,7 +190,7 @@ public String startVentilator(String vin, String pin, Integer duration) {
190190
* @param vin The VIN of the car.
191191
* @param pin The pin used to authenticate the ventilator action.
192192
* @return The ventilator status ID.
193-
* @throws RuntimeException If an error occurs while stopping the ventilator.
193+
* @throws CarServiceException If an error occurs while stopping the ventilator.
194194
*/
195195
public String stopVentilator(String vin, String pin) {
196196
VentilatorWebRequestResource ventilatorWebRequestResource = new VentilatorWebRequestResource();
@@ -201,7 +201,7 @@ public String stopVentilator(String vin, String pin) {
201201
VentilatorWebResponseResource ventilatorStatus = ventilatorApi.stopVentilator(vin, ventilatorWebRequestResource);
202202
return ventilatorStatus.getId();
203203
} catch (ApiException e) {
204-
throw new RuntimeException(e);
204+
throw new CarServiceException("Failed to stop ventilator", e.getMessage());
205205
}
206206
}
207207

@@ -210,14 +210,14 @@ public String stopVentilator(String vin, String pin) {
210210
*
211211
* @param vin The VIN of the car.
212212
* @return A {@link CarLocation} object representing the car's location.
213-
* @throws RuntimeException If an error occurs while retrieving the car location.
213+
* @throws CarServiceException If an error occurs while retrieving the car location.
214214
*/
215215
public CarLocation getLocation(String vin) {
216216
try {
217217
LocationWebResponseResource location = locationApi.getLocation(vin);
218218
return new CarLocation(location.getLatitude(), location.getLongitude());
219219
} catch (ApiException e) {
220-
throw new RuntimeException(e);
220+
throw new CarServiceException("Failed to get car location", e.getMessage());
221221
}
222222
}
223223

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package be.nicholasmeyers.skoda.api.client;
2+
3+
/**
4+
* Exception thrown to indicate a failure during car service operations.
5+
* <p>
6+
* This exception provides both a user-friendly message and the original message
7+
* from a lower-level service or component, allowing for more detailed diagnostics.
8+
* </p>
9+
*/
10+
public class CarServiceException extends RuntimeException {
11+
12+
private final String originalMessage;
13+
14+
/**
15+
* Constructs a new {@code CarServiceException} with the specified detail message
16+
* and the original message from the source of the error.
17+
*
18+
* @param message a user-friendly message describing the exception
19+
* @param originalMessage the original message from the underlying system or service
20+
*/
21+
public CarServiceException(String message, String originalMessage) {
22+
super(message);
23+
this.originalMessage = originalMessage;
24+
}
25+
26+
/**
27+
* Returns the original message from the underlying system or service that caused the exception.
28+
*
29+
* @return the original error message
30+
*/
31+
public String getOriginalMessage() {
32+
return originalMessage;
33+
}
34+
}

0 commit comments

Comments
 (0)