errorCatcher.coffee 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. app = angular.module 'ErrorCatcher', []
  2. app.config ['$provide', ($provide) ->
  3. $provide.decorator '$exceptionHandler', ['$log', '$delegate', ($log, $delegate) ->
  4. return (exception, cause) ->
  5. if (Raven?.captureException?)
  6. Raven.captureException exception;
  7. $delegate(exception, cause)
  8. ]
  9. ]
  10. # Interceptor to check auth failures in all $http requests
  11. # http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html
  12. app.factory 'unAuthHttpResponseInterceptor', ['$q','$location', ($q, $location) ->
  13. responseError: (response) ->
  14. # redirect any unauthorised or forbidden responses back to /login
  15. #
  16. # set disableAutoLoginRedirect:true in the http request config
  17. # to disable this behaviour
  18. if response.status in [401, 403] and not response.config?.disableAutoLoginRedirect
  19. # for /project urls set the ?redir parameter to come back here
  20. # otherwise just go to the login page
  21. if window.location.pathname.match(/^\/project/)
  22. window.location = "/login?redir=#{encodeURI(window.location.pathname)}"
  23. else
  24. window.location = "/login"
  25. # pass the response back to the original requester
  26. return $q.reject(response)
  27. ]
  28. app.config ['$httpProvider', ($httpProvider) ->
  29. $httpProvider.interceptors.push 'unAuthHttpResponseInterceptor'
  30. ]