Skip to content

Commit 5d7e521

Browse files
committed
refactor(errors): add errorMessage helper + replace 38 call sites
Adds a tiny helper that unwraps `unknown` thrown values into a readable message string, with a stable 'Unknown error' fallback for nullish / empty-message / undefined cases. Lives in two places because src/ and scripts/ are separate graphs: - src/error.ts — exported alongside PurlError + PurlInjectionError - scripts/utils/error-message.mts — mirror for build tooling Migrates every occurrence of the `e instanceof Error ? e.message : String(e)` pattern to `errorMessage(e)`. Covers: - 16 src/purl-types/ files (exists-check catch blocks) - 20 scripts/*.mts + scripts/validate/*.mts files (CLI + validator error reporting) - A 'reason ?? ""' fallback in scripts/test.mts and an ad-hoc `err ?? 'unknown'` in scripts/tour.mts — both subsumed by the helper's 'Unknown error' fallback Tests for the helper in test/purl-edge-cases.test.mts cover Error, Error subclass (TypeError, PurlError), empty-message Error, null, undefined, empty string, string, number, object. All 1783 tests pass; 100% code coverage maintained; pnpm check / lint / type / build / tour:build all green.
1 parent 4f58387 commit 5d7e521

40 files changed

Lines changed: 173 additions & 69 deletions

scripts/build.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { Logger } from '@socketsecurity/lib/logger'
1818
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1919
import { printFooter } from '@socketsecurity/lib/stdio/footer'
2020
import { printHeader } from '@socketsecurity/lib/stdio/header'
21+
import { errorMessage } from './utils/error-message.mts'
2122

2223
const logger: Logger = getDefaultLogger()
2324

@@ -87,7 +88,7 @@ const rootPath = path.resolve(
8788
)
8889

8990
function getErrorMessage(error: unknown): string {
90-
return error instanceof Error ? error.message : String(error)
91+
return errorMessage(error)
9192
}
9293

9394
function getLogLevel(quiet: boolean, verbose: boolean): LogLevel {

scripts/bump.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { SpawnOptions, SpawnResult } from '@socketsecurity/lib/spawn'
2020
import { spawn } from '@socketsecurity/lib/spawn'
2121
import { printFooter } from '@socketsecurity/lib/stdio/footer'
2222
import { printHeader } from '@socketsecurity/lib/stdio/header'
23+
import { errorMessage } from './utils/error-message.mts'
2324

2425
type CommandResult = {
2526
exitCode: number
@@ -925,7 +926,7 @@ async function main(): Promise<void> {
925926

926927
process.exitCode = 0
927928
} catch (e) {
928-
const message = e instanceof Error ? e.message : String(e)
929+
const message = errorMessage(e)
929930
logger.error(`Version bump failed: ${message}`)
930931
process.exitCode = 1
931932
}

scripts/check.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { printFooter } from '@socketsecurity/lib/stdio/footer'
1212
import { printHeader } from '@socketsecurity/lib/stdio/header'
1313

1414
import { runCommandQuiet } from './utils/run-command.mts'
15+
import { errorMessage } from './utils/error-message.mts'
1516

1617
type CheckRunOptions = {
1718
all?: boolean
@@ -404,9 +405,7 @@ async function main(): Promise<void> {
404405
printFooter()
405406
}
406407
} catch (error) {
407-
logger.error(
408-
`Check runner failed: ${error instanceof Error ? error.message : String(error)}`,
409-
)
408+
logger.error(`Check runner failed: ${errorMessage(error)}`)
410409
process.exitCode = 1
411410
}
412411
}

scripts/ci-validate.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getDefaultLogger } from '@socketsecurity/lib/logger'
1212
import type { SpawnResult } from '@socketsecurity/lib/spawn'
1313
import { spawn } from '@socketsecurity/lib/spawn'
1414
import { printHeader } from '@socketsecurity/lib/stdio/header'
15+
import { errorMessage } from './utils/error-message.mts'
1516

1617
const logger: Logger = getDefaultLogger()
1718

@@ -76,14 +77,14 @@ async function main(): Promise<void> {
7677

7778
logger.success('CI validation completed successfully!')
7879
} catch (e) {
79-
const message = e instanceof Error ? e.message : String(e)
80+
const message = errorMessage(e)
8081
logger.error(`CI validation failed: ${message}`)
8182
process.exitCode = 1
8283
}
8384
}
8485

8586
main().catch((e: unknown) => {
86-
const message = e instanceof Error ? e.message : String(e)
87+
const message = errorMessage(e)
8788
logger.error(`CI validation crashed: ${message}`)
8889
process.exitCode = 1
8990
})

scripts/claude.mts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { parseArgs } from '@socketsecurity/lib/argv/parse'
1818
import { LOG_SYMBOLS } from '@socketsecurity/lib/logger'
1919
import type { SpawnOptions, SpawnResult } from '@socketsecurity/lib/spawn'
2020
import { spawn } from '@socketsecurity/lib/spawn'
21+
import { errorMessage } from './utils/error-message.mts'
2122

2223
type CommandResult = {
2324
exitCode: number
@@ -5095,7 +5096,7 @@ Fix all issues by making necessary file changes. Be direct, don't ask questions.
50955096
log.warn(`Claude fix exited with code ${exitCode}`)
50965097
}
50975098
} catch (e) {
5098-
const message = e instanceof Error ? e.message : String(e)
5099+
const message = errorMessage(e)
50995100
log.warn(`Claude fix error: ${message}`)
51005101
} finally {
51015102
clearInterval(progressInterval)
@@ -5430,7 +5431,7 @@ Fix the issue by making necessary file changes. Be direct, don't ask questions.`
54305431
log.warn(`Claude fix exited with code ${exitCode}`)
54315432
}
54325433
} catch (e) {
5433-
const message = e instanceof Error ? e.message : String(e)
5434+
const message = errorMessage(e)
54345435
log.warn(`Claude fix error: ${message}`)
54355436
} finally {
54365437
clearInterval(progressInterval)
@@ -5636,7 +5637,7 @@ async function runWatchMode(
56365637
log.done('No issues found')
56375638
}
56385639
} catch (e) {
5639-
const message = e instanceof Error ? e.message : String(e)
5640+
const message = errorMessage(e)
56405641
log.failed(`Error scanning ${project.name}: ${message}`)
56415642
}
56425643
},
@@ -5670,7 +5671,7 @@ async function runWatchMode(
56705671
)
56715672
}
56725673
} catch (e) {
5673-
const message = e instanceof Error ? e.message : String(e)
5674+
const message = errorMessage(e)
56745675
log.failed(`Full scan error in ${project.name}: ${message}`)
56755676
}
56765677
}
@@ -6079,7 +6080,7 @@ async function main(): Promise<void> {
60796080

60806081
process.exitCode = success ? 0 : 1
60816082
} catch (e) {
6082-
const message = e instanceof Error ? e.message : String(e)
6083+
const message = errorMessage(e)
60836084
log.error(`Operation failed: ${message}`)
60846085
process.exitCode = 1
60856086
}

scripts/clean.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { parseArgs } from '@socketsecurity/lib/argv/parse'
1616
import type { Logger } from '@socketsecurity/lib/logger'
1717
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1818
import { createSectionHeader } from '@socketsecurity/lib/stdio/header'
19+
import { errorMessage } from './utils/error-message.mts'
1920

2021
const logger: Logger = getDefaultLogger()
2122

@@ -86,7 +87,7 @@ async function cleanDirectories(
8687
} catch (e) {
8788
if (!quiet) {
8889
logger.error(`Failed to clean ${name}`)
89-
const message = e instanceof Error ? e.message : String(e)
90+
const message = errorMessage(e)
9091
console.error(message)
9192
}
9293
return 1
@@ -235,7 +236,7 @@ async function main(): Promise<void> {
235236
}
236237
}
237238
} catch (e) {
238-
const message = e instanceof Error ? e.message : String(e)
239+
const message = errorMessage(e)
239240
logger.error(`Clean runner failed: ${message}`)
240241
process.exitCode = 1
241242
}

scripts/cover.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { printHeader } from '@socketsecurity/lib/stdio/header'
1919

2020
import type { CommandResult } from './utils/run-command.mts'
2121
import { runCommandQuiet } from './utils/run-command.mts'
22+
import { errorMessage } from './utils/error-message.mts'
2223

2324
const logger: Logger = getDefaultLogger()
2425

@@ -236,7 +237,7 @@ try {
236237

237238
process.exitCode = exitCode
238239
} catch (e) {
239-
const message = e instanceof Error ? e.message : String(e)
240+
const message = errorMessage(e)
240241
logger.error(`Coverage script failed: ${message}`)
241242
process.exitCode = 1
242243
}

scripts/fix.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { Logger } from '@socketsecurity/lib/logger'
1515
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1616
import type { SpawnResult } from '@socketsecurity/lib/spawn'
1717
import { spawn } from '@socketsecurity/lib/spawn'
18+
import { errorMessage } from './utils/error-message.mts'
1819

1920
const WIN32 = process.platform === 'win32'
2021
const logger: Logger = getDefaultLogger()
@@ -25,7 +26,7 @@ type RunOptions = {
2526
}
2627

2728
function getErrorMessage(error: unknown): string {
28-
return error instanceof Error ? error.message : String(error)
29+
return errorMessage(error)
2930
}
3031

3132
async function run(

scripts/lint.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getDefaultLogger } from '@socketsecurity/lib/logger'
1616
import { printHeader } from '@socketsecurity/lib/stdio/header'
1717

1818
import { runCommandQuiet } from './utils/run-command.mts'
19+
import { errorMessage } from './utils/error-message.mts'
1920

2021
type LintResult = {
2122
exitCode: number
@@ -463,9 +464,7 @@ async function main(): Promise<void> {
463464
}
464465
}
465466
} catch (error) {
466-
logger.error(
467-
`Lint runner failed: ${error instanceof Error ? error.message : String(error)}`,
468-
)
467+
logger.error(`Lint runner failed: ${errorMessage(error)}`)
469468
process.exitCode = 1
470469
}
471470
}

scripts/publish.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { spawn } from '@socketsecurity/lib/spawn'
2222
import { printFooter } from '@socketsecurity/lib/stdio/footer'
2323
import { printHeader } from '@socketsecurity/lib/stdio/header'
2424

25+
import { errorMessage } from './utils/error-message.mts'
26+
2527
const logger: Logger = getDefaultLogger()
2628

2729
type CommandResult = {
@@ -474,7 +476,7 @@ async function main(): Promise<void> {
474476
})
475477
process.exitCode = 0
476478
} catch (e) {
477-
const message = e instanceof Error ? e.message : String(e)
479+
const message = errorMessage(e)
478480
logger.error(`Publish runner failed: ${message}`)
479481
process.exitCode = 1
480482
}

0 commit comments

Comments
 (0)