errorCatcher.js 2.2 KB

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