fix: improve error handling and directory creation for settings path (#364)

This commit is contained in:
samanhappy
2025-10-12 15:30:40 +08:00
committed by GitHub
parent 6a59becd8d
commit 435227cbd4
2 changed files with 20 additions and 5 deletions

View File

@@ -42,8 +42,9 @@ export const loadOriginalSettings = (): McpSettings => {
console.log(`Loaded settings from ${settingsPath}`);
return settings;
} catch (error) {
console.error(`Failed to load settings from ${settingsPath}:`, error);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.warn(`Failed to load settings from ${settingsPath}:`, errorMessage);
const defaultSettings = { mcpServers: {}, users: [] };
// Cache default settings

View File

@@ -5,6 +5,13 @@ import { dirname } from 'path';
// Project root directory - use process.cwd() as a simpler alternative
const rootDir = process.cwd();
function getParentPath(p: string, filename: string): string {
if (p.endsWith(filename)) {
p = p.slice(0, -filename.length);
}
return path.resolve(p);
}
/**
* Find the path to a configuration file by checking multiple potential locations.
* @param filename The name of the file to locate (e.g., 'servers.json', 'mcp_settings.json')
@@ -15,11 +22,18 @@ export const getConfigFilePath = (filename: string, description = 'Configuration
if (filename === 'mcp_settings.json') {
const envPath = process.env.MCPHUB_SETTING_PATH;
if (envPath) {
// check envPath is file or directory
const stats = fs.statSync(envPath);
if (stats.isFile()) {
// Ensure directory exists
const dir = getParentPath(envPath, filename);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log(`Created directory for settings at ${dir}`);
}
// if full path, return as is
if (envPath?.endsWith(filename)) {
return envPath;
}
// if directory, return path under that directory
return path.resolve(envPath, filename);
}