initializeSdk.spec.tsx 6.5 KB

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