-
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
5 changed files
with
76 additions
and
58 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { normalizeError } from '../../../error/utils.js' | ||
import { wrapError } from '../../../error/wrap.js' | ||
import { maybeFunction } from '../../../utils/function.js' | ||
|
||
// Append `prefix` option to error message. | ||
// The space at the end of `prefix` is optional. | ||
// If `prefix` ends with `.`: | ||
// - The default prefix is prepended. | ||
// - No space is appended. | ||
// Also surround property name with quotes. | ||
export const addPropPrefix = async function (error, opts) { | ||
const prefix = await getPrefix(opts) | ||
const propName = `${prefix}${opts.name}` | ||
const propNameA = quotePropName(propName) | ||
return wrapError(error, propNameA) | ||
} | ||
|
||
// Default value for the `prefix` option | ||
export const DEFAULT_PREFIX = 'Configuration property' | ||
|
||
const getPrefix = async function (opts) { | ||
const prefix = await callPrefix(opts) | ||
|
||
if (prefix === undefined) { | ||
return `${DEFAULT_PREFIX} ` | ||
} | ||
|
||
const prefixA = String(prefix) | ||
|
||
if (prefixA.endsWith('.')) { | ||
return `${DEFAULT_PREFIX} ${prefixA}` | ||
} | ||
|
||
return shouldAppendSpace(prefixA) ? `${prefixA} ` : prefixA | ||
} | ||
|
||
const callPrefix = async function ({ prefix, ...opts }) { | ||
try { | ||
return await maybeFunction(prefix, opts) | ||
} catch (error) { | ||
const { message } = normalizeError(error) | ||
return `${message}\n` | ||
} | ||
} | ||
|
||
const shouldAppendSpace = function (prefix) { | ||
return prefix !== '' && !prefix.endsWith(' ') | ||
} | ||
|
||
const quotePropName = function (propName) { | ||
const lastSpaceIndex = propName.lastIndexOf(' ') | ||
const [firstWords, lastWord] = | ||
lastSpaceIndex === -1 | ||
? ['', propName] | ||
: [ | ||
propName.slice(0, lastSpaceIndex + 1), | ||
propName.slice(lastSpaceIndex + 1), | ||
] | ||
return `${firstWords}"${lastWord}"` | ||
} |
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
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