errorCatcher.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* eslint-disable
  2. max-len,
  3. no-undef,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const app = angular.module('ErrorCatcher', [])
  14. const UNHANDLED_REJECTION_ERR_MSG = 'Possibly unhandled rejection: canceled'
  15. app.config([
  16. '$provide',
  17. $provide =>
  18. $provide.decorator('$exceptionHandler', [
  19. '$log',
  20. '$delegate',
  21. ($log, $delegate) =>
  22. function(exception, cause) {
  23. if (
  24. exception === UNHANDLED_REJECTION_ERR_MSG &&
  25. cause === undefined
  26. ) {
  27. return
  28. }
  29. if (
  30. (typeof Raven !== 'undefined' && Raven !== null
  31. ? Raven.captureException
  32. : undefined) != null
  33. ) {
  34. Raven.captureException(exception)
  35. }
  36. return $delegate(exception, cause)
  37. }
  38. ])
  39. ])
  40. // Interceptor to check auth failures in all $http requests
  41. // http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html
  42. app.factory('unAuthHttpResponseInterceptor', ($q, $location) => ({
  43. responseError(response) {
  44. // redirect any unauthorised or forbidden responses back to /login
  45. //
  46. // set disableAutoLoginRedirect:true in the http request config
  47. // to disable this behaviour
  48. if (
  49. [401, 403].includes(response.status) &&
  50. !(response.config != null
  51. ? response.config.disableAutoLoginRedirect
  52. : undefined)
  53. ) {
  54. // for /project urls set the ?redir parameter to come back here
  55. // otherwise just go to the login page
  56. if (window.location.pathname.match(/^\/project/)) {
  57. window.location = `/login?redir=${encodeURI(window.location.pathname)}`
  58. } else {
  59. window.location = '/login'
  60. }
  61. }
  62. // pass the response back to the original requester
  63. return $q.reject(response)
  64. }
  65. }))
  66. app.config([
  67. '$httpProvider',
  68. $httpProvider =>
  69. $httpProvider.interceptors.push('unAuthHttpResponseInterceptor')
  70. ])