Skip to content

Commit 8f2a8f2

Browse files
committed
better error reporting when failing to detect docker
1 parent f17870b commit 8f2a8f2

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

extensions/positron-dev-containers/src/container/devContainerManager.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ import { getCLIHost, loadNativeModule } from '../spec/spec-common/commonUtils';
2121
import { workspaceFromPath } from '../spec/spec-utils/workspaces';
2222
import { makeLog, LogLevel } from '../spec/spec-utils/log';
2323

24+
/**
25+
* Format an error for logging, handling both Error instances and docker command errors
26+
* which have stdout/stderr/code properties.
27+
*/
28+
function formatError(error: unknown): string {
29+
if (error instanceof Error) {
30+
return error.message;
31+
}
32+
if (typeof error === 'object' && error !== null) {
33+
// Handle docker command errors which have stdout/stderr/code
34+
const err = error as { message?: string; stderr?: Buffer; code?: number };
35+
const parts: string[] = [];
36+
if (err.message) {
37+
parts.push(err.message);
38+
}
39+
if (err.stderr && Buffer.isBuffer(err.stderr)) {
40+
const stderrText = err.stderr.toString().trim();
41+
if (stderrText) {
42+
parts.push(stderrText);
43+
}
44+
}
45+
if (err.code !== undefined) {
46+
parts.push(`Exit code: ${err.code}`);
47+
}
48+
return parts.length > 0 ? parts.join(' - ') : JSON.stringify(error);
49+
}
50+
return String(error);
51+
}
52+
2453
/**
2554
* Options for creating/starting a dev container
2655
*/
@@ -368,29 +397,8 @@ export class DevContainerManager {
368397
const params = await this.createDockerParams();
369398
await dockerCLI(params, 'start', containerId);
370399
logger.info(`Container started: ${containerId}`);
371-
} catch (error: any) {
372-
let errorMsg = 'Unknown error';
373-
if (error instanceof Error) {
374-
errorMsg = error.message;
375-
} else if (typeof error === 'object' && error !== null) {
376-
// Handle docker command errors which have stdout/stderr/code
377-
const parts: string[] = [];
378-
if (error.message) {
379-
parts.push(error.message);
380-
}
381-
if (error.stderr && Buffer.isBuffer(error.stderr)) {
382-
const stderrText = error.stderr.toString().trim();
383-
if (stderrText) {
384-
parts.push(stderrText);
385-
}
386-
}
387-
if (error.code !== undefined) {
388-
parts.push(`Exit code: ${error.code}`);
389-
}
390-
errorMsg = parts.length > 0 ? parts.join(' - ') : JSON.stringify(error);
391-
} else {
392-
errorMsg = String(error);
393-
}
400+
} catch (error) {
401+
const errorMsg = formatError(error);
394402
logger.error(`Failed to start container: ${errorMsg}`, error);
395403
throw new Error(`Failed to start container: ${errorMsg}`);
396404
}
@@ -544,7 +552,8 @@ export class DevContainerManager {
544552
logger.debug('Docker is available');
545553
return true;
546554
} catch (error) {
547-
logger.warn('Docker not available:', error instanceof Error ? error.message : String(error));
555+
const errorMsg = formatError(error);
556+
logger.warn(`Docker not available: ${errorMsg}`, error);
548557
if (error instanceof Error && error.stack) {
549558
logger.debug('Stack trace:', error.stack);
550559
}

0 commit comments

Comments
 (0)