Skip to content

Commit

Permalink
Replace originalError with native cause
Browse files Browse the repository at this point in the history
  • Loading branch information
jstayton committed Aug 23, 2024
1 parent 36b8a32 commit 42382cc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bin/suri.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ try {
} catch (error) {
console.error(`Error: ${error.message}`)

if (error.originalError) {
console.error(` ↳ ${error.originalError.message}`)
if (error.cause) {
console.error(` ↳ ${error.cause.message}`)
}

process.exitCode = 1
Expand Down
14 changes: 4 additions & 10 deletions src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ class SuriError extends Error {
* Create an error for Suri.
*
* @param {string} message The description of what failed.
* @param {Error} [originalError] The original error that was caught.
* @param {Object} [options]
* @param {Error} [options.cause] The underlying cause of the error.
*/
constructor(message, originalError = null) {
super(message)

/**
* The original error that was caught.
*
* @member {Error}
*/
this.originalError = originalError
constructor(message, { cause = null } = {}) {
super(message, { cause })

Error.captureStackTrace(this, this.constructor)

Expand Down
38 changes: 20 additions & 18 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ async function loadConfig({ path }) {

try {
config = await readFile(path)
} catch (error) {
if (!isErrorFileNotExists(error)) {
throw new SuriError('Failed to load config file', error)
} catch (cause) {
if (!isErrorFileNotExists(cause)) {
throw new SuriError('Failed to load config file', { cause })
}

console.log('No config file found, using default config')
Expand All @@ -72,8 +72,8 @@ async function loadConfig({ path }) {

try {
config = JSON.parse(config)
} catch (error) {
throw new SuriError('Failed to parse config as JSON', error)
} catch (cause) {
throw new SuriError('Failed to parse config as JSON', { cause })
}

return {
Expand All @@ -98,14 +98,14 @@ async function loadLinks({ path }) {

try {
links = await readFile(path)
} catch (error) {
throw new SuriError('Failed to load links file', error)
} catch (cause) {
throw new SuriError('Failed to load links file', { cause })
}

try {
links = JSON.parse(links)
} catch (error) {
throw new SuriError('Failed to parse links as JSON', error)
} catch (cause) {
throw new SuriError('Failed to parse links as JSON', { cause })
}

return links
Expand Down Expand Up @@ -152,17 +152,19 @@ async function createLink({ linkPath, redirectURL, config, buildDirPath }) {

try {
await mkdir(linkDirPath, { recursive: true })
} catch (error) {
throw new SuriError(`Failed to create link directory: ${linkPath}`, error)
} catch (cause) {
throw new SuriError(`Failed to create link directory: ${linkPath}`, {
cause,
})
}

try {
await writeFile(
join(linkDirPath, 'index.html'),
buildLinkPage({ redirectURL, config }),
)
} catch (error) {
throw new SuriError(`Failed to create link file: ${linkPath}`, error)
} catch (cause) {
throw new SuriError(`Failed to create link file: ${linkPath}`, { cause })
}

return true
Expand Down Expand Up @@ -192,9 +194,9 @@ async function copyPublic({ path, buildDirPath }) {
preserveTimestamps: true,
recursive: true,
})
} catch (error) {
if (!isErrorFileNotExists(error)) {
throw new SuriError('Failed to copy public directory', error)
} catch (cause) {
if (!isErrorFileNotExists(cause)) {
throw new SuriError('Failed to copy public directory', { cause })
}

console.log('No public directory found, skipping')
Expand All @@ -216,8 +218,8 @@ async function copyPublic({ path, buildDirPath }) {
async function removeBuild({ path }) {
try {
return await rm(path, { recursive: true, force: true })
} catch (error) {
throw new SuriError('Failed to remove build directory', error)
} catch (cause) {
throw new SuriError('Failed to remove build directory', { cause })
}
}

Expand Down

0 comments on commit 42382cc

Please sign in to comment.