-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathErrorAdvice.java
More file actions
50 lines (35 loc) · 1.86 KB
/
ErrorAdvice.java
File metadata and controls
50 lines (35 loc) · 1.86 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
package com.codejam.demo.exception;
import com.codejam.demo.constant.ErrorCodes;
import com.codejam.demo.dto.ErrorResponseDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class ErrorAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(DataNotFoundException.class)
public ResponseEntity<ErrorResponseDto> handleDataNotFoundException(DataNotFoundException ex) {
ErrorResponseDto errorResponseDto = ErrorResponseDto.builder()
.errorCode(ErrorCodes.LS001)
.message(ex.getMessage())
.variable(ex.getVariable()).build();
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponseDto);
}
@ExceptionHandler(ExternalCallException.class)
public ResponseEntity<ErrorResponseDto> handleExternalCallException(ExternalCallException ex) {
ErrorResponseDto errorResponseDto = ErrorResponseDto.builder()
.errorCode(ErrorCodes.LS002)
.message(ex.getMessage())
.variable(ex.getVariable()).build();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponseDto);
}
@ExceptionHandler(GeneralException.class)
public ResponseEntity<ErrorResponseDto> handleGeneralException(GeneralException ex) {
ErrorResponseDto errorResponseDto = ErrorResponseDto.builder()
.errorCode(ErrorCodes.LS003)
.message(ex.getMessage())
.variable(ex.getVariable()).build();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponseDto);
}
}