Guest User

Untitled

a guest
Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. public @ResponseBody Result save(@Valid Entity entity, BindingResult errors) {
  2. Result r = new Result();
  3. if (errors.hasErrors()) {
  4. r.setStatus(Result.VALIDATION_ERROR);
  5. // ...
  6. } else {
  7. try {
  8. dao.save(entity);
  9. r.setStatus(Result.SUCCESS);
  10. } except (ValidationException e) {
  11. r.setStatus(Result.VALIDATION_ERROR);
  12. r.setText(e.getMessage());
  13. }
  14. }
  15. return r;
  16. }
  17.  
  18. public class MyAppTechnicalException extends Exception { ... }
  19.  
  20. @Controller
  21. public class MyController {
  22.  
  23. ...
  24.  
  25. @RequestMapping(...)
  26. public void createMyObject(...) throws MyAppTechnicalException {
  27. ...
  28. someServiceThanCanThrowMyAppTechnicalException.create(...);
  29. ...
  30. }
  31.  
  32. ...
  33.  
  34. @ExceptionHandler(MyAppTechnicalException.class)
  35. public String handleMyAppTechnicalException(MyAppTechnicalException e, Model model) {
  36.  
  37. // Compute your generic error message/code with e.
  38. // Or just use a generic error/code, in which case you can remove e from the parameters
  39. String genericErrorMessage = "Some technical exception has occured blah blah blah" ;
  40.  
  41. // There are many other ways to pass an error to the view, but you get the idea
  42. model.addAttribute("myErrors", genericErrorMessage);
  43.  
  44. return "myView";
  45. }
  46.  
  47. }
  48.  
  49. @ExceptionHandler({MyException1.class, MyException2.class, ...})
  50. public String yourMethod(Exception e, Model model) {
  51. ...
  52. }
Add Comment
Please, Sign In to add comment