const util = require('util') const exec = util.promisify(require('child_process').exec) const errorStackParser = require('error-stack-parser') const { escapeHTML } = require('../utils') async function errorLog (error, ctx) { const errorInfo = errorStackParser.parse(error) let gitBlame for (const ei of errorInfo) { if (!gitBlame) gitBlame = await exec(`git blame -L ${ei.lineNumber},${ei.lineNumber} -- ${ei.fileName}`).catch(() => {}) } let errorText = `error for ${ctx.updateType}:` if (ctx.match) errorText += `\n${ctx.match[0]}` if (ctx.from && ctx.from.id) errorText += `\n\nuser: ${escapeHTML(ctx.from.first_name)} #user_${ctx.from.id}` if (ctx?.session?.chainActions && ctx?.session.chainActions.length > 0) errorText += '\n\nπŸ”— ' + ctx?.session.chainActions.map(v => `${v}`).join(' ➜ ') if (gitBlame && !gitBlame.stderr) { const parsedBlame = gitBlame.stdout.match(/^(?[0-9a-f]+)\s+\((?.+)(?\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [+-]\d{4}\s+)(?\d+)\) ?(?.*)$/m) errorText += `\n\n${parsedBlame.groups.USER.trim()}` errorText += `\ncommit: ${parsedBlame.groups.SHA}` errorText += `\n\n${parsedBlame.groups.code}` } errorText += `\n\n\n${escapeHTML(error.stack)}` if (error.description && error.description.includes('timeout')) return if (!ctx.config) return console.error(errorText) await ctx.telegram.sendMessage(ctx.config.logChatId, errorText, { parse_mode: 'HTML' }).catch((error) => { console.error('send log error:', error) }) if (ctx?.chat?.type === 'private') { await ctx.replyWithHTML((` ΠŸΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»Π° нСизвСстная ошибка. ВСроятно, ΠΎ Π½Π΅ΠΉ ΡƒΠΆΠ΅ извСстно администратору, Π½ΠΎ Π²Ρ‹ всё Ρ€Π°Π²Π½ΠΎ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ ΡΠΎΠΎΠ±Ρ‰ΠΈΡ‚ΡŒ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ, которая ΠΏΡ€ΠΈΠ²Π΅Π»Π° ΠΊ ошибкС => @Degradin. ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, сразу ΠΏΠΎΠ΄Ρ€ΠΎΠ±Π½ΠΎ ΠΎΠΏΠΈΡˆΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡƒ Π² ΠΎΠ΄Π½ΠΎΠΌ сообщСнии, ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€ΠΈΠ»ΠΎΠΆΠΈΡ‚ΡŒ скрин.`)).catch(() => {}) } } module.exports = async (error, ctx) => { console.error(error) errorLog(error, ctx).catch(e => { console.error('errorLog error:', e) }) }