Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. const BaseExceptionHandler = use('BaseExceptionHandler');
  2. const Logger = use('Logger');
  3.  
  4. class ExceptionHandler extends BaseExceptionHandler {
  5. async handle(error, { request, response, session, view }) {
  6. const isJSON = request.accepts(['html', 'json']) === 'json';
  7. const production = process.env.NODE_ENV === 'production';
  8.  
  9. if (error.code === 'E_INVALID_SESSION') {
  10. session.flash({
  11. flash_error: 'You have to login/sign up to access this page.'
  12. });
  13. await session.commit();
  14. response.route('login');
  15. return;
  16. }
  17.  
  18. if (error.code === 'E_VALIDATION_FAILED' && !isJSON) {
  19. session.withErrors(error.messages).flashAll();
  20. await session.commit();
  21. response.redirect('back');
  22. return;
  23. }
  24.  
  25. // handle if production
  26. if (production && !isJSON) {
  27. return response.send(view.render('errors.index', { error }));
  28. }
  29.  
  30. if (isJSON) {
  31. return response.status(error.status).send({
  32. code: error.code,
  33. status: error.status,
  34. errors: error.messages,
  35. name: error.name
  36. });
  37. }
  38.  
  39. // handle all other
  40. super.handle(...arguments);
  41. }
  42.  
  43. async report(error, {}) {
  44. if (error.code !== 'E_INVALID_SESSION') {
  45. Logger.info(error);
  46. }
  47. }
  48. }
  49.  
  50. module.exports = ExceptionHandler;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement