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)
})
}