Implement comprehensive error handling and structured logging for MCP module. **Exception Hierarchy**: - Created McpException base class with JSON-RPC error mapping - Implemented 8 specific exception types (Parse, InvalidRequest, MethodNotFound, etc.) - Each exception maps to correct HTTP status code (401, 403, 404, 422, 400, 500) **Middleware**: - McpCorrelationIdMiddleware: Generates/extracts correlation ID for request tracking - McpExceptionHandlerMiddleware: Global exception handler with JSON-RPC error responses - McpLoggingMiddleware: Request/response logging with sensitive data sanitization **Serilog Integration**: - Configured structured logging with Console and File sinks - Log rotation (daily, 30-day retention) - Correlation ID enrichment in all log entries **Features**: - Correlation ID propagation across request chain - Structured logging with TenantId, UserId, ApiKeyId - Sensitive data sanitization (API keys, passwords) - Performance metrics (request duration, slow request warnings) - JSON-RPC 2.0 compliant error responses **Testing**: - 174 tests passing (all MCP module tests) - Unit tests for all exception classes - Unit tests for all middleware components - 100% coverage of error mapping and HTTP status codes **Files Added**: - 9 exception classes in Domain/Exceptions/ - 3 middleware classes in Infrastructure/Middleware/ - 4 test files with comprehensive coverage **Files Modified**: - Program.cs: Serilog configuration - McpServiceExtensions.cs: Middleware pipeline registration - JsonRpcError.cs: Added parameterless constructor for deserialization - MCP Infrastructure .csproj: Added Serilog package reference **Verification**: ✅ All 174 MCP module tests passing ✅ Build successful with no errors ✅ Exception-to-HTTP-status mapping verified ✅ Correlation ID propagation tested ✅ Sensitive data sanitization verified Story: docs/stories/sprint_5/story_5_4.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using ColaFlow.Modules.Mcp.Contracts.JsonRpc;
|
|
|
|
namespace ColaFlow.Modules.Mcp.Domain.Exceptions;
|
|
|
|
/// <summary>
|
|
/// Exception thrown when a requested resource is not found
|
|
/// Maps to JSON-RPC error code -32003 (NotFound)
|
|
/// HTTP 404 status code
|
|
/// </summary>
|
|
public class McpNotFoundException : McpException
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="McpNotFoundException"/> class
|
|
/// </summary>
|
|
/// <param name="resourceType">Type of resource (e.g., "Task", "Epic")</param>
|
|
/// <param name="resourceId">ID of the resource</param>
|
|
public McpNotFoundException(string resourceType, string resourceId)
|
|
: base(JsonRpcErrorCode.NotFound, $"{resourceType} not found: {resourceId}")
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="McpNotFoundException"/> class with custom message
|
|
/// </summary>
|
|
/// <param name="message">Error message</param>
|
|
/// <param name="errorData">Additional error data (optional)</param>
|
|
public McpNotFoundException(string message, object? errorData = null)
|
|
: base(JsonRpcErrorCode.NotFound, message, errorData)
|
|
{
|
|
}
|
|
}
|