Refactor duplicated error handling and validation utilities#126
Refactor duplicated error handling and validation utilities#126
Conversation
Co-authored-by: synthable <374893+synthable@users.noreply.github.com>
- Extract error message extraction utility in CLI file-operations - Extract NodeJS error code checking utility in SDK - Extract validation error formatting utility in SDK loaders - Add comprehensive tests for new utility functions - Consolidate file read/write operations in CLI Co-authored-by: synthable <374893+synthable@users.noreply.github.com>
Address code review feedback by ensuring error.code is a string type Co-authored-by: synthable <374893+synthable@users.noreply.github.com>
Use explicit 'module' parameter in module-loader to match persona-loader pattern Co-authored-by: synthable <374893+synthable@users.noreply.github.com>
27a90f7 to
9f5d63c
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors duplicated error handling and validation utilities across CLI and SDK packages, extracting common patterns into shared utility functions to improve code maintainability and reduce duplication.
Key changes:
- Extracted error message formatting helper in CLI file operations
- Added type-safe file error checking utilities in SDK
- Created new loader-utils module with validation error formatting and file URL conversion helpers
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
packages/ums-sdk/src/utils/file-utils.ts |
Added isFileNotFoundError() type guard with proper validation, replacing inline ENOENT checks |
packages/ums-sdk/src/utils/file-utils.test.ts |
Added 6 comprehensive tests for new isFileNotFoundError() function |
packages/ums-sdk/src/loaders/loader-utils.ts |
New utility module with filePathToUrl() and formatValidationErrors() helpers |
packages/ums-sdk/src/loaders/loader-utils.test.ts |
Added 8 tests covering both new utility functions |
packages/ums-sdk/src/loaders/persona-loader.ts |
Updated to use shared utilities from loader-utils, removing duplicated code |
packages/ums-sdk/src/loaders/module-loader.ts |
Updated to use shared utilities from loader-utils and file-utils |
packages/ums-cli/src/utils/file-operations.ts |
Extracted getErrorMessage() and readFileWithContext() helpers to eliminate duplication |
package-lock.json |
Dependency management changes including tsx and peer dependency markers |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| throw new ModuleNotFoundError(filePath); | ||
| } | ||
| throw new ModuleLoadError( | ||
| `Failed to read file: ${error instanceof Error ? error.message : String(error)}`, |
There was a problem hiding this comment.
This error message construction still uses the inline pattern that was supposed to be refactored. Consider extracting this to a helper function like getErrorMessage() in the CLI package to maintain consistency with the stated goal of eliminating duplicated error handling patterns. The same pattern exists in several other SDK files (high-level-api.ts lines 142 and 196, module-discovery.ts line 91, standard-library.ts line 60, build-orchestrator.ts line 163).
Extracted repeated error handling, file operations, and validation patterns into shared utilities across CLI and SDK packages.
Changes
CLI File Operations (
packages/ums-cli/src/utils/file-operations.ts)getErrorMessage()to eliminate 4 instances oferror instanceof Error ? error.message : String(error)readFileWithContext()helperSDK File Utils (
packages/ums-sdk/src/utils/file-utils.ts)isFileNotFoundError()type guard with proper string validation forerror.codeSDK Loader Utils (
packages/ums-sdk/src/loaders/loader-utils.ts) - new filefilePathToUrl()- consolidatespathToFileURL().hrefpattern from both loadersformatValidationErrors()- eliminates duplicated validation error formattingModule/Persona Loaders
Before
After
Test Coverage
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.