Skip to content
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
21 changes: 21 additions & 0 deletions packages/build/src/error/parse/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Ensure error is an `Error` instance
const normalizeError = function(error) {
if (error instanceof Array) {
return normalizeArray(error)
}

if (error instanceof Error && typeof error.message === 'string' && typeof error.stack === 'string') {
return error
}

return new Error(String(error))
}

// Some libraries throw arrays of Errors
const normalizeArray = function(errorArray) {
const [error, ...errors] = errorArray.map(normalizeError)
error.errors = errors
return error
}

module.exports = { normalizeError }
9 changes: 1 addition & 8 deletions packages/build/src/error/parse/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { getErrorInfo } = require('../info')
const { getTypeInfo } = require('../type')

const { getLocationInfo } = require('./location')
const { normalizeError } = require('./normalize')
const { getPluginInfo } = require('./plugin')
const { getErrorProps } = require('./properties')
const { getStackInfo } = require('./stack')
Expand Down Expand Up @@ -61,14 +62,6 @@ const parseErrorInfo = function(error) {
}
}

const normalizeError = function(error) {
if (error instanceof Error && typeof error.message === 'string' && typeof error.stack === 'string') {
return error
}

return new Error(String(error))
}

// Retrieve title to print in logs
const getTitle = function(title, errorInfo) {
if (typeof title !== 'function') {
Expand Down
4 changes: 3 additions & 1 deletion packages/build/src/plugins/child/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const logProcessErrors = require('log-process-errors')

const { errorToJson } = require('../../error/build')
const { isBuildError } = require('../../error/info')
const { normalizeError } = require('../../error/parse/normalize')
const { sendEventToParent } = require('../ipc')

// Handle any top-level error and communicate it back to parent
const handleError = async function(error) {
const errorPayload = errorToJson(error, { type: 'pluginInternal' })
const errorA = normalizeError(error)
const errorPayload = errorToJson(errorA, { type: 'pluginInternal' })
await sendEventToParent('error', errorPayload)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: test
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[plugins]]
package = "./plugin"
5 changes: 5 additions & 0 deletions packages/build/tests/error/fixtures/exception_array/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
async onPreBuild() {
throw [new Error('test'), new Error('testTwo')]
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: test
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[plugins]]
package = "./plugin"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
async onPreBuild() {
throw 'test'
},
}
127 changes: 127 additions & 0 deletions packages/build/tests/error/snapshots/tests.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -3415,6 +3415,133 @@ Generated by [AVA](https://ava.li).

## exception

> Snapshot 1

`␊
┌─────────────────────────────┐␊
│ Netlify Build │␊
└─────────────────────────────┘␊
> Version␊
@netlify/build 1.0.0␊
> Flags␊
debug: true␊
repositoryRoot: /file/path␊
> Current directory␊
/file/path␊
> Config file␊
/file/path␊
> Resolved config␊
plugins:␊
- inputs: {}␊
origin: config␊
package: /file/path␊
> Context␊
production␊
> Loading plugins␊
- /file/path from netlify.toml␊
┌─────────────────────────────────────┐␊
│ 1. onPreBuild command from /file/path │␊
└─────────────────────────────────────┘␊
┌──────────────────────────────────┐␊
│ Plugin "/file/path" internal error │␊
└──────────────────────────────────┘␊
Error message␊
Error: test␊
Plugin details␊
Package: /file/path␊
Version: 1.0.0␊
Repository: git+https://github.com/netlify/build.git␊
Report issues: https://github.com/netlify/build/issues␊
Error location␊
In "onPreBuild" event in "/file/path" from netlify.toml␊
STACK TRACE␊
Resolved config␊
plugins:␊
- inputs: {}␊
origin: config␊
package: /file/path`

## exception that are arrays

> Snapshot 1

`␊
┌─────────────────────────────┐␊
│ Netlify Build │␊
└─────────────────────────────┘␊
> Version␊
@netlify/build 1.0.0␊
> Flags␊
debug: true␊
repositoryRoot: /file/path␊
> Current directory␊
/file/path␊
> Config file␊
/file/path␊
> Resolved config␊
plugins:␊
- inputs: {}␊
origin: config␊
package: /file/path␊
> Context␊
production␊
> Loading plugins␊
- /file/path from netlify.toml␊
┌─────────────────────────────────────┐␊
│ 1. onPreBuild command from /file/path │␊
└─────────────────────────────────────┘␊
┌──────────────────────────────────┐␊
│ Plugin "/file/path" internal error │␊
└──────────────────────────────────┘␊
Error message␊
Error: test␊
Plugin details␊
Package: /file/path␊
Version: 1.0.0␊
Repository: git+https://github.com/netlify/build.git␊
Report issues: https://github.com/netlify/build/issues␊
Error location␊
In "onPreBuild" event in "/file/path" from netlify.toml␊
STACK TRACE␊
Error properties␊
{ errors: [ {} ] }␊
Resolved config␊
plugins:␊
- inputs: {}␊
origin: config␊
package: /file/path`

## exception that are strings

> Snapshot 1

`␊
Expand Down
Binary file modified packages/build/tests/error/snapshots/tests.js.snap
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/build/tests/error/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ test('exception with circular references', async t => {
await runFixture(t, 'exception_circular')
})

test('exception that are strings', async t => {
await runFixture(t, 'exception_string')
})

test('exception that are arrays', async t => {
await runFixture(t, 'exception_array')
})

test('Clean stack traces of build.command', async t => {
const { returnValue } = await runFixture(t, 'build_command', { snapshot: false, normalize: false })
const count = getStackLinesCount(returnValue)
Expand Down