Skip to content

Refactor error serialization #1259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions packages/build/src/error/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ const { getBuildCommandDescription } = require('../log/description')

// Retrieve an error's location to print in logs.
// Each error type has its own logic (or none if there's no location to print).
const getLocationBlock = function({ stack, location, getLocation }) {
const getLocationInfo = function({ stack, location, getLocation }) {
// No location to print
if (getLocation === undefined && stack === undefined) {
return
}

const locationString = serializeLocation({ stack, location, getLocation })
return { name: 'Error location', value: locationString }
}

const serializeLocation = function({ stack, location, getLocation }) {
// The location is only the stack trace
if (getLocation === undefined) {
return stack
Expand Down Expand Up @@ -52,7 +47,7 @@ const getApiLocation = function({ endpoint, parameters }) {
}

module.exports = {
getLocationBlock,
getLocationInfo,
getBuildCommandLocation,
getBuildFailLocation,
getApiLocation,
Expand Down
70 changes: 70 additions & 0 deletions packages/build/src/error/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { getErrorInfo } = require('./info')
const { getLocationInfo } = require('./location')
const { getPluginInfo } = require('./plugin')
const { getErrorProps } = require('./properties')
const { getStackInfo } = require('./stack')
const { getTypeInfo } = require('./type')

// Parse all error information into a normalized sets of properties
const parseError = function(error) {
const {
message,
stack,
errorProps,
errorInfo,
errorInfo: { location = {}, plugin = {} },
header,
isSuccess,
stackType,
getLocation,
showErrorProps,
rawStack,
} = parseErrorInfo(error)

const headerA = getHeader(header, errorInfo)

const { message: messageA, stack: stackA } = getStackInfo({ message, stack, stackType, rawStack, isSuccess })

const pluginInfo = getPluginInfo(plugin, location)
const locationInfo = getLocationInfo({ stack: stackA, location, getLocation })
const errorPropsA = getErrorProps(errorProps, showErrorProps)
return { header: headerA, message: messageA, pluginInfo, locationInfo, errorProps: errorPropsA, isSuccess }
}

// Parse error instance into all the basic properties containing information
const parseErrorInfo = function(error) {
const { message, stack, ...errorProps } = normalizeError(error)
const { header, isSuccess, stackType, getLocation, showErrorProps, rawStack } = getTypeInfo(errorProps)
const errorInfo = getErrorInfo(errorProps)
return {
message,
stack,
errorProps,
errorInfo,
header,
isSuccess,
stackType,
getLocation,
showErrorProps,
rawStack,
}
}

const normalizeError = function(error) {
if (error instanceof Error) {
return error
}

return new Error(String(error))
}

// Retrieve header to print in logs
const getHeader = function(header, errorInfo) {
if (typeof header !== 'function') {
return header
}

return header(errorInfo)
}

module.exports = { parseError }
10 changes: 2 additions & 8 deletions packages/build/src/error/plugin.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// Retrieve plugin's package.json details to include in error messages.
// Please note `packageJson` has been normalized by `normalize-package-data`.
const getPluginBlock = function({ packageJson = {} }, { package }) {
const getPluginInfo = function({ packageJson = {} }, { package }) {
if (Object.keys(packageJson).length === 0) {
return
}

const fields = serializeFields(packageJson, package)
return { name: 'Plugin details', value: fields }
}

// Iterate over a series of package.json fields, serialize each then join them
const serializeFields = function(packageJson, package) {
return Object.entries(FIELDS)
.map(([name, getField]) => serializeField({ name, getField, packageJson, package }))
.filter(Boolean)
Expand Down Expand Up @@ -67,4 +61,4 @@ const FIELDS = {
'Report issues': getIssuesLink,
}

module.exports = { getPluginBlock }
module.exports = { getPluginInfo }
21 changes: 21 additions & 0 deletions packages/build/src/error/properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { inspect } = require('util')

const { omit } = require('../utils/omit')

const { INFO_SYM } = require('./info')

// In uncaught exceptions, print error static properties
const getErrorProps = function(errorProps, showErrorProps) {
const errorPropsA = omit(errorProps, CLEANED_ERROR_PROPS)

if (!showErrorProps || Object.keys(errorPropsA).length === 0) {
return
}

return inspect(errorPropsA)
}

// Remove error static properties that should not be logged
const CLEANED_ERROR_PROPS = [INFO_SYM, 'requireStack']

module.exports = { getErrorProps }
87 changes: 0 additions & 87 deletions packages/build/src/error/serialize.js

This file was deleted.

43 changes: 43 additions & 0 deletions packages/build/src/error/serialize_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { THEME } = require('../log/theme')

const { parseError } = require('./parse')

// Serialize an error object into a header|body string to print in logs
const serializeLogError = function(error) {
const { header, message, pluginInfo, locationInfo, errorProps, isSuccess } = parseError(error)
const body = getBody({ message, pluginInfo, locationInfo, errorProps, isSuccess })
return { header, body, isSuccess }
}

const getBody = function({ message, pluginInfo, locationInfo, errorProps, isSuccess }) {
if (isSuccess) {
return message
}

return Object.entries({
message,
pluginInfo,
locationInfo,
errorProps,
})
.filter(blockHasValue)
.map(serializeBlock)
.join('\n\n')
}

const blockHasValue = function([, value]) {
return value !== undefined
}

const serializeBlock = function([key, value]) {
return `${THEME.errorSubHeader(LOG_BLOCK_NAMES[key])}\n${value}`
}

const LOG_BLOCK_NAMES = {
message: 'Error message',
pluginInfo: 'Plugin details',
locationInfo: 'Error location',
errorProps: 'Error properties',
}

module.exports = { serializeLogError }
7 changes: 5 additions & 2 deletions packages/build/src/error/stack.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { cleanStacks } = require('./clean_stack')

// Retrieve the stack trace
const getStackInfo = function({ message, stack, stackType, rawStack }) {
const getStackInfo = function({ message, stack, stackType, rawStack, isSuccess }) {
const { message: messageA, stack: stackA } = splitStackInfo({ message, stack, stackType })
const messageB = isSuccess ? messageA.replace(SUCCESS_ERROR_NAME, '') : messageA
const stackB = cleanStacks(stackA, rawStack)
return { message: messageA, stack: stackB }
return { message: messageB, stack: stackB }
}

const splitStackInfo = function({ message, stack, stackType }) {
Expand Down Expand Up @@ -39,4 +40,6 @@ const isStackTrace = function(line) {
return line.trim().startsWith('at ')
}

const SUCCESS_ERROR_NAME = 'Error: '

module.exports = { getStackInfo }
6 changes: 3 additions & 3 deletions packages/build/src/log/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { arrowDown } = require('figures')
const prettyMs = require('pretty-ms')

const { name, version } = require('../../package.json')
const { serializeError } = require('../error/serialize')
const { serializeLogError } = require('../error/serialize_log')
const { omit } = require('../utils/omit')

const { getCommandDescription, getBuildCommandDescription } = require('./description')
Expand Down Expand Up @@ -239,13 +239,13 @@ const logCacheDir = function(path) {
}

const logPluginError = function(error) {
const { header, body } = serializeError(error)
const { header, body } = serializeLogError(error)
logErrorHeader(header)
logMessage(`\n${body}`)
}

const logBuildError = function(error) {
const { header, body, isSuccess } = serializeError(error)
const { header, body, isSuccess } = serializeLogError(error)
const logFunction = isSuccess ? logHeader : logErrorHeader
logFunction(header)
logMessage(`\n${body}\n`)
Expand Down