-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
136 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { createErrorType } from '../../../error/create.js' | ||
import { modernErrors } from '../../../error/modern.js' | ||
|
||
// Invalid `inputs` | ||
export const InputError = createErrorType('InputError') | ||
// Invalid `rules` or `options` | ||
export const DefinitionError = createErrorType('DefinitionError') | ||
// Bug in a keyword|plugin | ||
export const KeywordError = createErrorType('KeywordError') | ||
// Bug in the library itself | ||
export const CoreError = createErrorType('CoreError') | ||
const { InputError, DefinitionError, KeywordError, CoreError, onError } = | ||
modernErrors(['InputError', 'DefinitionError', 'KeywordError', 'CoreError']) | ||
|
||
// All error types, with first being default type | ||
export const ErrorTypes = [CoreError, InputError, DefinitionError, KeywordError] | ||
export { | ||
// Invalid `inputs` | ||
InputError, | ||
// Invalid `rules` or `options` | ||
DefinitionError, | ||
// Bug in a keyword|plugin | ||
KeywordError, | ||
// Bug in the library itself | ||
CoreError, | ||
// Top-level error handler | ||
onError, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { createErrorType } from '../../../error/create.js' | ||
import { modernErrors } from '../../../error/modern.js' | ||
|
||
// Error from the library | ||
export const CoreError = createErrorType('CoreError') | ||
// Error from the library's user, who defines available plugin types | ||
export const UserError = createErrorType('UserError') | ||
// Error from a plugin author, who defines a specific plugin | ||
export const PluginError = createErrorType('PluginError') | ||
// Error from a plugin user | ||
export const ConsumerError = createErrorType('ConsumerError') | ||
const { CoreError, UserError, PluginError, ConsumerError, onError } = | ||
modernErrors(['CoreError', 'UserError', 'PluginError', 'ConsumerError']) | ||
|
||
// All error types, with first being default type | ||
export const ErrorTypes = [CoreError, UserError, PluginError, ConsumerError] | ||
export { | ||
// Error from the library | ||
CoreError, | ||
// Error from the library's user, who defines available plugin types | ||
UserError, | ||
// Error from a plugin author, who defines a specific plugin | ||
PluginError, | ||
// Error from a plugin user | ||
ConsumerError, | ||
// Top-level error handler | ||
onError, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// eslint-disable-next-line n/file-extension-in-import, import/no-unassigned-import | ||
import 'error-cause/auto' | ||
|
||
import { createErrorType } from './create.js' | ||
import { mergeErrorCause } from './merge/main.js' | ||
import { allowErrorTypes } from './types.js' | ||
|
||
// Create error types by passing an array of error names. | ||
// Also returns an `onError(error) => error` function to use as a top-level | ||
// error handler. | ||
// Custom error `onCreate()` logic can be specified | ||
// - To make it type-specific, an object of functions should be used, then | ||
// `object[error.name]` should be used inside `onCreate()` | ||
export const modernErrors = function (errorNames, onCreate) { | ||
const ErrorTypes = Object.fromEntries( | ||
errorNames.map((errorName) => [ | ||
errorName, | ||
createErrorType(errorName, onCreate), | ||
]), | ||
) | ||
const onErrorHandler = onError.bind(undefined, Object.values(ErrorTypes)) | ||
return { ...ErrorTypes, onError: onErrorHandler } | ||
} | ||
|
||
// Error handler that normalizes an error, merge its `error.cause` and ensure | ||
// its type is among an allowed list of types. | ||
const onError = function (ErrorTypes, error) { | ||
const errorA = mergeErrorCause(error) | ||
const errorB = allowErrorTypes(errorA, ErrorTypes) | ||
return errorB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,35 @@ | ||
import { createErrorType } from '../../error/create.js' | ||
import { onError } from '../../error/handler.js' | ||
import { modernErrors } from '../../error/modern.js' | ||
|
||
// Error from the library itself | ||
export const CoreError = createErrorType('CoreError') | ||
// Could not JSON-stringify IPC payload | ||
export const IpcSerializationError = createErrorType('IpcSerializationError') | ||
// Tasks file throws when loading | ||
export const TasksLoadError = createErrorType('TasksLoadError') | ||
// Tasks file has invalid syntax, e.g. exports invalid fields | ||
export const TasksSyntaxError = createErrorType('TasksSyntaxError') | ||
// Tasks throws when running | ||
export const TasksRunError = createErrorType('TasksRunError') | ||
// Invalid runner config | ||
export const ConfigError = createErrorType('ConfigError') | ||
|
||
// All error types, with first being default type | ||
const ErrorTypes = [ | ||
const { | ||
CoreError, | ||
IpcSerializationError, | ||
TasksLoadError, | ||
TasksSyntaxError, | ||
TasksRunError, | ||
ConfigError, | ||
] | ||
onError, | ||
} = modernErrors([ | ||
'CoreError', | ||
'IpcSerializationError', | ||
'TasksLoadError', | ||
'TasksSyntaxError', | ||
'TasksRunError', | ||
'ConfigError', | ||
]) | ||
|
||
// Serialize an error to send to parent | ||
export const serializeError = function (error) { | ||
const { name, message, stack } = onError(error, ErrorTypes) | ||
return { name, message, stack } | ||
export { | ||
// Error from the library itself | ||
CoreError, | ||
// Could not JSON-stringify IPC payload | ||
IpcSerializationError, | ||
// Tasks file throws when loading | ||
TasksLoadError, | ||
// Tasks file has invalid syntax, e.g. exports invalid fields | ||
TasksSyntaxError, | ||
// Tasks throws when running | ||
TasksRunError, | ||
// Invalid runner config | ||
ConfigError, | ||
// Top-level error handler | ||
onError, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters