Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 8d84a2f

Browse files
dev/codeforces/ Отрефакторил util классы
1 parent 3458a09 commit 8d84a2f

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.cf.cfteam.advicers.codeforces;
22

33
import com.cf.cfteam.exceptions.codeforces.GroupNotFoundException;
4+
import com.cf.cfteam.utils.ErrorResponseBuilder;
45
import org.springframework.http.HttpStatus;
56
import org.springframework.http.ResponseEntity;
67
import org.springframework.web.bind.annotation.ControllerAdvice;
78
import org.springframework.web.bind.annotation.ExceptionHandler;
89

9-
import java.time.LocalDateTime;
10-
import java.util.HashMap;
1110
import java.util.Map;
1211

1312
@ControllerAdvice
@@ -16,20 +15,7 @@ public class CodeforcesExceptionHandler {
1615

1716
@ExceptionHandler(GroupNotFoundException.class)
1817
public ResponseEntity<Object> handleUserNotFoundException(GroupNotFoundException ex) {
19-
return buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(ID, ex.getId()));
18+
return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(ID, ex.getId()));
2019
}
2120

22-
private ResponseEntity<Object> buildErrorResponse(String message, HttpStatus status, Map<String, Object> details) {
23-
Map<String, Object> errorResponse = new HashMap<>();
24-
errorResponse.put("timestamp", LocalDateTime.now());
25-
errorResponse.put("status", status.value());
26-
errorResponse.put("error", status.getReasonPhrase());
27-
errorResponse.put("message", message);
28-
29-
if (details != null) {
30-
errorResponse.put("details", details);
31-
}
32-
33-
return new ResponseEntity<>(errorResponse, status);
34-
}
3521
}

src/main/java/com/cf/cfteam/advicers/security/SecurityExceptionHandler.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,40 @@
44
import com.cf.cfteam.exceptions.security.TokenRevokedException;
55
import com.cf.cfteam.exceptions.security.UserAlreadyRegisterException;
66
import com.cf.cfteam.exceptions.security.UserNotFoundException;
7+
import com.cf.cfteam.utils.ErrorResponseBuilder;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.annotation.ControllerAdvice;
1011
import org.springframework.web.bind.annotation.ExceptionHandler;
1112

12-
import java.time.LocalDateTime;
13-
import java.util.HashMap;
1413
import java.util.Map;
1514

1615
@ControllerAdvice
1716
public class SecurityExceptionHandler {
1817

1918
private static final String LOGIN = "login";
2019
private static final String TOKEN = "token";
21-
private static final String UNEXPECTED_ERROR = "unexpected.error";
2220

2321
@ExceptionHandler(UserAlreadyRegisterException.class)
2422
public ResponseEntity<Object> handleUserAlreadyRegisterException(UserAlreadyRegisterException ex) {
25-
return buildErrorResponse(ex.getMessage(), HttpStatus.CONFLICT, Map.of(LOGIN, ex.getLogin()));
23+
return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.CONFLICT, Map.of(LOGIN,
24+
ex.getLogin()));
2625
}
2726

2827
@ExceptionHandler(UserNotFoundException.class)
2928
public ResponseEntity<Object> handleUserNotFoundException(UserNotFoundException ex) {
30-
return buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(LOGIN, ex.getLogin()));
29+
return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(LOGIN,
30+
ex.getLogin()));
3131
}
3232

3333
@ExceptionHandler(TokenRevokedException.class)
3434
public ResponseEntity<Object> handleTokenRevokedException(TokenRevokedException ex) {
35-
return buildErrorResponse(ex.getMessage(), HttpStatus.UNAUTHORIZED, Map.of(TOKEN, ex.getToken()));
35+
return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.UNAUTHORIZED, Map.of(TOKEN,
36+
ex.getToken()));
3637
}
3738

3839
@ExceptionHandler(InvalidTwoFactorCodeException.class)
3940
public ResponseEntity<Object> handleInvalidTwoFactorCodeException(InvalidTwoFactorCodeException ex) {
40-
return buildErrorResponse(ex.getMessage(), HttpStatus.BAD_REQUEST, null);
41-
}
42-
43-
@ExceptionHandler(Exception.class)
44-
public ResponseEntity<Object> handleGenericException(Exception ex) {
45-
return buildErrorResponse(UNEXPECTED_ERROR, HttpStatus.INTERNAL_SERVER_ERROR, null);
46-
}
47-
48-
private ResponseEntity<Object> buildErrorResponse(String message, HttpStatus status, Map<String, Object> details) {
49-
Map<String, Object> errorResponse = new HashMap<>();
50-
errorResponse.put("timestamp", LocalDateTime.now());
51-
errorResponse.put("status", status.value());
52-
errorResponse.put("error", status.getReasonPhrase());
53-
errorResponse.put("message", message);
54-
55-
if (details != null) {
56-
errorResponse.put("details", details);
57-
}
58-
59-
return new ResponseEntity<>(errorResponse, status);
41+
return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.BAD_REQUEST, null);
6042
}
6143
}

src/main/java/com/cf/cfteam/services/codeforces/GroupService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.cf.cfteam.exceptions.codeforces.GroupNotFoundException;
44
import com.cf.cfteam.exceptions.security.UserNotFoundException;
5-
import com.cf.cfteam.mappers.codeforces.GroupMapper;
5+
import com.cf.cfteam.utils.codeforces.GroupMapper;
66
import com.cf.cfteam.models.entities.codeforces.Group;
77
import com.cf.cfteam.models.entities.security.User;
88
import com.cf.cfteam.repositories.jpa.codeforces.GroupRepository;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.cf.cfteam.utils;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public final class ErrorResponseBuilder {
11+
12+
public static ResponseEntity<Object> buildErrorResponse(String message, HttpStatus status,
13+
Map<String, Object> details) {
14+
Map<String, Object> errorResponse = new HashMap<>();
15+
errorResponse.put("timestamp", LocalDateTime.now());
16+
errorResponse.put("status", status.value());
17+
errorResponse.put("error", status.getReasonPhrase());
18+
errorResponse.put("message", message);
19+
20+
if (details != null) {
21+
errorResponse.put("details", details);
22+
}
23+
24+
return new ResponseEntity<>(errorResponse, status);
25+
}
26+
27+
private ErrorResponseBuilder() {
28+
throw new UnsupportedOperationException("Utility class");
29+
}
30+
}

src/main/java/com/cf/cfteam/mappers/codeforces/GroupMapper.java renamed to src/main/java/com/cf/cfteam/utils/codeforces/GroupMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cf.cfteam.mappers.codeforces;
1+
package com.cf.cfteam.utils.codeforces;
22

33
import com.cf.cfteam.models.entities.codeforces.Group;
44
import com.cf.cfteam.models.entities.security.User;

0 commit comments

Comments
 (0)