Skip to content

feat(errors): add structured error handling with tier awareness#75

Merged
polaz merged 16 commits intomainfrom
feat/50-structured-errors
Jan 21, 2026
Merged

feat(errors): add structured error handling with tier awareness#75
polaz merged 16 commits intomainfrom
feat/50-structured-errors

Conversation

@polaz
Copy link
Copy Markdown
Member

@polaz polaz commented Jan 20, 2026

Summary

  • Add tier-aware error handling infrastructure with tier-features.ts and error-handler.ts
  • Integrate structured error responses into MCP handlers
  • Enable LLMs to receive actionable error information for self-correction

Features

  • Tier-restricted feature detection: Automatically identifies when a feature requires Premium/Ultimate tier
  • Structured error codes: TIER_RESTRICTED, PERMISSION_DENIED, NOT_FOUND, RATE_LIMITED, SERVER_ERROR, API_ERROR
  • Actionable suggestions: Each error includes suggested_fix with guidance
  • Alternatives for tier-restricted features: Provides alternative approaches available on Free tier
  • Documentation links: Includes docs_url and upgrade_url for tier-restricted errors

Example Structured Error Response

{
  "error_code": "TIER_RESTRICTED",
  "tool": "browse_protected_branches",
  "action": "list",
  "http_status": 403,
  "tier_required": "Premium",
  "feature_name": "Protected Branches API",
  "message": "Protected Branches API requires GitLab Premium or higher",
  "suggested_fix": "Upgrade to GitLab Premium, or use one of the alternatives",
  "alternatives": [
    {
      "action": "Configure via UI",
      "description": "Use Settings > Repository > Protected branches in GitLab UI",
      "available_on": "Free"
    }
  ],
  "docs_url": "https://docs.gitlab.com/ee/api/protected_branches.html",
  "upgrade_url": "https://about.gitlab.com/pricing/"
}

Test plan

  • Unit tests for tier-features.ts (20 tests)
  • Unit tests for error-handler.ts (26 tests)
  • All 2308 existing tests pass
  • Manual test with GitLab Free tier instance

Closes #50

polaz added 2 commits January 20, 2026 23:10
Add tier-aware error handling infrastructure:
- tier-features.ts: GitLab tier feature map with Premium/Ultimate features
- error-handler.ts: Structured error types and handleGitLabError() function
- Support for TIER_RESTRICTED, PERMISSION_DENIED, NOT_FOUND, RATE_LIMITED,
  SERVER_ERROR, and API_ERROR error codes
- Alternatives suggestions for tier-restricted features
- Unit tests for both modules (46 tests)

Closes #50
Add structured error response integration:
- Import error handler utilities in handlers.ts
- Add parseGitLabApiError() to extract status codes from error messages
- Add toStructuredError() to convert errors to structured responses
- Modify catch block to return structured errors for GitLab API errors
- Fallback to original error format for non-API errors

This enables LLMs to receive actionable error information including:
- error_code (TIER_RESTRICTED, PERMISSION_DENIED, NOT_FOUND, etc.)
- suggested_fix with guidance on how to resolve the issue
- alternatives for tier-restricted features

Part of #50
@polaz polaz requested review from Copilot and removed request for Copilot January 20, 2026 21:13
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 20, 2026

📊 Test Coverage Report

Overall Coverage: 89.91%

Coverage Details

Metric Percentage
Statements 89.91%
Branches 82.89%
Functions 77.86%
Lines 90.38%

Coverage Report: View detailed coverage report

This report was generated automatically from your PR changes.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 20, 2026

Codecov Report

❌ Patch coverage is 87.66234% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/utils/error-handler.ts 89.52% 1 Missing and 10 partials ⚠️
src/handlers.ts 88.37% 2 Missing and 3 partials ⚠️
src/logger.ts 50.00% 1 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

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 adds structured error handling infrastructure with tier-awareness to help LLMs self-correct and provide actionable guidance for users on restricted GitLab tiers. The implementation includes tier feature detection, structured error response generation, and integration with MCP handlers.

Changes:

  • Add tier-features.ts with mappings of Premium/Ultimate features and their Free tier alternatives
  • Add error-handler.ts with structured error types and transformation functions
  • Integrate structured error handling into MCP tool handler in handlers.ts

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
src/utils/tier-features.ts Defines tier-restricted features map with Premium/Ultimate features, alternatives, and documentation URLs
src/utils/error-handler.ts Implements structured error types and transformation logic from GitLab API errors to actionable responses
src/handlers.ts Adds error parsing and structured error integration in the main tool handler
tests/unit/utils/tier-features.test.ts Comprehensive unit tests for tier-features.ts (20 tests covering all functions)
tests/unit/utils/error-handler.test.ts Comprehensive unit tests for error-handler.ts (26 tests covering all error types)

