initializeSdk.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {ERROR_MAP as origErrorMap} from 'sentry/utils/requestError/requestError';
  2. import {isEventWithFileUrl, isFilteredRequestErrorEvent} from './initializeSdk';
  3. const ERROR_MAP = {
  4. ...origErrorMap,
  5. // remove `UndefinedResponseBodyError` since we don't filter those
  6. 200: undefined,
  7. };
  8. describe('isFilteredRequestErrorEvent', () => {
  9. const methods = ['GET', 'POST', 'PUT', 'DELETE'];
  10. const stati = [200, 401, 403, 404, 429];
  11. describe('matching error type, matching message', () => {
  12. for (const method of methods) {
  13. describe(`${method} requests`, () => {
  14. for (const status of stati) {
  15. // We have to filter out falsy values here because 200 isn't in `ERROR_MAP`
  16. // and will never appear with any other error name besides `RequestError`
  17. for (const errorName of ['RequestError', ERROR_MAP[status]].filter(Boolean)) {
  18. describe('main error', () => {
  19. it(`recognizes ${status} ${method} events of type ${errorName}`, () => {
  20. const event = {
  21. exception: {
  22. values: [
  23. {type: errorName, value: `${method} /dogs/are/great/ ${status}`},
  24. ],
  25. },
  26. };
  27. expect(isFilteredRequestErrorEvent(event)).toBeTruthy();
  28. });
  29. });
  30. describe('cause error', () => {
  31. it(`recognizes ${status} ${method} events of type ${errorName} as causes`, () => {
  32. const event = {
  33. exception: {
  34. values: [
  35. {type: errorName, value: `${method} /dogs/are/great/ ${status}`},
  36. {type: 'InsufficientTreatsError', value: 'Not enough treats!'},
  37. ],
  38. },
  39. };
  40. expect(isFilteredRequestErrorEvent(event)).toBeTruthy();
  41. });
  42. });
  43. }
  44. }
  45. });
  46. }
  47. });
  48. describe('matching error type, non-matching message', () => {
  49. for (const status of stati) {
  50. // We have to filter out falsy values here because 200 isn't in `ERROR_MAP`
  51. // and will never appear with any other error name besides `RequestError`
  52. for (const errorName of ['RequestError', ERROR_MAP[status]].filter(Boolean)) {
  53. describe('main error', () => {
  54. it(`rejects other errors of type ${errorName}`, () => {
  55. const event = {
  56. exception: {
  57. values: [
  58. {type: errorName, value: "Failed to fetch requested object: 'ball'"},
  59. ],
  60. },
  61. };
  62. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  63. });
  64. });
  65. describe('cause error', () => {
  66. it(`rejects other errors of type ${errorName} as causes`, () => {
  67. const event = {
  68. exception: {
  69. values: [
  70. {type: errorName, value: "Failed to fetch requested object: 'ball'"},
  71. {type: 'InsufficientTreatsError', value: 'Not enough treats!'},
  72. ],
  73. },
  74. };
  75. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  76. });
  77. });
  78. }
  79. }
  80. });
  81. describe('non-matching error type, non-matching message', () => {
  82. it(`rejects other errors`, () => {
  83. const event = {
  84. exception: {
  85. values: [{type: 'UncaughtSquirrelError', value: 'Squirrel was not caught'}],
  86. },
  87. };
  88. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  89. });
  90. it(`rejects other errors as causes`, () => {
  91. const event = {
  92. exception: {
  93. values: [
  94. {type: 'UncaughtSquirrelError', value: 'Squirrel was not caught'},
  95. {type: 'InsufficientTreatsError', value: 'Not enough treats!'},
  96. ],
  97. },
  98. };
  99. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  100. });
  101. });
  102. });
  103. describe('isEventWithFileUrl', () => {
  104. it('recognizes events with `file://` urls', () => {
  105. const event = {request: {url: 'file://dogs/are/great.html'}};
  106. expect(isEventWithFileUrl(event)).toBeTruthy();
  107. });
  108. it('rejects events with other urls', () => {
  109. const event = {request: {url: 'http://dogs.are.great'}};
  110. expect(isEventWithFileUrl(event)).toBeFalsy();
  111. });
  112. it('rejects events without urls', () => {
  113. const event = {};
  114. expect(isEventWithFileUrl(event)).toBeFalsy();
  115. });
  116. });