|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import * as currentScopes from '../../../../src/currentScopes'; |
| 3 | +import * as exports from '../../../../src/exports'; |
| 4 | +import { captureError } from '../../../../src/integrations/mcp-server/errorCapture'; |
| 5 | +import { wrapMcpServerWithSentry } from '../../../../src/integrations/mcp-server'; |
| 6 | +import { createMockMcpServer } from './testUtils'; |
| 7 | + |
| 8 | +describe('MCP Server Error Capture', () => { |
| 9 | + const captureExceptionSpy = vi.spyOn(exports, 'captureException'); |
| 10 | + const getClientSpy = vi.spyOn(currentScopes, 'getClient'); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + vi.clearAllMocks(); |
| 14 | + getClientSpy.mockReturnValue({ |
| 15 | + getOptions: () => ({ sendDefaultPii: true }), |
| 16 | + } as ReturnType<typeof currentScopes.getClient>); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('captureError', () => { |
| 20 | + it('should capture errors with default error type', () => { |
| 21 | + const error = new Error('Test error'); |
| 22 | + |
| 23 | + captureError(error); |
| 24 | + |
| 25 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, { |
| 26 | + tags: { |
| 27 | + mcp_error_type: 'handler_execution', |
| 28 | + }, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should capture errors with custom error type', () => { |
| 33 | + const error = new Error('Tool execution failed'); |
| 34 | + |
| 35 | + captureError(error, 'tool_execution'); |
| 36 | + |
| 37 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, { |
| 38 | + tags: { |
| 39 | + mcp_error_type: 'tool_execution', |
| 40 | + }, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should capture transport errors', () => { |
| 45 | + const error = new Error('Connection failed'); |
| 46 | + |
| 47 | + captureError(error, 'transport'); |
| 48 | + |
| 49 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, { |
| 50 | + tags: { |
| 51 | + mcp_error_type: 'transport', |
| 52 | + }, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should capture protocol errors', () => { |
| 57 | + const error = new Error('Invalid JSON-RPC request'); |
| 58 | + |
| 59 | + captureError(error, 'protocol'); |
| 60 | + |
| 61 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, { |
| 62 | + tags: { |
| 63 | + mcp_error_type: 'protocol', |
| 64 | + }, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should capture validation errors', () => { |
| 69 | + const error = new Error('Invalid parameters'); |
| 70 | + |
| 71 | + captureError(error, 'validation'); |
| 72 | + |
| 73 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, { |
| 74 | + tags: { |
| 75 | + mcp_error_type: 'validation', |
| 76 | + }, |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should capture timeout errors', () => { |
| 81 | + const error = new Error('Operation timed out'); |
| 82 | + |
| 83 | + captureError(error, 'timeout'); |
| 84 | + |
| 85 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, { |
| 86 | + tags: { |
| 87 | + mcp_error_type: 'timeout', |
| 88 | + }, |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should not capture when no client is available', () => { |
| 93 | + getClientSpy.mockReturnValue(undefined); |
| 94 | + |
| 95 | + const error = new Error('Test error'); |
| 96 | + |
| 97 | + captureError(error, 'tool_execution'); |
| 98 | + |
| 99 | + expect(captureExceptionSpy).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle Sentry capture errors gracefully', () => { |
| 103 | + captureExceptionSpy.mockImplementation(() => { |
| 104 | + throw new Error('Sentry error'); |
| 105 | + }); |
| 106 | + |
| 107 | + const error = new Error('Test error'); |
| 108 | + |
| 109 | + // Should not throw |
| 110 | + expect(() => captureError(error, 'tool_execution')).not.toThrow(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle undefined client gracefully', () => { |
| 114 | + getClientSpy.mockReturnValue(undefined); |
| 115 | + |
| 116 | + const error = new Error('Test error'); |
| 117 | + |
| 118 | + // Should not throw and not capture |
| 119 | + expect(() => captureError(error, 'tool_execution')).not.toThrow(); |
| 120 | + expect(captureExceptionSpy).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('Error Capture Integration', () => { |
| 125 | + let mockMcpServer: ReturnType<typeof createMockMcpServer>; |
| 126 | + let wrappedMcpServer: ReturnType<typeof createMockMcpServer>; |
| 127 | + |
| 128 | + beforeEach(() => { |
| 129 | + mockMcpServer = createMockMcpServer(); |
| 130 | + wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should capture tool execution errors and continue normal flow', async () => { |
| 134 | + const toolError = new Error('Tool execution failed'); |
| 135 | + const mockToolHandler = vi.fn().mockRejectedValue(toolError); |
| 136 | + |
| 137 | + wrappedMcpServer.tool('failing-tool', mockToolHandler); |
| 138 | + |
| 139 | + await expect(mockToolHandler({ input: 'test' }, { requestId: 'req-123', sessionId: 'sess-456' })).rejects.toThrow( |
| 140 | + 'Tool execution failed', |
| 141 | + ); |
| 142 | + |
| 143 | + // The capture should be set up correctly |
| 144 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(0); // No capture yet since we didn't call the wrapped handler |
| 145 | + }); |
| 146 | + |
| 147 | + it('should handle Sentry capture errors gracefully', async () => { |
| 148 | + captureExceptionSpy.mockImplementation(() => { |
| 149 | + throw new Error('Sentry error'); |
| 150 | + }); |
| 151 | + |
| 152 | + // Test that the capture function itself doesn't throw |
| 153 | + const toolError = new Error('Tool execution failed'); |
| 154 | + const mockToolHandler = vi.fn().mockRejectedValue(toolError); |
| 155 | + |
| 156 | + wrappedMcpServer.tool('failing-tool', mockToolHandler); |
| 157 | + |
| 158 | + // The error capture should be resilient to Sentry errors |
| 159 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(0); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments