Skip to content

Commit 4d9a0ca

Browse files
refactor
1 parent ebfa248 commit 4d9a0ca

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/main/java/org/gridsuite/useradmin/server/RestResponseEntityExceptionHandler.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
*/
1919
@ControllerAdvice
2020
public class RestResponseEntityExceptionHandler {
21-
2221
@ExceptionHandler(value = { UserAdminException.class })
2322
protected ResponseEntity<Object> handleException(RuntimeException exception) {
24-
if (exception instanceof UserAdminException) {
25-
UserAdminException userAdminException = (UserAdminException) exception;
26-
if (userAdminException.getType().equals(FORBIDDEN)) {
27-
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(userAdminException.getType());
28-
}
23+
if (exception instanceof UserAdminException userAdminException) {
24+
return switch (userAdminException.getType()) {
25+
case FORBIDDEN -> ResponseEntity.status(HttpStatus.FORBIDDEN).body(userAdminException.getType());
26+
};
27+
} else {
28+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
2929
}
30-
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
3130
}
3231
}

src/main/java/org/gridsuite/useradmin/server/UserAdminException.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
*/
77
package org.gridsuite.useradmin.server;
88

9+
import lombok.Data;
10+
import lombok.EqualsAndHashCode;
11+
912
import java.util.Objects;
1013

1114
/**
1215
* @author Etienne Homer <etienne.homer at rte-france.com>
1316
*/
17+
@EqualsAndHashCode(callSuper = false)
18+
@Data
1419
public class UserAdminException extends RuntimeException {
1520
public enum Type {
1621
FORBIDDEN
@@ -22,9 +27,4 @@ public UserAdminException(Type type) {
2227
super(Objects.requireNonNull(type.name()));
2328
this.type = type;
2429
}
25-
26-
Type getType() {
27-
return type;
28-
}
29-
3030
}

0 commit comments

Comments
 (0)