initializeSdk.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {ERROR_MAP as origErrorMap} from 'sentry/utils/requestError/requestError';
  2. import {
  3. addEndpointTagToRequestError,
  4. isEventWithFileUrl,
  5. isFilteredRequestErrorEvent,
  6. } from './initializeSdk';
  7. const ERROR_MAP = {
  8. ...origErrorMap,
  9. // remove `UndefinedResponseBodyError` since we don't filter those
  10. 200: undefined,
  11. };
  12. describe('isFilteredRequestErrorEvent', () => {
  13. const methods = ['GET', 'POST', 'PUT', 'DELETE'];
  14. const stati = [200, 400, 401, 403, 404, 429];
  15. describe('matching error type, matching message', () => {
  16. for (const method of methods) {
  17. describe(`${method} requests`, () => {
  18. for (const status of stati) {
  19. // We have to filter out falsy values here because 200 isn't in `ERROR_MAP`
  20. // and will never appear with any other error name besides `RequestError`
  21. for (const errorName of ['RequestError', ERROR_MAP[status]].filter(Boolean)) {
  22. describe('main error', () => {
  23. it(`recognizes ${status} ${method} events of type ${errorName}`, () => {
  24. const event = {
  25. exception: {
  26. values: [
  27. {type: errorName, value: `${method} /dogs/are/great/ ${status}`},
  28. ],
  29. },
  30. };
  31. expect(isFilteredRequestErrorEvent(event)).toBeTruthy();
  32. });
  33. });
  34. describe('cause error', () => {
  35. it(`recognizes ${status} ${method} events of type ${errorName} as causes`, () => {
  36. const event = {
  37. exception: {
  38. values: [
  39. {type: errorName, value: `${method} /dogs/are/great/ ${status}`},
  40. {type: 'InsufficientTreatsError', value: 'Not enough treats!'},
  41. ],
  42. },
  43. };
  44. expect(isFilteredRequestErrorEvent(event)).toBeTruthy();
  45. });
  46. });
  47. }
  48. }
  49. });
  50. }
  51. });
  52. describe('matching error type, non-matching message', () => {
  53. for (const status of stati) {
  54. // We have to filter out falsy values here because 200 isn't in `ERROR_MAP`
  55. // and will never appear with any other error name besides `RequestError`
  56. for (const errorName of ['RequestError', ERROR_MAP[status]].filter(Boolean)) {
  57. describe('main error', () => {
  58. it(`rejects other errors of type ${errorName}`, () => {
  59. const event = {
  60. exception: {
  61. values: [
  62. {type: errorName, value: "Failed to fetch requested object: 'ball'"},
  63. ],
  64. },
  65. };
  66. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  67. });
  68. });
  69. describe('cause error', () => {
  70. it(`rejects other errors of type ${errorName} as causes`, () => {
  71. const event = {
  72. exception: {
  73. values: [
  74. {type: errorName, value: "Failed to fetch requested object: 'ball'"},
  75. {type: 'InsufficientTreatsError', value: 'Not enough treats!'},
  76. ],
  77. },
  78. };
  79. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  80. });
  81. });
  82. }
  83. }
  84. });
  85. describe('non-matching error type, non-matching message', () => {
  86. it(`rejects other errors`, () => {
  87. const event = {
  88. exception: {
  89. values: [{type: 'UncaughtSquirrelError', value: 'Squirrel was not caught'}],
  90. },
  91. };
  92. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  93. });
  94. it(`rejects other errors as causes`, () => {
  95. const event = {
  96. exception: {
  97. values: [
  98. {type: 'UncaughtSquirrelError', value: 'Squirrel was not caught'},
  99. {type: 'InsufficientTreatsError', value: 'Not enough treats!'},
  100. ],
  101. },
  102. };
  103. expect(isFilteredRequestErrorEvent(event)).toBeFalsy();
  104. });
  105. });
  106. });
  107. describe('isEventWithFileUrl', () => {
  108. it('recognizes events with `file://` urls', () => {
  109. const event = {request: {url: 'file://dogs/are/great.html'}};
  110. expect(isEventWithFileUrl(event)).toBeTruthy();
  111. });
  112. it('rejects events with other urls', () => {
  113. const event = {request: {url: 'http://dogs.are.great'}};
  114. expect(isEventWithFileUrl(event)).toBeFalsy();
  115. });
  116. it('rejects events without urls', () => {
  117. const event = {};
  118. expect(isEventWithFileUrl(event)).toBeFalsy();
  119. });
  120. });
  121. describe('addEndpointTagToRequestError', () => {
  122. it('adds `endpoint` tag to events with matching message`', () => {
  123. const event = {
  124. exception: {
  125. values: [{type: 'RequestError', value: 'GET /dogs/are/great/ 500'}],
  126. },
  127. tags: {},
  128. };
  129. addEndpointTagToRequestError(event);
  130. expect(event.tags).toEqual({
  131. endpoint: 'GET /dogs/are/great/',
  132. });
  133. });
  134. it("doesn't add `endpoint` tag to events with non-matching message", () => {
  135. const nonmatchingMessages = [
  136. 'RequestError with no endpoint for some reason',
  137. 'Some other stuff is wrong with endpoint /dogs/are/great/',
  138. 'This error has nothing to do with requests or endpoints at all',
  139. ];
  140. for (const msg of nonmatchingMessages) {
  141. const event = {
  142. exception: {
  143. values: [{type: 'RequestError', value: msg}],
  144. },
  145. tags: {},
  146. };
  147. addEndpointTagToRequestError(event);
  148. expect(event.tags).toEqual({});
  149. }
  150. });
  151. it("doesn't add `endpoint` tag to events with no exception", () => {
  152. const event = {
  153. tags: {},
  154. };
  155. addEndpointTagToRequestError(event);
  156. expect(event.tags).toEqual({});
  157. });
  158. });