Skip to content

Commit 1420f46

Browse files
committed
feat: add specific handler for CameraDeviceErrorException
1 parent d17febb commit 1420f46

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/main/java/com/kcn/hikvisionmanager/client/HttpDownloadClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.kcn.hikvisionmanager.config.CameraConfig;
44
import com.kcn.hikvisionmanager.events.model.CameraRestartInitiatedEvent;
5+
import com.kcn.hikvisionmanager.exception.CameraDeviceErrorException;
56
import com.kcn.hikvisionmanager.exception.CameraRequestException;
67
import com.kcn.hikvisionmanager.exception.CameraUnauthorizedException;
78
import com.kcn.hikvisionmanager.service.ProgressListener;
@@ -117,13 +118,24 @@ public void executeDownloadStream(
117118
try {
118119
httpClient.execute(httpGet, response -> {
119120
int statusCode = response.getCode();
121+
120122
log.debug("Download response status: {}", statusCode);
121123

122124
// Handle authentication errors
123125
if (statusCode == 401 || statusCode == 403) {
124126
throw new CameraUnauthorizedException(
125127
"Unauthorized download request to camera " + cameraConfig.getIp());
126128
}
129+
// Handle Hikvision device error
130+
if (statusCode == 500) {
131+
String errorBody = EntityUtils.toString(response.getEntity());
132+
if(errorBody.contains("<subStatusCode>deviceError</subStatusCode>")) {
133+
throw new CameraDeviceErrorException(
134+
"Camera returned DEVICE ERROR. The device is overloaded or internal buffer failed."
135+
);
136+
}
137+
throw new CameraRequestException("500 Internal Error: " + errorBody);
138+
}
127139

128140
// Handle HTTP errors
129141
if (statusCode >= 400) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.kcn.hikvisionmanager.exception;
2+
3+
public class CameraDeviceErrorException extends CameraRequestException {
4+
public CameraDeviceErrorException(String message) {
5+
super(message);
6+
}
7+
}

src/main/java/com/kcn/hikvisionmanager/exception/GlobalExceptionHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ public ResponseEntity<ApiErrorResponse> handleUnauthorized(CameraUnauthorizedExc
8181
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
8282
}
8383

84+
@ExceptionHandler(CameraDeviceErrorException.class)
85+
public ResponseEntity<ApiErrorResponse> handleCameraDeviceError(
86+
CameraDeviceErrorException ex, HttpServletRequest request) {
87+
88+
// Using 503 Service Unavailable because the device is likely overloaded/busy
89+
ApiErrorResponse response = ApiErrorResponse.of(
90+
HttpStatus.SERVICE_UNAVAILABLE,
91+
"Camera device error: " + ex.getMessage(),
92+
request.getRequestURI()
93+
);
94+
95+
log.warn("🔥 Camera internal device error on {}: {}", request.getRequestURI(), ex.getMessage());
96+
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
97+
}
98+
8499
@ExceptionHandler(CameraOfflineException.class)
85100
public ResponseEntity<ApiErrorResponse> handleCameraOfflineException(
86101
CameraOfflineException ex, HttpServletRequest request) {

0 commit comments

Comments
 (0)