utils.ts 845 B

123456789101112131415161718192021
  1. import { last } from 'lodash';
  2. /**
  3. * Graphite-web before v1.6 returns HTTP 500 with full stack traces in an HTML page
  4. * when a query fails. It results in massive error alerts with HTML tags in the UI.
  5. * This function removes all HTML tags and keeps only the last line from the stack
  6. * trace which should be the most meaningful.
  7. */
  8. export function reduceError(error: any): any {
  9. if (error && error.status === 500 && error.data?.message?.startsWith('<body')) {
  10. // Remove all HTML tags and take the last line from the stack trace
  11. const newMessage = last<string>(
  12. error.data.message
  13. .replace(/(<([^>]+)>)/gi, '')
  14. .trim()
  15. .split(/\n/)
  16. )!.replace(/u?&#[^;]+;/g, '');
  17. error.data.message = `Graphite encountered an unexpected error while handling your request. ${newMessage}`;
  18. }
  19. return error;
  20. }