Skip to content

Refactor duplicated error handling and validation utilities#126

Open
Copilot wants to merge 4 commits intodevelopfrom
copilot/refactor-duplicate-code
Open

Refactor duplicated error handling and validation utilities#126
Copilot wants to merge 4 commits intodevelopfrom
copilot/refactor-duplicate-code

Conversation

Copy link

Copilot AI commented Dec 20, 2025

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)

  • Extracted getErrorMessage() to eliminate 4 instances of error instanceof Error ? error.message : String(error)
  • Consolidated file reading via readFileWithContext() helper

SDK File Utils (packages/ums-sdk/src/utils/file-utils.ts)

  • Added isFileNotFoundError() type guard with proper string validation for error.code
  • Replaced inline ENOENT checking in 2 locations

SDK Loader Utils (packages/ums-sdk/src/loaders/loader-utils.ts) - new file

  • filePathToUrl() - consolidates pathToFileURL().href pattern from both loaders
  • formatValidationErrors() - eliminates duplicated validation error formatting

Module/Persona Loaders

  • Updated to use shared utilities from loader-utils

Before

// Repeated in module-loader.ts and persona-loader.ts
const fileUrl = pathToFileURL(filePath).href;

const errorMessages = validation.errors
  .map(e => `${e.path ?? 'module'}: ${e.message}`)
  .join('; ');

After

import { filePathToUrl, formatValidationErrors } from './loader-utils.js';

const fileUrl = filePathToUrl(filePath);
const errorMessages = formatValidationErrors(validation, 'module');

Test Coverage

  • Added 8 tests for loader-utils
  • Added 6 tests for file-utils enhancements
  • All existing 486 tests pass
Original prompt

Find and refactor duplicated code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Refactor duplicated code for improved maintainability Refactor duplicated error handling and validation utilities Dec 20, 2025
Copilot AI requested a review from synthable December 20, 2025 15:33
Copilot AI and others added 4 commits December 21, 2025 06:49
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>
@synthable synthable force-pushed the copilot/refactor-duplicate-code branch from 27a90f7 to 9f5d63c Compare December 21, 2025 14:49
@synthable synthable marked this pull request as ready for review December 21, 2025 14:49
Copilot AI review requested due to automatic review settings December 21, 2025 14:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)}`,
Copy link

Copilot AI Dec 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants