errorCatcher.js 2.6 KB

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