-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathErrorHandler.java
More file actions
125 lines (102 loc) · 5.38 KB
/
ErrorHandler.java
File metadata and controls
125 lines (102 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.gam.api.common;
import com.gam.api.common.exception.AuthException;
import com.gam.api.common.exception.AwsException;
import com.gam.api.common.exception.BlockException;
import com.gam.api.common.exception.ReportException;
import com.gam.api.common.exception.ScrapException;
import com.gam.api.common.exception.WorkException;
import com.gam.api.common.message.ExceptionMessage;
import com.gam.api.config.SlackConfig;
import com.slack.api.Slack;
import java.time.LocalDateTime;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.persistence.EntityNotFoundException;
@RestControllerAdvice
@RequiredArgsConstructor
public class ErrorHandler {
private final Slack slack;
private final SlackConfig slackConfig;
/** Internal Server Error + Slack Alert **/
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleException(Exception exception, HttpServletRequest request) {
val errorDTO = requestToDTO(exception, request);
sendSlackAlarm(errorDTO.toString());
ApiResponse response = ApiResponse.serverError(errorDTO);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Object> handleRuntimeException(RuntimeException exception, HttpServletRequest request) {
val errorDTO = requestToDTO(exception, request);
sendSlackAlarm(errorDTO.toString());
ApiResponse response = ApiResponse.serverError(errorDTO);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
/** Custom Error + 4__ Error Handler **/
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ApiResponse> handleEntityNotFoundException(EntityNotFoundException exception) {
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ApiResponse> handleIllegalArgumentException(IllegalArgumentException exception) {
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception){
ApiResponse response = ApiResponse.fail(exception.getBindingResult().getAllErrors().get(0).getDefaultMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(AuthException.class)
public ResponseEntity<ApiResponse> authException(AuthException exception){
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(AwsException.class)
public ResponseEntity<ApiResponse> awsException(AwsException exception){
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(WorkException.class)
public ResponseEntity<ApiResponse> workException(WorkException exception){
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(BlockException.class)
public ResponseEntity<ApiResponse> BlockException(BlockException exception){
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, exception.getStatusCode());
}
@ExceptionHandler(ScrapException.class)
public ResponseEntity<ApiResponse> ScrapException(ScrapException exception) {
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, exception.getStatusCode());
}
@ExceptionHandler(ReportException.class)
public ResponseEntity<ApiResponse> ReportException(ReportException exception){
ApiResponse response = ApiResponse.fail(exception.getMessage());
return new ResponseEntity<>(response, exception.getStatusCode());
}
private InternalServerErrorDTO requestToDTO(Exception exception, HttpServletRequest request) {
val header = InternalServerErrorDTO.extractHeaders(request);
return InternalServerErrorDTO.of(header, request.getMethod(), request.getRequestURL().toString(),
exception.getMessage(), exception.getClass().getName(), LocalDateTime.now());
}
private void sendSlackAlarm(String dto) {
try{
val slackResponse = slack.send(slackConfig.getUrl(), SlackErrorPayload.of(dto)).getBody();
if(!slackResponse.equals("ok")) {
throw new Exception(ExceptionMessage.NOT_POST_SLACK_ALARM.getMessage());// todo log -> 슬랙알림 안감
}
}catch (Exception exception) {
System.out.println(exception.toString()); // todo log
}
}
}