Skip to content

Commit

Permalink
Improve error wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 23, 2022
1 parent 09cf169 commit 2f77932
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
13 changes: 8 additions & 5 deletions src/history/data/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pathExists } from 'path-exists'
import { isDirectory, isFile } from 'path-type'

import { UserError } from '../../error/main.js'
import { wrapError } from '../../error/wrap.js'

// Find all filenames of the history directory
export const listFilenames = async function (historyDir) {
Expand All @@ -15,7 +16,7 @@ export const readRawResult = async function (path) {
try {
return await fs.readFile(path, 'utf8')
} catch (error) {
throw new UserError(`History file could not be read: ${error.message}`)
throw wrapError(error, 'History file could not be read:', UserError)
}
}

Expand All @@ -24,7 +25,7 @@ export const writeRawResult = async function (path, rawResultStr) {
try {
return await fs.writeFile(path, rawResultStr)
} catch (error) {
throw new UserError(`History file could not be written: ${error.message}`)
throw wrapError(error, 'History file could not be written:', UserError)
}
}

Expand All @@ -33,7 +34,7 @@ export const deleteRawResult = async function (path) {
try {
await fs.unlink(path)
} catch (error) {
throw new UserError(`History file could not be deleted: ${error.message}`)
throw wrapError(error, 'History file could not be deleted:', UserError)
}
}

Expand All @@ -51,8 +52,10 @@ export const ensureHistoryDir = async function (historyDir) {
try {
await fs.mkdir(historyDir, { recursive: true })
} catch (error) {
throw new UserError(
`Could not create history directory "${historyDir}"\n${error.message}`,
throw wrapError(
error,
`Could not create history directory "${historyDir}"\n`,
UserError,
)
}
}
Expand Down
16 changes: 5 additions & 11 deletions src/history/delta/error.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { findFormat } from './formats/main.js'

// Enhances delta error messages
export const addDeltaError = function (error, { type, delta, name }) {
const deltaError = getDeltaError({ type, delta, name })
error.message = `${deltaError} ${error.message}`
return error
}

export const getDeltaError = function ({ type, delta, name }) {
const { message } = findFormat(type)
const deltaProp = getDeltaProp(delta, name)
return `${deltaProp} (${message})`
const typeMessage = getDeltaTypeMessage(type)
return `Configuration property "${name}" with value "${delta}" (${typeMessage})`
}

export const getDeltaProp = function (delta, name) {
return `"${name}" configuration property "${delta}"`
export const getDeltaTypeMessage = function (type) {
const { message } = findFormat(type)
return message
}
5 changes: 3 additions & 2 deletions src/history/delta/find.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UserError } from '../../error/main.js'
import { wrapError } from '../../error/wrap.js'

import { getDeltaError, addDeltaError } from './error.js'
import { getDeltaError } from './error.js'
import { findFormat } from './formats/main.js'

// We use deltas to locate specific rawResults in the history for either:
Expand Down Expand Up @@ -105,6 +106,6 @@ const findByDelta = async function (
try {
return await format.find(metadataGroups, value, cwd)
} catch (error) {
throw addDeltaError(error, { type, delta, name })
throw wrapError(error, getDeltaError({ type, delta, name }))
}
}
17 changes: 7 additions & 10 deletions src/history/delta/transform.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import { UserError } from '../../error/main.js'
import { wrapError } from '../../error/wrap.js'
import { findValue } from '../../utils/find.js'

import { getDeltaProp, addDeltaError } from './error.js'
import { getDeltaTypeMessage } from './error.js'
import { FORMATS } from './formats/main.js'

// Several configuration properties targets a previous rawResult using a delta,
// which can an integer, "first", date/time/duration, rawResult.id or git
// reference.
export const transformDelta = function (delta, { name }) {
if (delta === '') {
const deltaProp = getDeltaProp(delta, name)
throw new UserError(`${deltaProp} must not be an empty string`)
throw new UserError('must not be an empty string.')
}

const deltaReturn = findValue(FORMATS, (format) =>
parseDelta({ format, delta, name }),
)
const deltaReturn = findValue(FORMATS, (format) => parseDelta(format, delta))

if (deltaReturn === undefined) {
const deltaProp = getDeltaProp(delta, name)
throw new UserError(
`${deltaProp} must be an integer, "first", a date/time/duration, a result id or a git commit/tag/branch.`,
'must be an integer, "first", a date/time/duration, a result id or a git commit/tag/branch.',
)
}

const { type, value } = deltaReturn
return { type, value, delta, name }
}

const parseDelta = function ({ format: { parse, type }, delta, name }) {
const parseDelta = function ({ parse, type }, delta) {
try {
const value = parse(delta)
return value === undefined ? undefined : { type, value }
} catch (error) {
throw addDeltaError(error, { type, delta, name })
throw wrapError(error, getDeltaTypeMessage(type))
}
}

0 comments on commit 2f77932

Please sign in to comment.