Skip to content

Commit 970ce52

Browse files
committed
add custom exception handler
1 parent cfc736e commit 970ce52

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package es.in2.vcverifier.exception;
2+
3+
public class ResourceNotFoundException extends RuntimeException {
4+
5+
public ResourceNotFoundException(String message) {
6+
super(message);
7+
}
8+
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package es.in2.vcverifier.exception.handler;
2+
3+
import es.in2.vcverifier.exception.ResourceNotFoundException;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
9+
@ControllerAdvice
10+
public class CustomExceptionHandler {
11+
12+
@ExceptionHandler(ResourceNotFoundException.class)
13+
public ResponseEntity<String> handleResourceNotFoundException(ResourceNotFoundException ex) {
14+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
15+
}
16+
17+
@ExceptionHandler(Exception.class)
18+
public ResponseEntity<String> handleException(Exception ex) {
19+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred: " + ex.getMessage());
20+
}
21+
}
22+

src/main/java/es/in2/vcverifier/oid4vp/controller/Oid4vpController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package es.in2.vcverifier.oid4vp.controller;
22

33
import es.in2.vcverifier.config.CacheStore;
4+
import es.in2.vcverifier.exception.ResourceNotFoundException;
45
import es.in2.vcverifier.model.AuthorizationRequestJWT;
56
import es.in2.vcverifier.oid4vp.service.AuthorizationResponseProcessorService;
67
import jakarta.servlet.http.HttpServletResponse;
@@ -29,12 +30,9 @@ public ResponseEntity<String> getAuthorizationRequest(@PathVariable String id) {
2930
String jwt = authorizationRequestJWT.authRequest();
3031

3132
if (jwt != null) {
32-
// Si el JWT existe en la caché, lo devolvemos en la respuesta
3333
return ResponseEntity.ok(jwt);
3434
} else {
35-
// Si no se encuentra el JWT, devolvemos un error 404
36-
return ResponseEntity.status(HttpStatus.NOT_FOUND)
37-
.body("JWT not found for id: " + id);
35+
throw new ResourceNotFoundException("JWT not found for id: " + id);
3836
}
3937
}
4038

0 commit comments

Comments
 (0)