Extract error codes from CosmosDB storage to centralized errorHelper with tokenized helplinks#773
Conversation
Co-authored-by: cleemullins <1165321+cleemullins@users.noreply.github.com>
|
@copilot Add in a set of simple unit tests. |
Co-authored-by: cleemullins <1165321+cleemullins@users.noreply.github.com>
Added comprehensive unit tests for errorHelper in commit 1f89bf7. The test suite includes 21 tests covering:
All tests pass successfully. |
There was a problem hiding this comment.
Pull Request Overview
This PR successfully extracts all error strings from the CosmosDB storage package into a centralized errorHelper.ts module, following the C# pattern from Agents-for-net. The implementation introduces structured error definitions with error codes (-100000 to -100019), descriptions, and help URLs.
Key changes:
- New
errorHelper.tswithAgentErrorDefinition,ErrorHelper(20 error definitions), andExceptionHelperclasses - Updated error handling in
cosmosDbPartitionedStorage.tsandcosmosDbKeyEscape.tsto use centralized error definitions - Comprehensive test suite with 21 unit tests covering all error helper functionality
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
src/errorHelper.ts |
New centralized error helper with error definitions, codes, and help URLs |
src/cosmosDbPartitionedStorage.ts |
Replaced hardcoded error strings with ErrorHelper definitions; enhanced throwInformativeError with error codes |
src/cosmosDbKeyEscape.ts |
Updated to use ErrorHelper for missing key parameter error |
src/index.ts |
Exported error helper classes for external consumption |
test/errorHelper.test.ts |
Comprehensive unit tests for all error helper functionality |
tsconfig.json |
Excluded test directory from compilation output |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Storage.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Storage.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…nner exception handling Co-authored-by: cleemullins <1165321+cleemullins@users.noreply.github.com>
benbrown
left a comment
There was a problem hiding this comment.
Why are we changing the tsconfig file?
…rror properties Co-authored-by: cleemullins <1165321+cleemullins@users.noreply.github.com>
|
@copilot - the generated exception message needs to be in the format: [CODE] - [message] - [helplink]. Update the ExceptionHelper class to generate the right error message. |
Co-authored-by: cleemullins <1165321+cleemullins@users.noreply.github.com>
Updated in commit 97bfdee. Exception messages are now formatted as Example output: All 16 tests updated and passing with the new message format. |
|
@copilot, refactor the Errors class and the ExceptionHelper class, so that the error number only needs to be found in a single spot in the Error definition. Having the error number it in two spots is error prone and will make maintenance harder. Tokenize the helplink to look like this: and have the ExceptionHelper class replace the errorCode with the actual error number. This should end up looking like this: If the "errorCode" isn't present in the help link, just leave the link alone and pass it through directly. For example, in this code block the error code is defined, and it's duplicated again in the helplink. This should look like: |
Co-authored-by: cleemullins <1165321+cleemullins@users.noreply.github.com>
Completed in commit df76e61. Refactored to use tokenized {errorCode} in helplinks to eliminate duplication: Changes:
Example: MissingCosmosDbStorageOptions: {
code: -100000,
description: 'CosmosDbPartitionedStorageOptions is required.',
helplink: 'https://aka.ms/M365AgentsErrorCodes/#{errorCode}'
}Generates exception with helplink: All 18 tests pass, including new tests verifying token replacement behavior. |
Plan: Extract Error Codes from CosmosDB to errorHelper.ts ✅
Summary
Successfully extracted all error codes from the
agents-hosting-storage-cosmospackage into a centralized error definitions file, following the C# implementation pattern, and added comprehensive unit tests. MovedExceptionHelperandAgentErrorDefinitionto theagents-activitypackage to enable reuse across all packages in the SDK. Simplified error definitions from a class to a const object, implemented named parameter formatting for better readability and fault tolerance, added proper TypeScript typing to eliminate warnings when accessing custom error properties, formatted exception messages to include error codes and help links, and refactored helplinks to use a tokenized format that eliminates error code duplication.Changes Made:
1. Created
agents-activity/src/exceptionHelper.tswith:AgentErrorDefinitioninterface - represents error definitions with code, description, and help linkAgentErrorinterface - extends Error with custom properties (code,helpLink,innerException) for proper TypeScript typingExceptionHelperclass - generates exceptions with error codes and help linksparams?: { [key: string]: string }T & AgentError[CODE] - [message] - [helplink](e.g.,[-100000] - CosmosDbPartitionedStorageOptions is required. - https://aka.ms/M365AgentsErrorCodes/#-100000)2. Created
agents-hosting-storage-cosmos/src/errorHelper.tswith:Errorsconst object - contains 20 error definitions (codes -100000 to -100019)AgentErrorDefinitioninterface{containerId},{databaseId},{keySuffix}) instead of indexed placeholders ({0},{1})https://aka.ms/M365AgentsErrorCodes/#{errorCode}instead of hardcoded error codescodefield, eliminating duplication and maintenance issuesexport const Errors: { [key: string]: AgentErrorDefinition } = { ... }3. Updated
cosmosDbPartitionedStorage.ts:ExceptionHelper.generateException()consistently throughout{ keySuffix: 'value' }instead of positional argumentsthrowInformativeErrormethod with runtime lookupErrorsconst object4. Updated
cosmosDbKeyEscape.ts:Errorsconst object5. Updated package exports:
agents-activity/src/index.ts- exports AgentError, AgentErrorDefinition and ExceptionHelperagents-hosting-storage-cosmos/src/index.ts- exports Errors and re-exports AgentError, AgentErrorDefinition, ExceptionHelper from agents-activity6. Created comprehensive unit tests:
agents-activity/test/exceptionHelper.test.ts(11 tests) - tests for AgentErrorDefinition interface and ExceptionHelper classas anyor@ts-expect-errorcomments needed[CODE] - [message] - [helplink]agents-hosting-storage-cosmos/test/errorHelper.test.ts(7 tests) - tests for Errors object definitions (uniqueness, format, range)7. Code Review Improvements:
&&check)Named Parameters Benefits:
{ containerId: 'myContainer', partitionKeyPath: '/path' }vs'myContainer', '/path'TypeScript Benefits:
AgentErrorinterfacecode,helpLink, andinnerExceptionas anyor@ts-expect-errorException Message Format Benefits:
[CODE] - [message] - [helplink]pattern is consistent across all exceptionsTokenized Helplink Benefits:
codefield){errorCode}tokencodefield and helplink URL{errorCode})Error Code Range:
Architecture:
ExceptionHelper,AgentErrorinterface, andAgentErrorDefinitioninterface for use across all packagesErrorsconst object with 20 CosmosDB error definitions using tokenized helplinksVerification:
✅ Build successful
✅ Linting passed
✅ Manual testing verified
✅ Security scan (CodeQL) - no issues found
✅ Package exports validated
✅ 18 unit tests added - all passing (11 in agents-activity, 7 in agents-hosting-storage-cosmos)
✅ Code review feedback addressed
✅ ExceptionHelper moved to agents-activity for cross-package reuse
✅ Error definitions simplified from class to const object
✅ Message formatting uses named parameters for better readability and fault tolerance
✅ No TypeScript warnings on custom error properties
✅ Exception messages formatted with error code and help link
✅ Helplinks use {errorCode} token to eliminate duplication
All changes follow the C# pattern from the Agents-for-net repository.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.