feat(coding): add core host api composition
This commit is contained in:
42
electron/api/routes/coding-route-errors.ts
Normal file
42
electron/api/routes/coding-route-errors.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { ServerResponse } from 'node:http';
|
||||
import { CodingProjectServiceError } from '../../coding-projects/project-service';
|
||||
import { CodingConversationServiceError } from '../../coding-runtime/conversation-service';
|
||||
import { sendJson } from '../route-utils';
|
||||
|
||||
export function sendCodingRouteError(res: ServerResponse, error: unknown): void {
|
||||
if (error instanceof CodingProjectServiceError || error instanceof CodingConversationServiceError) {
|
||||
sendJson(res, error.status, {
|
||||
success: false,
|
||||
code: error.code,
|
||||
error: error.message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (error instanceof SyntaxError) {
|
||||
sendJson(res, 400, {
|
||||
success: false,
|
||||
code: 'CODING_REQUEST_INVALID',
|
||||
error: 'Request JSON is invalid',
|
||||
});
|
||||
return;
|
||||
}
|
||||
sendJson(res, 500, {
|
||||
success: false,
|
||||
code: 'CODING_REQUEST_FAILED',
|
||||
error: 'Coding request failed',
|
||||
});
|
||||
}
|
||||
|
||||
export function decodeRouteId(value: string): string {
|
||||
try {
|
||||
const decoded = decodeURIComponent(value).trim();
|
||||
if (!decoded || decoded.length > 128 || decoded.includes('/')) throw new Error();
|
||||
return decoded;
|
||||
} catch {
|
||||
throw new CodingConversationServiceError(
|
||||
400,
|
||||
'CODING_CONVERSATION_REQUEST_INVALID',
|
||||
'Route identifier is invalid',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user