HumanReadableLogs.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import LatexLogParser from '../log-parser/latex-log-parser'
  2. import ruleset from './HumanReadableLogsRules'
  3. export default {
  4. parse(rawLog, options) {
  5. let parsedLogEntries
  6. if (typeof rawLog === 'string') {
  7. const latexLogParser = new LatexLogParser(rawLog, options)
  8. parsedLogEntries = latexLogParser.parse()
  9. } else {
  10. parsedLogEntries = rawLog
  11. }
  12. const _getRule = function (logMessage) {
  13. for (const rule of ruleset) {
  14. if (rule.regexToMatch.test(logMessage)) {
  15. return rule
  16. }
  17. }
  18. }
  19. const seenErrorTypes = {} // keep track of types of errors seen
  20. for (const entry of parsedLogEntries.all) {
  21. const ruleDetails = _getRule(entry.message)
  22. if (ruleDetails != null) {
  23. let type
  24. if (ruleDetails.ruleId != null) {
  25. entry.ruleId = ruleDetails.ruleId
  26. }
  27. if (ruleDetails.newMessage != null) {
  28. entry.message = entry.message.replace(
  29. ruleDetails.regexToMatch,
  30. ruleDetails.newMessage
  31. )
  32. }
  33. if (ruleDetails.contentRegex) {
  34. const match = entry.content.match(ruleDetails.contentRegex)
  35. if (match) {
  36. entry.contentDetails = match.slice(1)
  37. }
  38. }
  39. if (entry.contentDetails && ruleDetails.improvedTitle) {
  40. const message = ruleDetails.improvedTitle(
  41. entry.message,
  42. entry.contentDetails
  43. )
  44. if (Array.isArray(message)) {
  45. entry.message = message[0]
  46. // removing the messageComponent, as the markup possible in it was causing crashes when
  47. // attempting to broadcast it in the detach-context (cant structuredClone an html node)
  48. // see https://github.com/overleaf/internal/discussions/15031 for context
  49. // entry.messageComponent = message[1]
  50. } else {
  51. entry.message = message
  52. }
  53. }
  54. // suppress any entries that are known to cascade from previous error types
  55. if (ruleDetails.cascadesFrom != null) {
  56. for (type of ruleDetails.cascadesFrom) {
  57. if (seenErrorTypes[type]) {
  58. entry.suppressed = true
  59. }
  60. }
  61. }
  62. // record the types of errors seen
  63. if (ruleDetails.types != null) {
  64. for (type of ruleDetails.types) {
  65. seenErrorTypes[type] = true
  66. }
  67. }
  68. }
  69. }
  70. // filter out the suppressed errors (from the array entries in parsedLogEntries)
  71. for (const key in parsedLogEntries) {
  72. const errors = parsedLogEntries[key]
  73. if (typeof errors === 'object' && errors.length > 0) {
  74. parsedLogEntries[key] = Array.from(errors).filter(
  75. err => !err.suppressed
  76. )
  77. }
  78. }
  79. return parsedLogEntries
  80. },
  81. }