feat: Update error handling to use getErrorMessage utility and improve logging across multiple services

This commit is contained in:
2025-11-25 08:25:54 +00:00
parent c59d56e70a
commit e94906b3bf
13 changed files with 97 additions and 75 deletions

View File

@@ -10,7 +10,7 @@ import { logger } from '../logging.ts';
import { getErrorMessage } from '../utils/error.ts';
export class OneboxDockerManager {
private dockerClient: plugins.docker.Docker | null = null;
private dockerClient: InstanceType<typeof plugins.docker.Docker> | null = null;
private networkName = 'onebox-network';
/**
@@ -695,14 +695,6 @@ export class OneboxDockerManager {
timestamps: true,
});
// v5 should return a string, but let's handle edge cases
if (typeof logs !== 'string') {
logger.error(`Unexpected logs type: ${typeof logs}, constructor: ${logs?.constructor?.name}`);
logger.error(`Logs content: ${JSON.stringify(logs).slice(0, 500)}`);
// If it's not a string, something went wrong
throw new Error(`Unexpected log format: expected string, got ${typeof logs}`);
}
// v5 returns already-parsed logs as a string
return {
stdout: logs,
@@ -809,20 +801,17 @@ export class OneboxDockerManager {
throw new Error(`Container not found: ${containerID}`);
}
const exec = await container.exec({
Cmd: cmd,
AttachStdout: true,
AttachStderr: true,
const { stream, inspect } = await container.exec(cmd, {
attachStdout: true,
attachStderr: true,
});
const stream = await exec.start({ Detach: false });
let stdout = '';
let stderr = '';
stream.on('data', (chunk: Buffer) => {
stream.on('data', (chunk: Uint8Array) => {
const streamType = chunk[0];
const content = chunk.slice(8).toString();
const content = new TextDecoder().decode(chunk.slice(8));
if (streamType === 1) {
stdout += content;
@@ -834,8 +823,8 @@ export class OneboxDockerManager {
// Wait for completion
await new Promise((resolve) => stream.on('end', resolve));
const inspect = await exec.inspect();
const exitCode = inspect.ExitCode || 0;
const execInfo = await inspect();
const exitCode = execInfo.ExitCode || 0;
return { stdout, stderr, exitCode };
} catch (error) {
@@ -928,4 +917,26 @@ export class OneboxDockerManager {
throw error;
}
}
/**
* Get a container by ID
* Public wrapper for Docker client method
*/
async getContainerById(containerID: string): Promise<any> {
if (!this.dockerClient) {
throw new Error('Docker client not initialized');
}
return this.dockerClient.getContainerById(containerID);
}
/**
* List all containers
* Public wrapper for Docker client method
*/
async listAllContainers(): Promise<any[]> {
if (!this.dockerClient) {
throw new Error('Docker client not initialized');
}
return this.dockerClient.listContainers();
}
}