|  | 
|  | 1 | +import { describe, test, expect } from '@jest/globals'; | 
|  | 2 | +import { | 
|  | 3 | +  MCP_ERROR_CODES, | 
|  | 4 | +  formatMCPError, | 
|  | 5 | +  createToolErrorResponse, | 
|  | 6 | +  createHTTPErrorResponse, | 
|  | 7 | +  ValidationError, | 
|  | 8 | +  AuthenticationError, | 
|  | 9 | +  CalDAVError, | 
|  | 10 | +  CardDAVError | 
|  | 11 | +} from '../src/error-handler.js'; | 
|  | 12 | + | 
|  | 13 | +describe('Error Handler Module', () => { | 
|  | 14 | +  describe('MCP_ERROR_CODES', () => { | 
|  | 15 | +    test('should have standard JSON-RPC error codes', () => { | 
|  | 16 | +      expect(MCP_ERROR_CODES.PARSE_ERROR).toBe(-32700); | 
|  | 17 | +      expect(MCP_ERROR_CODES.INVALID_REQUEST).toBe(-32600); | 
|  | 18 | +      expect(MCP_ERROR_CODES.METHOD_NOT_FOUND).toBe(-32601); | 
|  | 19 | +      expect(MCP_ERROR_CODES.INVALID_PARAMS).toBe(-32602); | 
|  | 20 | +      expect(MCP_ERROR_CODES.INTERNAL_ERROR).toBe(-32603); | 
|  | 21 | +    }); | 
|  | 22 | + | 
|  | 23 | +    test('should have custom application error codes', () => { | 
|  | 24 | +      expect(MCP_ERROR_CODES.CALDAV_ERROR).toBe(-32000); | 
|  | 25 | +      expect(MCP_ERROR_CODES.CARDDAV_ERROR).toBe(-32001); | 
|  | 26 | +      expect(MCP_ERROR_CODES.VALIDATION_ERROR).toBe(-32002); | 
|  | 27 | +      expect(MCP_ERROR_CODES.AUTH_ERROR).toBe(-32003); | 
|  | 28 | +    }); | 
|  | 29 | +  }); | 
|  | 30 | + | 
|  | 31 | +  describe('Custom Error Classes', () => { | 
|  | 32 | +    test('ValidationError should have correct properties', () => { | 
|  | 33 | +      const error = new ValidationError('Test validation error', { field: 'test' }); | 
|  | 34 | +      expect(error.name).toBe('ValidationError'); | 
|  | 35 | +      expect(error.code).toBe(MCP_ERROR_CODES.VALIDATION_ERROR); | 
|  | 36 | +      expect(error.message).toBe('Test validation error'); | 
|  | 37 | +      expect(error.details).toEqual({ field: 'test' }); | 
|  | 38 | +    }); | 
|  | 39 | + | 
|  | 40 | +    test('AuthenticationError should have correct properties', () => { | 
|  | 41 | +      const error = new AuthenticationError('Unauthorized'); | 
|  | 42 | +      expect(error.name).toBe('AuthenticationError'); | 
|  | 43 | +      expect(error.code).toBe(MCP_ERROR_CODES.AUTH_ERROR); | 
|  | 44 | +    }); | 
|  | 45 | + | 
|  | 46 | +    test('CalDAVError should have correct properties', () => { | 
|  | 47 | +      const error = new CalDAVError('CalDAV connection failed'); | 
|  | 48 | +      expect(error.name).toBe('CalDAVError'); | 
|  | 49 | +      expect(error.code).toBe(MCP_ERROR_CODES.CALDAV_ERROR); | 
|  | 50 | +    }); | 
|  | 51 | + | 
|  | 52 | +    test('CardDAVError should have correct properties', () => { | 
|  | 53 | +      const error = new CardDAVError('CardDAV connection failed'); | 
|  | 54 | +      expect(error.name).toBe('CardDAVError'); | 
|  | 55 | +      expect(error.code).toBe(MCP_ERROR_CODES.CARDDAV_ERROR); | 
|  | 56 | +    }); | 
|  | 57 | +  }); | 
|  | 58 | + | 
|  | 59 | +  describe('formatMCPError', () => { | 
|  | 60 | +    test('should format error with explicit code', () => { | 
|  | 61 | +      const error = new Error('Test error'); | 
|  | 62 | +      error.code = MCP_ERROR_CODES.INVALID_REQUEST; | 
|  | 63 | + | 
|  | 64 | +      const formatted = formatMCPError(error); | 
|  | 65 | + | 
|  | 66 | +      expect(formatted.code).toBe(MCP_ERROR_CODES.INVALID_REQUEST); | 
|  | 67 | +      expect(formatted.message).toBe('Test error'); | 
|  | 68 | +      expect(formatted.data.type).toBe('Error'); | 
|  | 69 | +    }); | 
|  | 70 | + | 
|  | 71 | +    test('should include stack trace when requested', () => { | 
|  | 72 | +      const error = new Error('Test error'); | 
|  | 73 | + | 
|  | 74 | +      const formatted = formatMCPError(error, true); | 
|  | 75 | + | 
|  | 76 | +      expect(formatted.data.stack).toBeDefined(); | 
|  | 77 | +      expect(typeof formatted.data.stack).toBe('string'); | 
|  | 78 | +    }); | 
|  | 79 | + | 
|  | 80 | +    test('should not include stack trace by default', () => { | 
|  | 81 | +      const error = new Error('Test error'); | 
|  | 82 | + | 
|  | 83 | +      const formatted = formatMCPError(error, false); | 
|  | 84 | + | 
|  | 85 | +      expect(formatted.data.stack).toBeUndefined(); | 
|  | 86 | +    }); | 
|  | 87 | + | 
|  | 88 | +    test('should detect error type from message', () => { | 
|  | 89 | +      const error = new Error('caldav connection failed'); | 
|  | 90 | + | 
|  | 91 | +      const formatted = formatMCPError(error); | 
|  | 92 | + | 
|  | 93 | +      expect(formatted.code).toBe(MCP_ERROR_CODES.CALDAV_ERROR); | 
|  | 94 | +    }); | 
|  | 95 | +  }); | 
|  | 96 | + | 
|  | 97 | +  describe('createToolErrorResponse', () => { | 
|  | 98 | +    test('should create MCP-compliant error response', () => { | 
|  | 99 | +      const error = new ValidationError('Invalid input'); | 
|  | 100 | + | 
|  | 101 | +      const response = createToolErrorResponse(error); | 
|  | 102 | + | 
|  | 103 | +      expect(response.isError).toBe(true); | 
|  | 104 | +      expect(response.content).toHaveLength(1); | 
|  | 105 | +      expect(response.content[0].type).toBe('text'); | 
|  | 106 | + | 
|  | 107 | +      const parsed = JSON.parse(response.content[0].text); | 
|  | 108 | +      expect(parsed.code).toBe(MCP_ERROR_CODES.VALIDATION_ERROR); | 
|  | 109 | +      expect(parsed.message).toBe('Invalid input'); | 
|  | 110 | +    }); | 
|  | 111 | +  }); | 
|  | 112 | + | 
|  | 113 | +  describe('createHTTPErrorResponse', () => { | 
|  | 114 | +    test('should map validation error to 400', () => { | 
|  | 115 | +      const error = new ValidationError('Invalid input'); | 
|  | 116 | + | 
|  | 117 | +      const response = createHTTPErrorResponse(error); | 
|  | 118 | + | 
|  | 119 | +      expect(response.statusCode).toBe(400); | 
|  | 120 | +      expect(response.body.error).toBe('Invalid input'); | 
|  | 121 | +      expect(response.body.code).toBe(MCP_ERROR_CODES.VALIDATION_ERROR); | 
|  | 122 | +    }); | 
|  | 123 | + | 
|  | 124 | +    test('should map auth error to 401', () => { | 
|  | 125 | +      const error = new AuthenticationError('Unauthorized'); | 
|  | 126 | + | 
|  | 127 | +      const response = createHTTPErrorResponse(error); | 
|  | 128 | + | 
|  | 129 | +      expect(response.statusCode).toBe(401); | 
|  | 130 | +    }); | 
|  | 131 | + | 
|  | 132 | +    test('should map method not found to 404', () => { | 
|  | 133 | +      const error = new Error('Tool not found'); | 
|  | 134 | +      error.code = MCP_ERROR_CODES.METHOD_NOT_FOUND; | 
|  | 135 | + | 
|  | 136 | +      const response = createHTTPErrorResponse(error); | 
|  | 137 | + | 
|  | 138 | +      expect(response.statusCode).toBe(404); | 
|  | 139 | +    }); | 
|  | 140 | + | 
|  | 141 | +    test('should use custom status code if provided', () => { | 
|  | 142 | +      const error = new Error('Custom error'); | 
|  | 143 | + | 
|  | 144 | +      const response = createHTTPErrorResponse(error, 418); | 
|  | 145 | + | 
|  | 146 | +      expect(response.statusCode).toBe(418); | 
|  | 147 | +    }); | 
|  | 148 | +  }); | 
|  | 149 | +}); | 
0 commit comments