Skip to content

Commit 0a2d056

Browse files
committed
refactor: global exception handle
1 parent 641f75c commit 0a2d056

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/domain/controllers/ExceptionHandlerController.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,39 @@
66
import org.springframework.http.ResponseEntity;
77
import org.springframework.web.bind.annotation.ControllerAdvice;
88
import org.springframework.web.bind.annotation.ExceptionHandler;
9-
10-
import java.util.Arrays;
9+
//import java.util.Arrays;
1110

1211
@ControllerAdvice
1312
public class ExceptionHandlerController extends BaseController {
13+
@ExceptionHandler(BadRequestException.class)
14+
public ResponseEntity<Object> handleBadRequestException(BadRequestException e) {
15+
ResponseDTO response = this.responseMapper.toDTO(
16+
400,
17+
e.getMessage(),
18+
null
19+
);
20+
21+
return ResponseEntity.status(400).body(response);
22+
}
23+
24+
@ExceptionHandler(UnauthorizedException.class)
25+
public ResponseEntity<Object> handleUnauthorizedException(UnauthorizedException e) {
26+
27+
ResponseDTO response = this.responseMapper.toDTO(
28+
403,
29+
e.getMessage(),
30+
null
31+
);
32+
33+
return ResponseEntity.status(403).body(response);
34+
}
35+
1436
@ExceptionHandler(Exception.class)
1537
public ResponseEntity<Object> handleException(Exception e) {
16-
if (e instanceof BadRequestException) {
17-
ResponseDTO response = this.responseMapper.toDTO(
18-
400,
19-
e.getMessage(),
20-
null
21-
);
22-
23-
return ResponseEntity.status(400).body(response);
24-
}
25-
26-
if (e instanceof UnauthorizedException) {
27-
ResponseDTO response = this.responseMapper.toDTO(
28-
403,
29-
e.getMessage(),
30-
null
31-
);
32-
33-
return ResponseEntity.status(403).body(response);
34-
}
35-
3638
ResponseDTO response = this.responseMapper.toDTO(
3739
500,
38-
Arrays.toString(e.getStackTrace()),
40+
// Arrays.toString(e.getStackTrace()),
41+
e.getLocalizedMessage(),
3942
null
4043
);
4144

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/users/controllers/CreateUserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class CreateUserController extends BaseController {
3939
@ApiResponse(responseCode = "400", description = "Erro da requisição (email e ou username não disponivel)"),
4040
@ApiResponse(responseCode = "500", description = "Erro Interno"),
4141
})
42-
public ResponseEntity<Object> handle(@RequestPart("user") @Valid CreateUserDTO user, @Nullable @RequestParam("file")MultipartFile file) throws Exception {
42+
public ResponseEntity<Object> handle(@RequestPart("user") @Valid CreateUserDTO user, @Nullable @RequestPart("file")MultipartFile file) throws Exception {
4343
UserDTO createdUser = this.createUserService.execute(user, file);
4444

4545
ResponseDTO response = this.responseMapper.toDTO(

0 commit comments

Comments
 (0)