-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathRestAdvice.java
More file actions
43 lines (36 loc) · 1.84 KB
/
RestAdvice.java
File metadata and controls
43 lines (36 loc) · 1.84 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
package com.programmers.vouchermanagement.advice;
import java.util.NoSuchElementException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice(annotations = RestController.class)
public class RestAdvice {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ExceptionMessage handleInvalidMethodArgumentException(BindingResult bindingResult) {
FieldError error = bindingResult.getFieldErrors()
.get(0);
String message = error.getField() + " exception - " + error.getDefaultMessage();
return new ExceptionMessage(message);
}
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ExceptionMessage handleIllegalArgumentException(IllegalArgumentException exception) {
return new ExceptionMessage(exception.getMessage());
}
@ExceptionHandler(NoSuchElementException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ExceptionMessage handleNoSuchElementException(NoSuchElementException exception) {
return new ExceptionMessage(exception.getMessage());
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ExceptionMessage handleRuntimeException(RuntimeException exception) {
return new ExceptionMessage("Unexpected message appears. Please contact to DEV team");
}
}