errorCatcher.js 2.2 KB

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