UNPKG

bc-webclient-mcp

Version:

Model Context Protocol (MCP) server for Microsoft Dynamics 365 Business Central via WebUI protocol. Enables AI assistants to interact with BC through the web client protocol, supporting Card, List, and Document pages with full line item support and server

416 lines 14.2 kB
/** * Error Hierarchy for BC MCP Server * * Comprehensive error types following SOLID principles. * All errors are immutable and provide structured error information. */ // ============================================================================ // Base Error Class // ============================================================================ export class BCError extends Error { name; code; timestamp; context; constructor(message, code, context) { super(message); this.name = this.constructor.name; this.code = code; this.timestamp = new Date(); this.context = context; // Maintains proper stack trace for where our error was thrown (only available on V8) if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } // Set the prototype explicitly for proper instanceof checks Object.setPrototypeOf(this, new.target.prototype); } toJSON() { return { name: this.name, code: this.code, message: this.message, timestamp: this.timestamp.toISOString(), context: this.context, stack: this.stack, }; } toString() { const contextStr = this.context ? ` | Context: ${JSON.stringify(this.context)}` : ''; return `[${this.code}] ${this.name}: ${this.message}${contextStr}`; } } // ============================================================================ // Connection Errors // ============================================================================ export class ConnectionError extends BCError { constructor(message, context) { super(message, 'BC_CONNECTION_ERROR', context); } } export class AuthenticationError extends BCError { constructor(message, context) { super(message, 'BC_AUTH_ERROR', { ...context, subtype: 'authentication' }); } } export class WebSocketConnectionError extends BCError { constructor(message, context) { super(message, 'BC_WS_CONNECTION_ERROR', { ...context, subtype: 'websocket' }); } } export class SessionExpiredError extends BCError { constructor(message, context) { super(message, 'BC_SESSION_EXPIRED', { ...context, subtype: 'session_expired' }); } } export class TimeoutError extends BCError { constructor(message, context) { super(message, 'BC_TIMEOUT_ERROR', { ...context, subtype: 'timeout' }); } } export class AbortedError extends BCError { constructor(message, context) { super(message, 'BC_ABORTED_ERROR', { ...context, subtype: 'aborted' }); } } export class NetworkError extends BCError { constructor(message, context) { super(message, 'BC_NETWORK_ERROR', { ...context, subtype: 'network' }); } } // ============================================================================ // Protocol Errors // ============================================================================ export class ProtocolError extends BCError { constructor(message, context) { super(message, 'BC_PROTOCOL_ERROR', context); } } export class JsonRpcError extends BCError { rpcErrorCode; constructor(message, rpcErrorCode, context) { super(message, 'BC_JSONRPC_ERROR', { ...context, rpcErrorCode }); Object.defineProperty(this, 'rpcErrorCode', { value: rpcErrorCode, writable: false, enumerable: true, configurable: false, }); } } export class DecompressionError extends BCError { constructor(message, context) { super(message, 'BC_DECOMPRESSION_ERROR', { ...context, subtype: 'decompression' }); } } export class InvalidResponseError extends BCError { constructor(message, context) { super(message, 'BC_INVALID_RESPONSE', { ...context, subtype: 'invalid_response' }); } } // ============================================================================ // Parse Errors // ============================================================================ export class ParseError extends BCError { constructor(message, context) { super(message, 'BC_PARSE_ERROR', context); } } export class HandlerParseError extends BCError { handlerType; constructor(message, handlerType, context) { super(message, 'BC_HANDLER_PARSE_ERROR', { ...context, handlerType }); Object.defineProperty(this, 'handlerType', { value: handlerType, writable: false, enumerable: true, configurable: false, }); } } export class ControlParseError extends BCError { controlType; constructor(message, controlType, context) { super(message, 'BC_CONTROL_PARSE_ERROR', { ...context, controlType }); Object.defineProperty(this, 'controlType', { value: controlType, writable: false, enumerable: true, configurable: false, }); } } export class LogicalFormParseError extends BCError { constructor(message, context) { super(message, 'BC_LOGICAL_FORM_PARSE_ERROR', { ...context, subtype: 'logical_form' }); } } // ============================================================================ // Validation Errors // ============================================================================ export class ValidationError extends BCError { field; validationErrors; constructor(message, field, validationErrors, context) { super(message, 'BC_VALIDATION_ERROR', { ...context, field, validationErrors, }); this.field = field; this.validationErrors = validationErrors; } } export class ConfigValidationError extends BCError { field; constructor(message, field, context) { super(message, 'BC_CONFIG_VALIDATION_ERROR', { ...context, field, subtype: 'config' }); Object.defineProperty(this, 'field', { value: field, writable: false, enumerable: true, configurable: false, }); } } export class InputValidationError extends BCError { field; validationErrors; constructor(message, field, validationErrors, context) { super(message, 'BC_INPUT_VALIDATION_ERROR', { ...context, field, validationErrors, subtype: 'input', }); Object.defineProperty(this, 'field', { value: field, writable: false, enumerable: true, configurable: false, }); Object.defineProperty(this, 'validationErrors', { value: validationErrors, writable: false, enumerable: true, configurable: false, }); } } export class SchemaValidationError extends BCError { validationErrors; constructor(message, validationErrors, context) { super(message, 'BC_SCHEMA_VALIDATION_ERROR', { ...context, validationErrors, subtype: 'schema', }); Object.defineProperty(this, 'validationErrors', { value: validationErrors, writable: false, enumerable: true, configurable: false, }); } } // ============================================================================ // Business Logic Errors // ============================================================================ export class BusinessLogicError extends BCError { constructor(message, context) { super(message, 'BC_BUSINESS_LOGIC_ERROR', context); } } export class PageNotFoundError extends BCError { pageId; constructor(pageId, message, context) { super(message ?? `Page ${pageId} not found`, 'BC_PAGE_NOT_FOUND', { ...context, pageId }); Object.defineProperty(this, 'pageId', { value: pageId, writable: false, enumerable: true, configurable: false, }); } } export class ActionNotFoundError extends BCError { actionName; constructor(actionName, message, context) { super(message ?? `Action ${actionName} not found`, 'BC_ACTION_NOT_FOUND', { ...context, actionName }); Object.defineProperty(this, 'actionName', { value: actionName, writable: false, enumerable: true, configurable: false, }); } } export class FieldNotFoundError extends BCError { fieldName; constructor(fieldName, message, context) { super(message ?? `Field ${fieldName} not found`, 'BC_FIELD_NOT_FOUND', { ...context, fieldName }); Object.defineProperty(this, 'fieldName', { value: fieldName, writable: false, enumerable: true, configurable: false, }); } } export class RecordNotFoundError extends BCError { recordId; constructor(recordId, message, context) { super(message ?? `Record ${recordId} not found`, 'BC_RECORD_NOT_FOUND', { ...context, recordId }); Object.defineProperty(this, 'recordId', { value: recordId, writable: false, enumerable: true, configurable: false, }); } } export class PermissionDeniedError extends BCError { resource; action; constructor(message, resource, action, context) { super(message, 'BC_PERMISSION_DENIED', { ...context, resource, action }); Object.defineProperty(this, 'resource', { value: resource, writable: false, enumerable: true, configurable: false, }); Object.defineProperty(this, 'action', { value: action, writable: false, enumerable: true, configurable: false, }); } } export class ActionDisabledError extends BCError { actionName; constructor(actionName, message, context) { super(message ?? `Action ${actionName} is disabled`, 'BC_ACTION_DISABLED', { ...context, actionName }); Object.defineProperty(this, 'actionName', { value: actionName, writable: false, enumerable: true, configurable: false, }); } } export class FieldReadOnlyError extends BCError { fieldName; constructor(fieldName, message, context) { super(message ?? `Field ${fieldName} is read-only`, 'BC_FIELD_READONLY', { ...context, fieldName }); Object.defineProperty(this, 'fieldName', { value: fieldName, writable: false, enumerable: true, configurable: false, }); } } // ============================================================================ // MCP Protocol Errors // ============================================================================ export class MCPError extends BCError { constructor(message, context) { super(message, 'MCP_ERROR', context); } } export class MCPToolNotFoundError extends BCError { toolName; constructor(toolName, message, context) { super(message ?? `MCP tool ${toolName} not found`, 'MCP_TOOL_NOT_FOUND', { ...context, toolName }); Object.defineProperty(this, 'toolName', { value: toolName, writable: false, enumerable: true, configurable: false, }); } } export class MCPResourceNotFoundError extends BCError { resourceUri; constructor(resourceUri, message, context) { super(message ?? `MCP resource ${resourceUri} not found`, 'MCP_RESOURCE_NOT_FOUND', { ...context, resourceUri }); Object.defineProperty(this, 'resourceUri', { value: resourceUri, writable: false, enumerable: true, configurable: false, }); } } export class MCPInvalidArgumentsError extends BCError { toolName; constructor(toolName, message, context) { super(message, 'MCP_INVALID_ARGUMENTS', { ...context, toolName }); Object.defineProperty(this, 'toolName', { value: toolName, writable: false, enumerable: true, configurable: false, }); } } // ============================================================================ // Internal Errors // ============================================================================ export class InternalError extends BCError { constructor(message, context) { super(message, 'BC_INTERNAL_ERROR', context); } } export class NotImplementedError extends BCError { feature; constructor(feature, message, context) { super(message ?? `Feature ${feature ?? 'unknown'} is not yet implemented`, 'BC_NOT_IMPLEMENTED', { ...context, feature }); Object.defineProperty(this, 'feature', { value: feature, writable: false, enumerable: true, configurable: false, }); } } export class UnreachableError extends BCError { constructor(message, context) { super(message ?? 'Unreachable code path executed', 'BC_UNREACHABLE', context); } } // ============================================================================ // Error Type Guards // ============================================================================ export function isConnectionError(error) { return error instanceof ConnectionError; } export function isAuthenticationError(error) { return error instanceof AuthenticationError; } export function isProtocolError(error) { return error instanceof ProtocolError; } export function isParseError(error) { return error instanceof ParseError; } export function isValidationError(error) { return error instanceof ValidationError; } export function isBusinessLogicError(error) { return error instanceof BusinessLogicError; } export function isMCPError(error) { return error instanceof MCPError; } export function isInternalError(error) { return error instanceof InternalError; } export function isBCError(error) { return error instanceof BCError; } //# sourceMappingURL=errors.js.map