feat: Update error handling to use getErrorMessage utility and improve logging across multiple services
This commit is contained in:
@@ -8,7 +8,7 @@ import * as plugins from '../plugins.ts';
|
||||
import { logger } from '../logging.ts';
|
||||
import { getErrorMessage } from '../utils/error.ts';
|
||||
import type { Onebox } from './onebox.ts';
|
||||
import type { IApiResponse, ICreateRegistryTokenRequest, IRegistryTokenView } from '../types.ts';
|
||||
import type { IApiResponse, ICreateRegistryTokenRequest, IRegistryTokenView, TPlatformServiceType } from '../types.ts';
|
||||
|
||||
export class OneboxHttpServer {
|
||||
private oneboxRef: Onebox;
|
||||
@@ -482,8 +482,8 @@ export class OneboxHttpServer {
|
||||
private async handleGetLogsRequest(name: string): Promise<Response> {
|
||||
try {
|
||||
const logs = await this.oneboxRef.services.getServiceLogs(name);
|
||||
logger.log(`handleGetLogsRequest: logs type = ${typeof logs}, constructor = ${logs?.constructor?.name}`);
|
||||
logger.log(`handleGetLogsRequest: logs value = ${String(logs).slice(0, 100)}`);
|
||||
logger.debug(`handleGetLogsRequest: logs type = ${typeof logs}, constructor = ${logs?.constructor?.name}`);
|
||||
logger.debug(`handleGetLogsRequest: logs value = ${String(logs).slice(0, 100)}`);
|
||||
return this.jsonResponse({ success: true, data: logs });
|
||||
} catch (error) {
|
||||
logger.error(`Failed to get logs for service ${name}: ${getErrorMessage(error)}`);
|
||||
@@ -878,13 +878,13 @@ export class OneboxHttpServer {
|
||||
|
||||
// Get the container (handle both direct container IDs and service IDs)
|
||||
logger.info(`Looking up container for service ${serviceName}, containerID: ${service.containerID}`);
|
||||
let container = await this.oneboxRef.docker.dockerClient!.getContainerById(service.containerID!);
|
||||
let container = await this.oneboxRef.docker.getContainerById(service.containerID!);
|
||||
logger.info(`Direct lookup result: ${container ? 'found' : 'null'}`);
|
||||
|
||||
// If not found, it might be a service ID - try to get the actual container ID
|
||||
if (!container) {
|
||||
logger.info('Listing all containers to find matching service...');
|
||||
const containers = await this.oneboxRef.docker.dockerClient!.listContainers();
|
||||
const containers = await this.oneboxRef.docker.listAllContainers();
|
||||
logger.info(`Found ${containers.length} containers`);
|
||||
|
||||
const serviceContainer = containers.find((c: any) => {
|
||||
@@ -894,7 +894,7 @@ export class OneboxHttpServer {
|
||||
|
||||
if (serviceContainer) {
|
||||
logger.info(`Found matching container: ${serviceContainer.Id}`);
|
||||
container = await this.oneboxRef.docker.dockerClient!.getContainerById(serviceContainer.Id);
|
||||
container = await this.oneboxRef.docker.getContainerById(serviceContainer.Id);
|
||||
logger.info(`Second lookup result: ${container ? 'found' : 'null'}`);
|
||||
} else {
|
||||
logger.error(`No container found with service label matching ${service.containerID}`);
|
||||
@@ -924,18 +924,21 @@ export class OneboxHttpServer {
|
||||
|
||||
// Demultiplex and pipe log data to WebSocket
|
||||
// Docker streams use 8-byte headers: [STREAM_TYPE, 0, 0, 0, SIZE_BYTE1, SIZE_BYTE2, SIZE_BYTE3, SIZE_BYTE4]
|
||||
let buffer = Buffer.alloc(0);
|
||||
let buffer = new Uint8Array(0);
|
||||
|
||||
logStream.on('data', (chunk: Buffer) => {
|
||||
logStream.on('data', (chunk: Uint8Array) => {
|
||||
if (socket.readyState !== WebSocket.OPEN) return;
|
||||
|
||||
// Append new data to buffer
|
||||
buffer = Buffer.concat([buffer, chunk]);
|
||||
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
||||
newBuffer.set(buffer);
|
||||
newBuffer.set(chunk, buffer.length);
|
||||
buffer = newBuffer;
|
||||
|
||||
// Process complete frames
|
||||
while (buffer.length >= 8) {
|
||||
// Read frame size from header (bytes 4-7, big-endian)
|
||||
const frameSize = buffer.readUInt32BE(4);
|
||||
const frameSize = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
|
||||
|
||||
// Check if we have the complete frame
|
||||
if (buffer.length < 8 + frameSize) {
|
||||
@@ -946,7 +949,7 @@ export class OneboxHttpServer {
|
||||
const frameData = buffer.slice(8, 8 + frameSize);
|
||||
|
||||
// Send the clean log line
|
||||
socket.send(frameData.toString('utf8'));
|
||||
socket.send(new TextDecoder().decode(frameData));
|
||||
|
||||
// Remove processed frame from buffer
|
||||
buffer = buffer.slice(8 + frameSize);
|
||||
@@ -1083,7 +1086,7 @@ export class OneboxHttpServer {
|
||||
}
|
||||
}
|
||||
|
||||
private async handleGetPlatformServiceRequest(type: string): Promise<Response> {
|
||||
private async handleGetPlatformServiceRequest(type: TPlatformServiceType): Promise<Response> {
|
||||
try {
|
||||
const provider = this.oneboxRef.platformServices.getProvider(type);
|
||||
if (!provider) {
|
||||
@@ -1123,7 +1126,7 @@ export class OneboxHttpServer {
|
||||
}
|
||||
}
|
||||
|
||||
private async handleStartPlatformServiceRequest(type: string): Promise<Response> {
|
||||
private async handleStartPlatformServiceRequest(type: TPlatformServiceType): Promise<Response> {
|
||||
try {
|
||||
const provider = this.oneboxRef.platformServices.getProvider(type);
|
||||
if (!provider) {
|
||||
@@ -1154,7 +1157,7 @@ export class OneboxHttpServer {
|
||||
}
|
||||
}
|
||||
|
||||
private async handleStopPlatformServiceRequest(type: string): Promise<Response> {
|
||||
private async handleStopPlatformServiceRequest(type: TPlatformServiceType): Promise<Response> {
|
||||
try {
|
||||
const provider = this.oneboxRef.platformServices.getProvider(type);
|
||||
if (!provider) {
|
||||
|
||||
Reference in New Issue
Block a user