ApiCallerHandlers.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { AxiosRequestConfig } from 'axios'
  2. import notificationStore from '../stores/NotificationStore'
  3. import { RequestOptions } from './ApiCaller'
  4. import logger from './ApiLogger'
  5. const isOnLoginPage = (): boolean => window.location.pathname.indexOf('login') > -1
  6. const truncateUrl = (url: string): string => {
  7. const MAX_LENGTH = 100
  8. let relativePath = url.replace(/http(s)?:\/\/.*?\//, '/')
  9. relativePath += relativePath
  10. if (relativePath.length > MAX_LENGTH) {
  11. relativePath = `${relativePath.substr(0, MAX_LENGTH)}...`
  12. }
  13. return relativePath
  14. }
  15. const redirect = (statusCode: number) => {
  16. if (statusCode !== 401 || isOnLoginPage()) {
  17. return
  18. }
  19. let currentPath = '?prev=/'
  20. if (window.location.pathname !== '/') {
  21. currentPath = `?prev=${window.location.pathname}${window.location.search}`
  22. }
  23. window.location.href = `/login${currentPath}`
  24. }
  25. class ApiCallerHandlers {
  26. setupOptions: RequestOptions
  27. axiosOptions: AxiosRequestConfig
  28. constructor(setupOptions: RequestOptions, axiosOptions: AxiosRequestConfig) {
  29. this.setupOptions = setupOptions
  30. this.axiosOptions = axiosOptions
  31. }
  32. handleErrorResponse(error: any) {
  33. if ((error.response.status !== 401 || !isOnLoginPage()) && !this.setupOptions.quietError) {
  34. const data = error.response.data
  35. const message = (data && data.error && data.error.message) || (data && data.description)
  36. const alertMessage = message || `${error.response.statusText || error.response.status} ${truncateUrl(this.setupOptions.url)}`
  37. const status = error.response.status && error.response.statusText
  38. ? `${error.response.status} - ${error.response.statusText}`
  39. : error.response.statusText || error.response.status
  40. notificationStore.alert(alertMessage, 'error', {
  41. action: {
  42. label: 'View details',
  43. callback: () => ({ request: this.axiosOptions, error: { status, message } }),
  44. },
  45. })
  46. }
  47. if (error.request.responseURL.indexOf('/proxy/') === -1 && error.request.responseURL.indexOf('/azure-login') === -1) {
  48. redirect(error.response.status)
  49. }
  50. logger.log({
  51. url: this.axiosOptions.url,
  52. method: this.axiosOptions.method || 'GET',
  53. type: 'RESPONSE',
  54. requestStatus: error.response.status,
  55. requestError: error,
  56. })
  57. return error.response
  58. }
  59. handleErrorRequest(error: any) {
  60. // The request was made but no response was received
  61. // `error.request` is an instance of XMLHttpRequest
  62. if (!isOnLoginPage() && !this.setupOptions.quietError) {
  63. notificationStore.alert(
  64. `Request failed, there might be a problem with the connection to the server. ${truncateUrl(this.setupOptions.url)}`,
  65. 'error',
  66. {
  67. action: {
  68. label: 'View details',
  69. callback: () => ({
  70. request: this.axiosOptions,
  71. error: { message: 'Request was made but no response was received' },
  72. }),
  73. },
  74. },
  75. )
  76. }
  77. logger.log({
  78. url: this.axiosOptions.url,
  79. method: this.axiosOptions.method || 'GET',
  80. type: 'RESPONSE',
  81. description: 'No response',
  82. requestStatus: 500,
  83. requestError: error,
  84. })
  85. return {}
  86. }
  87. handleRequestCancel(error: any) {
  88. const canceled = error.__CANCEL__
  89. if (canceled) {
  90. logger.log({
  91. url: this.axiosOptions.url,
  92. method: this.axiosOptions.method || 'GET',
  93. type: 'RESPONSE',
  94. requestStatus: 'canceled',
  95. })
  96. return { canceled }
  97. }
  98. // Something happened in setting up the request that triggered an Error
  99. logger.log({
  100. url: this.axiosOptions.url,
  101. method: this.axiosOptions.method || 'GET',
  102. type: 'RESPONSE',
  103. description: 'Something happened in setting up the request',
  104. requestStatus: 500,
  105. })
  106. notificationStore.alert(
  107. `Request failed, there might be a problem with the connection to the server. ${truncateUrl(this.setupOptions.url)}`,
  108. 'error',
  109. {
  110. action: {
  111. label: 'View details',
  112. callback: () => ({
  113. request: this.axiosOptions,
  114. error: { message: 'Something happened in setting up the request' },
  115. }),
  116. },
  117. },
  118. )
  119. return error
  120. }
  121. }
  122. export default ApiCallerHandlers