errorCatcher.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const app = angular.module('ErrorCatcher', [])
  13. const UNHANDLED_REJECTION_ERR_MSG = 'Possibly unhandled rejection: canceled'
  14. app.config([
  15. '$provide',
  16. $provide =>
  17. $provide.decorator('$exceptionHandler', [
  18. '$log',
  19. '$delegate',
  20. ($log, $delegate) =>
  21. function(exception, cause) {
  22. if (
  23. exception === UNHANDLED_REJECTION_ERR_MSG &&
  24. cause === undefined
  25. ) {
  26. return
  27. }
  28. if (
  29. (typeof Raven !== 'undefined' && Raven !== null
  30. ? Raven.captureException
  31. : undefined) != null
  32. ) {
  33. Raven.captureException(exception)
  34. }
  35. return $delegate(exception, cause)
  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', ($q, $location) => ({
  42. responseError(response) {
  43. // redirect any unauthorised or forbidden responses back to /login
  44. //
  45. // set disableAutoLoginRedirect:true in the http request config
  46. // to disable this behaviour
  47. if (
  48. [401, 403].includes(response.status) &&
  49. !(response.config != null
  50. ? response.config.disableAutoLoginRedirect
  51. : undefined)
  52. ) {
  53. // for /project urls set the ?redir parameter to come back here
  54. // otherwise just go to the login page
  55. if (window.location.pathname.match(/^\/project/)) {
  56. window.location = `/login?redir=${encodeURI(window.location.pathname)}`
  57. } else {
  58. window.location = '/login'
  59. }
  60. }
  61. // pass the response back to the original requester
  62. return $q.reject(response)
  63. }
  64. }))
  65. app.config([
  66. '$httpProvider',
  67. $httpProvider =>
  68. $httpProvider.interceptors.push('unAuthHttpResponseInterceptor')
  69. ])