Skip to content

Commit

Permalink
added error parsing for vercel logs
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanrahic committed May 20, 2020
1 parent 9bd29bb commit 079cee5
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions lib/plugins/output-filter/vercel-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ const initDurationRegex = /Init\sDuration:\s([\d+]+\.[\d+]+)\sms/
function formatVercelLogsOutput (context, config, eventEmitter, log, callback) {
try {
const parsedLog = parseVercelLog(log)
const error = parseVercelError(log.message)
const logToSend = { ...parsedLog, ...error }

if (config.debug) {
console.log(parsedLog)
console.log(logToSend)
}

if (parsedLog) {
callback(null, parsedLog)
if (logToSend) {
callback(null, logToSend)
}
} catch (e) {
if (config.debug) {
Expand Down Expand Up @@ -56,4 +58,45 @@ function parseVercelLog (log) {
}
}

function parseVercelError (message) {
try {
const errorLine = message
.split('\n')
.filter(line => line.length)
.filter(line => !(line.startsWith('REPORT') || line.startsWith('END') || line.startsWith('START')))
.map(line => removeErrorLineStart(line))
.shift()

const { errorType, errorMessage, stack } = tryParseErrorLine(errorLine)

return {
error: {
type: errorType,
message: errorMessage,
stack: stack.join('\n')
}
}
} catch (error) {
// return empty object it is not an error message
return {}
}
}

function removeErrorLineStart (line) {
const res = line.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/, '')
return res.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i, '')
}

function tryParseErrorLine (line) {
const json = line.match(/{[\s\S]*}/)
if (json === null) {
return line
}
try {
return JSON.parse(json[0])
} catch (e) {
return line
}
}

module.exports = formatVercelLogsOutput

0 comments on commit 079cee5

Please sign in to comment.