errorCatcher.js 2.4 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. import { captureException } from '../infrastructure/error-reporter'
  13. const app = angular.module('ErrorCatcher', [])
  14. const UNHANDLED_REJECTION_ERR_MSG = 'Possibly unhandled rejection: canceled'
  15. app.config([
  16. '$provide',
  17. function ($provide) {
  18. return $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. captureException(exception, {
  30. tags: {
  31. handler: 'angular-exception-handler',
  32. },
  33. })
  34. return $delegate(exception, cause)
  35. },
  36. ])
  37. },
  38. ])
  39. // Interceptor to check auth failures in all $http requests
  40. // http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html
  41. app.factory('unAuthHttpResponseInterceptor', [
  42. '$q',
  43. function ($q) {
  44. return {
  45. responseError(response) {
  46. // redirect any unauthorised or forbidden responses back to /login
  47. //
  48. // set disableAutoLoginRedirect:true in the http request config
  49. // to disable this behaviour
  50. if (
  51. [401, 403].includes(response.status) &&
  52. !(response.config != null
  53. ? response.config.disableAutoLoginRedirect
  54. : undefined)
  55. ) {
  56. // for /project urls set the ?redir parameter to come back here
  57. // otherwise just go to the login page
  58. if (window.location.pathname.match(/^\/project/)) {
  59. window.location = `/login?redir=${encodeURI(
  60. window.location.pathname
  61. )}`
  62. } else {
  63. window.location = '/login'
  64. }
  65. }
  66. // pass the response back to the original requester
  67. return $q.reject(response)
  68. },
  69. }
  70. },
  71. ])
  72. app.config([
  73. '$httpProvider',
  74. function ($httpProvider) {
  75. return $httpProvider.interceptors.push('unAuthHttpResponseInterceptor')
  76. },
  77. ])