Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public @ResponseBody Result save(@Valid Entity entity, BindingResult errors) {
- Result r = new Result();
- if (errors.hasErrors()) {
- r.setStatus(Result.VALIDATION_ERROR);
- // ...
- } else {
- try {
- dao.save(entity);
- r.setStatus(Result.SUCCESS);
- } except (ValidationException e) {
- r.setStatus(Result.VALIDATION_ERROR);
- r.setText(e.getMessage());
- }
- }
- return r;
- }
- public class MyAppTechnicalException extends Exception { ... }
- @Controller
- public class MyController {
- ...
- @RequestMapping(...)
- public void createMyObject(...) throws MyAppTechnicalException {
- ...
- someServiceThanCanThrowMyAppTechnicalException.create(...);
- ...
- }
- ...
- @ExceptionHandler(MyAppTechnicalException.class)
- public String handleMyAppTechnicalException(MyAppTechnicalException e, Model model) {
- // Compute your generic error message/code with e.
- // Or just use a generic error/code, in which case you can remove e from the parameters
- String genericErrorMessage = "Some technical exception has occured blah blah blah" ;
- // There are many other ways to pass an error to the view, but you get the idea
- model.addAttribute("myErrors", genericErrorMessage);
- return "myView";
- }
- }
- @ExceptionHandler({MyException1.class, MyException2.class, ...})
- public String yourMethod(Exception e, Model model) {
- ...
- }
Add Comment
Please, Sign In to add comment