- Add action extraction from request.params.arguments and error cause
- Fix regex to handle wrapped errors (Failed to execute tool prefix)
- Include rawMessage context in permission denied suggested_fix
- Extract resourceId and resourceType from 404 error messages
- Add tests for parseGitLabApiError, toStructuredError, and error flows
Copy link
Copy Markdown

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

polaz added 2 commits January 21, 2026 00:08
…r tests

- Remove unused imports from handlers.test.ts
- Add 9 direct unit tests for parseGitLabApiError function
- Document TierAlternative naming convention (snake_case for JSON output)
- Use context-based regex for resourceId: first tries keyword match
  like "Project 123", then falls back to 4+ digit numbers
@polaz polaz requested a review from Copilot January 20, 2026 22:12
- Add test for 404 errors with 4+ digit ID extraction fallback
- Add test for action extraction from wrapped error cause
- Add test for StructuredToolError detection in cause chain
- Add tests for resolveRefs: $ref resolution, arrays, nested objects
- Preserve error cause when re-throwing in tool handler
- Check cause chain for StructuredToolError in toStructuredError
Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@polaz polaz requested a review from Copilot January 20, 2026 23:27
Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

- Replace non-existent tool references with actual implemented tools
- Add group_webhooks (Premium): list_webhooks:group, manage_webhook:group
- Add epics (Premium): browse_work_items:epic, manage_work_item:epic
- Add iterations (Premium): list_group_iterations
- Remove placeholder entries for unimplemented tools (protected branches,
  code owners, security dashboard, merge trains, MR approvals)
- Add NOTE comment listing planned features for future implementation
- Update tier-features and error-handler tests to match new structure
Copy link
Copy Markdown

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

polaz added 5 commits January 21, 2026 09:11
Pino-pretty uses worker threads via thread-stream which prevents
Jest from exiting properly. Disable transport when running tests
(detected via NODE_ENV=test or JEST_WORKER_ID).
…coverage test

The fs mock was too aggressive and broke pino logger which uses
sonic-boom internally. Now the mock preserves actual fs functions
for node_modules paths.
Copy link
Copy Markdown

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

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

- Remove redundant tierFeature.tier !== "Free" check (TIER_FEATURES
  map only contains Premium/Ultimate entries)
- Add security documentation for TLS certificate validation opt-in
Copy link
Copy Markdown

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

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Replace static TIER_FEATURES map with dynamic detection using
ConnectionManager.isFeatureAvailable() and getTier(). This ensures
tier restrictions are based on actual GitLab instance capabilities
detected at startup.

Key changes:
- Add parameter-aware tier detection for work item types (EPIC,
  OBJECTIVE, KEY_RESULT, REQUIREMENT) using toolArgs inspection
- Remove tier-features.ts and its static mapping approach
- Add FEATURE_METADATA map with feature info, alternatives, docs
- Update handleGitLabError signature to accept toolArgs for context
- Fix handlers.ts to pass toolArgs to error handling

This fixes the incorrect browse_work_items:epic matching where
EPIC is a work item TYPE (parameter), not an action name.
Copy link
Copy Markdown

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

Copilot reviewed 8 out of 9 changed files in this pull request and generated 1 comment.

Remove obsolete reference to deleted TierFeature interface.
Copy link
Copy Markdown

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

Copilot reviewed 8 out of 9 changed files in this pull request and generated 1 comment.

Import InternalTier from GitLabVersionDetector (lowercase) and keep
local GitLabTier for display (capitalized). Clarify the relationship
in normalizeTier function signature and comments.
Copy link
Copy Markdown

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

Copilot reviewed 8 out of 9 changed files in this pull request and generated no new comments.

@polaz polaz merged commit 2b5088c into main Jan 21, 2026
22 checks passed
@polaz polaz deleted the feat/50-structured-errors branch January 21, 2026 11:59
sw-release-bot bot pushed a commit that referenced this pull request Jan 21, 2026
## [6.18.0](v6.17.0...v6.18.0) (2026-01-21)

### Features

* **errors:** add structured error handling with tier awareness ([#75](#75)) ([2b5088c](2b5088c)), closes [#50](#50) [#50](#50)
* **mrs:** implement GitLab Suggestions API integration ([#70](#70)) ([b9ca090](b9ca090)), closes [#66](#66)
* **tiers:** add action-level tier requirements for consolidated tools ([#77](#77)) ([8e5ae0e](8e5ae0e)), closes [#48](#48)
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.

Structured error responses for action validation

3 participants