initializeSdk.spec.tsx 4.3 KB

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