Skip to content

Commit

Permalink
Add error types
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent 37692c5 commit 83d065a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
32 changes: 17 additions & 15 deletions src/config/normalize/lib/call/error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { normalizeError } from '../../../../error/utils.js'
import { wrapError } from '../../../../error/wrap.js'
import { InputError, DefinitionError, KeywordError } from '../error.js'

import {
addInputPrefix,
addDefinitionPrefix,
Expand All @@ -10,25 +14,24 @@ import {
addExampleInput,
} from './suffix.js'

// Handle errors in `keyword.test|normalize|main()` or in keyword functions
// Handle errors in `keyword.test|normalize|main()` or in keyword functions.
// We fail on the first error, as opposed to aggregating all errors
// - Otherwise, a failed property might be used by another property, which
// would also appear as failed, even if it has no issues
export const handleError = function ({ error, errorType, bugType, ...params }) {
const isValidation = isValidateError(error)
const errorA = normalizeError(error)
const isValidation = isValidateError(errorA)
const type = isValidation ? errorType : bugType
return ERROR_HANDLERS[type]({ ...params, error, isValidation })
return ERROR_HANDLERS[type]({ ...params, error: errorA, isValidation })
}

// Consumers can distinguish users errors from system bugs by checking
// the `error.validation` boolean property.
// User errors are distinguished by having error message starting with "must".
// We fail on the first error, as opposed to aggregating all errors
// - Otherwise, a failed property might be used by another property, which
// would also appear as failed, even if it has no issues
// We distinguish intentional errors from bugs by requiring messages to start
// with "must".
// We detect this using the error message instead of the error class because:
// - It is simpler for users
// - It works both on browsers and in Node.js
// - It ensures the error message looks good
const isValidateError = function (error) {
return error instanceof Error && error.message.startsWith('must')
return error.message.startsWith('must')
}

const handleInputError = function ({
Expand All @@ -40,11 +43,10 @@ const handleInputError = function ({
hasInput,
test,
}) {
error.validation = true
const errorA = addInputPrefix(error, prefix, originalName)
const errorB = addCurrentInput(errorA, input, hasInput)
const errorC = addExampleInput({ error: errorB, example, hasInput, test })
return errorC
return wrapError(errorC, '', InputError)
}

const handleDefinitionError = function ({
Expand All @@ -65,7 +67,7 @@ const handleDefinitionError = function ({
})
const errorB = addCurrentDefinition(errorA, definition)
const errorC = addExampleDefinition(errorB, exampleDefinition)
return errorC
return wrapError(errorC, '', DefinitionError)
}

const handleKeywordError = function ({
Expand All @@ -80,7 +82,7 @@ const handleKeywordError = function ({
const errorA = addKeywordPrefix({ error, prefix, originalName, keyword })
const errorB = addCurrentDefinition(errorA, definition)
const errorC = addCurrentInput(errorB, input, hasInput)
return errorC
return wrapError(errorC, '', KeywordError)
}

const ERROR_HANDLERS = {
Expand Down
3 changes: 2 additions & 1 deletion src/config/normalize/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { list } from 'wild-wild-path'

import { cleanObject } from '../../../utils/clean.js'

import { InputError } from './error.js'
import { getInfo } from './info.js'
import { applyKeywords } from './keywords/main.js'
import { normalizeRules } from './normalize.js'
Expand Down Expand Up @@ -78,7 +79,7 @@ const applyEntryRule = async function (
// When in `sort` mode, user errors are returned instead of being thrown.
// System errors are always propagated.
const handleError = function (error, soft) {
if (!soft || !error.validation) {
if (!soft || !(error instanceof InputError)) {
throw error
}
}

0 comments on commit 83d065a

Please sign in to comment.