Skip to content

Commit ea57504

Browse files
committed
fix: enhanced error handling
1 parent dae24dd commit ea57504

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

.changeset/cute-bikes-show.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ansible-database-mcp": patch
3+
---
4+
5+
enhanced error handling

src/main.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ const authStrategy = await initializeAuth();
1515
const app = express();
1616
const PORT = process.env.PORT || 3000;
1717

18-
// JSON parsing middleware
19-
app.use(express.json());
18+
// JSON parsing middleware with error handling
19+
app.use(express.json({ limit: '10mb' }));
20+
21+
// JSON parsing error handler
22+
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
23+
if (err instanceof SyntaxError && 'body' in err) {
24+
return res.status(400).json({
25+
jsonrpc: '2.0',
26+
error: {
27+
code: -32700, // Parse error in JSON-RPC
28+
message: 'Invalid JSON syntax: ' + err.message
29+
},
30+
id: null
31+
});
32+
}
33+
next(err);
34+
});
2035

2136
// CORS middleware
2237
app.use(cors({

src/services/db-connection/adapters/databricks-adapter.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ export class DatabricksAdapter extends EventEmitter {
7373
for (let i = 0; i < this.poolSize.min; i++) {
7474
await this.createSession();
7575
}
76-
} catch (error) {
77-
throw new Error(`Failed to initialize Databricks connection: ${error}`);
76+
} catch (error: any) {
77+
console.error('[Databricks] Connection error:', error);
78+
throw error;
7879
}
7980
}
8081

@@ -87,15 +88,21 @@ export class DatabricksAdapter extends EventEmitter {
8788
}
8889

8990
const connection = this.config.connection as any;
90-
const session = await this.client.openSession({
91-
initialCatalog: connection.catalog,
92-
initialSchema: connection.database // database field is used as schema in Databricks
93-
});
94-
95-
this.sessions.push(session);
96-
this.availableSessions.push(session);
9791

98-
return session;
92+
try {
93+
const session = await this.client.openSession({
94+
initialCatalog: connection.catalog,
95+
initialSchema: connection.database // database field is used as schema in Databricks
96+
});
97+
98+
this.sessions.push(session);
99+
this.availableSessions.push(session);
100+
101+
return session;
102+
} catch (error: any) {
103+
console.error('[Databricks] Session creation error:', error);
104+
throw error;
105+
}
99106
}
100107

101108
/**
@@ -177,6 +184,9 @@ export class DatabricksAdapter extends EventEmitter {
177184
rows: result,
178185
fields
179186
};
187+
} catch (error: any) {
188+
console.error('[Databricks] Query execution error:', error);
189+
throw error;
180190
} finally {
181191
this.releaseSession(session);
182192
}

src/services/query.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,17 @@ export async function executeQuery(dbKey: string, params: QueryParams): Promise<
8888
logError('Query execution error', error);
8989
logQuery(query, params, executionTime, false, error.message);
9090

91+
// Return full error details for better debugging
9192
return {
9293
success: false,
93-
error: error.message,
94-
message: 'An error occurred while executing the query.'
94+
error: error.message || 'Unknown error',
95+
message: error.stack || 'An error occurred while executing the query.',
96+
details: {
97+
code: error.code,
98+
statusCode: error.statusCode,
99+
sqlState: error.sqlState,
100+
originalError: error.toString()
101+
}
95102
};
96103
} finally {
97104
// Return connection

src/tools/query.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,23 @@ const handler: ToolCallback<typeof definition.inputSchema> = async (args) => {
5656
],
5757
};
5858
} else {
59-
// Format the error result
59+
// Format the error result with full details
60+
const errorResponse: any = {
61+
success: false,
62+
error: result.error || 'Unknown error occurred',
63+
message: result.message || 'Query execution failed',
64+
};
65+
66+
// Include details if available
67+
if (result.details) {
68+
errorResponse.details = result.details;
69+
}
70+
6071
return {
6172
content: [
6273
{
6374
type: 'text',
64-
text: JSON.stringify({
65-
success: false,
66-
error: result.error || 'Unknown error occurred',
67-
message: result.message || 'Query execution failed',
68-
}, null, 2),
75+
text: JSON.stringify(errorResponse, null, 2),
6976
},
7077
],
7178
};

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface APIResponse<T = any> {
1717
data?: T;
1818
error?: string;
1919
message?: string;
20+
details?: any;
2021
}
2122

2223
export interface QueryResult {

0 commit comments

Comments
 (0)