@utaba/ucm-mcp-server
Version:
Universal Context Manager MCP Server - AI Productivity Platform
55 lines • 2.57 kB
JavaScript
/**
* SharePointErrorHandler
* Centralized error handling for SharePoint MCP tools
*/
import { McpError, McpErrorCode } from './McpErrorHandler.js';
export class SharePointErrorHandler {
/**
* Handle SharePoint API errors and return appropriate MCP responses
* @param error - The error from axios/API call
* @param logger - Logger instance for error tracking
* @param toolName - Name of the tool for logging context
* @returns ToolResult for authentication errors, throws McpError for others
*/
static handle(error, logger, toolName) {
// Extract only serializable error details for logging to avoid circular reference issues
const sanitizedError = {
message: error?.message,
status: error?.response?.status,
statusText: error?.response?.statusText,
data: error?.response?.data,
url: error?.config?.url
};
logger.error(toolName, 'SharePoint operation failed', '', sanitizedError);
const serverError = error?.response?.data;
// Handle SharePoint authentication required (OnBehalfOf flow)
if (error?.response?.status === 401 && serverError?.error === 'SHAREPOINT_LOGIN_REQUIRED') {
const userMessage = serverError.message || 'To access SharePoint, please login then continue your conversation.';
// loginUrl can be in serverError directly or in details (check both)
const loginUrl = serverError.loginUrl || serverError.details?.loginUrl;
return {
content: [
{
type: 'text',
text: userMessage
},
{
type: 'text',
text: loginUrl
}
]
};
}
// Handle not found errors
if (error?.response?.status === 404) {
throw new McpError(McpErrorCode.InvalidParams, 'SharePoint resource not found');
}
// Handle authorization errors (403 or other 401s)
if (error?.response?.status === 401 || error?.response?.status === 403) {
throw new McpError(McpErrorCode.InvalidParams, 'Not authorized to access this SharePoint resource');
}
// Handle all other errors
throw new McpError(McpErrorCode.InternalError, `SharePoint operation failed: ${error?.message || 'Unknown error'}`, { originalError: error?.message });
}
}
//# sourceMappingURL=SharePointErrorHandler.js.map