utils.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { reduceError } from './utils';
  2. const SAMPLE_500_PAGE = `<body style="background-color: #666666; color: black;">
  3. <center>
  4. <h2 style='font-family: "Arial"'>
  5. <p>Graphite encountered an unexpected error while handling your request.</p>
  6. <p>Please contact your site administrator if the problem persists.</p>
  7. </h2>
  8. <br/>
  9. <div style="width: 50%; text-align: center; font-family: monospace; background-color: black; font-weight: bold; color: #ff4422;">
  10. </div>
  11. <div style="width: 70%; text-align: left; background-color: black; color: #44ff22; border: thin solid gray;">
  12. <pre>
  13. Traceback (most recent call last):
  14. File &quot;/usr/lib/python2.7/dist-packages/django/core/handlers/base.py&quot;, line 112, in get_response
  15. response = wrapped_callback(request, *callback_args, **callback_kwargs)
  16. File &quot;/var/lib/graphite/webapp/graphite/render/views.py&quot;, line 125, in renderView
  17. seriesList = evaluateTarget(requestContext, target)
  18. File &quot;/var/lib/graphite/webapp/graphite/render/evaluator.py&quot;, line 10, in evaluateTarget
  19. result = evaluateTokens(requestContext, tokens)
  20. File &quot;/var/lib/graphite/webapp/graphite/render/evaluator.py&quot;, line 21, in evaluateTokens
  21. return evaluateTokens(requestContext, tokens.expression)
  22. File &quot;/var/lib/graphite/webapp/graphite/render/evaluator.py&quot;, line 27, in evaluateTokens
  23. func = SeriesFunctions[tokens.call.func]
  24. KeyError: u&#39;aliasByNodde&#39;
  25. </pre>
  26. </div>
  27. </center>
  28. `;
  29. describe('Graphite utils', () => {
  30. it('should reduce HTML based errors', () => {
  31. const error = {
  32. status: 500,
  33. data: {
  34. message: SAMPLE_500_PAGE,
  35. },
  36. };
  37. expect(reduceError(error)).toMatchObject({
  38. data: {
  39. message: 'Graphite encountered an unexpected error while handling your request. KeyError: aliasByNodde',
  40. },
  41. });
  42. });
  43. it('should return original error for non-HTML 500 error pages', () => {
  44. const error = {
  45. status: 500,
  46. data: {
  47. message: 'ERROR MESSAGE',
  48. },
  49. };
  50. expect(reduceError(error)).toMatchObject({
  51. data: {
  52. message: 'ERROR MESSAGE',
  53. },
  54. });
  55. });
  56. it('should return original error for non 500 errors', () => {
  57. const error = {
  58. status: 400,
  59. data: {
  60. message: 'ERROR MESSAGE',
  61. },
  62. };
  63. expect(reduceError(error)).toMatchObject({
  64. data: {
  65. message: 'ERROR MESSAGE',
  66. },
  67. });
  68. });
  69. it('should return original error for errors other than FetchError (not data property)', () => {
  70. const error = {
  71. message: 'ERROR MESSAGE',
  72. };
  73. expect(reduceError(error)).toMatchObject({
  74. message: 'ERROR MESSAGE',
  75. });
  76. });
  77. });