-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathGeneralAdvice.java
More file actions
31 lines (25 loc) · 1.26 KB
/
GeneralAdvice.java
File metadata and controls
31 lines (25 loc) · 1.26 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
package com.programmers.vouchermanagement.advice;
import java.util.NoSuchElementException;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.programmers.vouchermanagement.advice.annotation.AdminController;
@ControllerAdvice(annotations = AdminController.class)
public class GeneralAdvice {
@ExceptionHandler({IllegalArgumentException.class, NoSuchElementException.class, RuntimeException.class})
public String renderErrorPage(RuntimeException exception, Model model) {
model.addAttribute("message", exception.getMessage());
return "error";
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public String handleInvalidMethodArgumentException(BindingResult bindingResult, Model model) {
FieldError error = bindingResult.getFieldErrors()
.get(0);
String message = error.getField() + " exception - " + error.getDefaultMessage();
model.addAttribute("message", message);
return "error";
}
}