mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-17 01:15:19 +00:00
Merge pull request #3104 from Dokploy/fix/error-parsing
feat: enhance error handling in deployment processes
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
|||||||
import { sendBuildErrorNotifications } from "@dokploy/server/utils/notifications/build-error";
|
import { sendBuildErrorNotifications } from "@dokploy/server/utils/notifications/build-error";
|
||||||
import { sendBuildSuccessNotifications } from "@dokploy/server/utils/notifications/build-success";
|
import { sendBuildSuccessNotifications } from "@dokploy/server/utils/notifications/build-success";
|
||||||
import {
|
import {
|
||||||
|
ExecError,
|
||||||
execAsync,
|
execAsync,
|
||||||
execAsyncRemote,
|
execAsyncRemote,
|
||||||
} from "@dokploy/server/utils/process/execAsync";
|
} from "@dokploy/server/utils/process/execAsync";
|
||||||
@@ -28,6 +29,7 @@ import { cloneGitlabRepository } from "@dokploy/server/utils/providers/gitlab";
|
|||||||
import { createTraefikConfig } from "@dokploy/server/utils/traefik/application";
|
import { createTraefikConfig } from "@dokploy/server/utils/traefik/application";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
import { encodeBase64 } from "../utils/docker/utils";
|
||||||
import { getDokployUrl } from "./admin";
|
import { getDokployUrl } from "./admin";
|
||||||
import {
|
import {
|
||||||
createDeployment,
|
createDeployment,
|
||||||
@@ -228,7 +230,16 @@ export const deployApplication = async ({
|
|||||||
environmentName: application.environment.name,
|
environmentName: application.environment.name,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const command = `echo "Error occurred ❌, check the logs for details." >> ${deployment.logPath};`;
|
let command = "";
|
||||||
|
|
||||||
|
// Only log details for non-ExecError errors
|
||||||
|
if (!(error instanceof ExecError)) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
const encodedMessage = encodeBase64(message);
|
||||||
|
command += `echo "${encodedMessage}" | base64 -d >> "${deployment.logPath}";`;
|
||||||
|
}
|
||||||
|
|
||||||
|
command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
|
||||||
if (application.serverId) {
|
if (application.serverId) {
|
||||||
await execAsyncRemote(application.serverId, command);
|
await execAsyncRemote(application.serverId, command);
|
||||||
} else {
|
} else {
|
||||||
@@ -317,6 +328,21 @@ export const rebuildApplication = async ({
|
|||||||
environmentName: application.environment.name,
|
environmentName: application.environment.name,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
let command = "";
|
||||||
|
|
||||||
|
// Only log details for non-ExecError errors
|
||||||
|
if (!(error instanceof ExecError)) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
const encodedMessage = encodeBase64(message);
|
||||||
|
command += `echo "${encodedMessage}" | base64 -d >> "${deployment.logPath}";`;
|
||||||
|
}
|
||||||
|
|
||||||
|
command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
|
||||||
|
if (application.serverId) {
|
||||||
|
await execAsyncRemote(application.serverId, command);
|
||||||
|
} else {
|
||||||
|
await execAsync(command);
|
||||||
|
}
|
||||||
await updateDeploymentStatus(deployment.deploymentId, "error");
|
await updateDeploymentStatus(deployment.deploymentId, "error");
|
||||||
await updateApplicationStatus(applicationId, "error");
|
await updateApplicationStatus(applicationId, "error");
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import type { ComposeSpecification } from "@dokploy/server/utils/docker/types";
|
|||||||
import { sendBuildErrorNotifications } from "@dokploy/server/utils/notifications/build-error";
|
import { sendBuildErrorNotifications } from "@dokploy/server/utils/notifications/build-error";
|
||||||
import { sendBuildSuccessNotifications } from "@dokploy/server/utils/notifications/build-success";
|
import { sendBuildSuccessNotifications } from "@dokploy/server/utils/notifications/build-success";
|
||||||
import {
|
import {
|
||||||
|
ExecError,
|
||||||
execAsync,
|
execAsync,
|
||||||
execAsyncRemote,
|
execAsyncRemote,
|
||||||
} from "@dokploy/server/utils/process/execAsync";
|
} from "@dokploy/server/utils/process/execAsync";
|
||||||
@@ -32,6 +33,7 @@ import { cloneGitlabRepository } from "@dokploy/server/utils/providers/gitlab";
|
|||||||
import { getCreateComposeFileCommand } from "@dokploy/server/utils/providers/raw";
|
import { getCreateComposeFileCommand } from "@dokploy/server/utils/providers/raw";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
import { encodeBase64 } from "../utils/docker/utils";
|
||||||
import { getDokployUrl } from "./admin";
|
import { getDokployUrl } from "./admin";
|
||||||
import {
|
import {
|
||||||
createDeploymentCompose,
|
createDeploymentCompose,
|
||||||
@@ -270,6 +272,21 @@ export const deployCompose = async ({
|
|||||||
environmentName: compose.environment.name,
|
environmentName: compose.environment.name,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
let command = "";
|
||||||
|
|
||||||
|
// Only log details for non-ExecError errors
|
||||||
|
if (!(error instanceof ExecError)) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
const encodedMessage = encodeBase64(message);
|
||||||
|
command += `echo "${encodedMessage}" | base64 -d >> "${deployment.logPath}";`;
|
||||||
|
}
|
||||||
|
|
||||||
|
command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
|
||||||
|
if (compose.serverId) {
|
||||||
|
await execAsyncRemote(compose.serverId, command);
|
||||||
|
} else {
|
||||||
|
await execAsync(command);
|
||||||
|
}
|
||||||
await updateDeploymentStatus(deployment.deploymentId, "error");
|
await updateDeploymentStatus(deployment.deploymentId, "error");
|
||||||
await updateCompose(composeId, {
|
await updateCompose(composeId, {
|
||||||
composeStatus: "error",
|
composeStatus: "error",
|
||||||
@@ -342,6 +359,21 @@ export const rebuildCompose = async ({
|
|||||||
composeStatus: "done",
|
composeStatus: "done",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
let command = "";
|
||||||
|
|
||||||
|
// Only log details for non-ExecError errors
|
||||||
|
if (!(error instanceof ExecError)) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
const encodedMessage = encodeBase64(message);
|
||||||
|
command += `echo "${encodedMessage}" | base64 -d >> "${deployment.logPath}";`;
|
||||||
|
}
|
||||||
|
|
||||||
|
command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
|
||||||
|
if (compose.serverId) {
|
||||||
|
await execAsyncRemote(compose.serverId, command);
|
||||||
|
} else {
|
||||||
|
await execAsync(command);
|
||||||
|
}
|
||||||
await updateDeploymentStatus(deployment.deploymentId, "error");
|
await updateDeploymentStatus(deployment.deploymentId, "error");
|
||||||
await updateCompose(composeId, {
|
await updateCompose(composeId, {
|
||||||
composeStatus: "error",
|
composeStatus: "error",
|
||||||
|
|||||||
@@ -60,10 +60,7 @@ export const getUpdateData = async (): Promise<IUpdateData> => {
|
|||||||
try {
|
try {
|
||||||
currentDigest = await getServiceImageDigest();
|
currentDigest = await getServiceImageDigest();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
// TODO: Docker versions 29.0.0 change the way to get the service image digest, so we need to update this in the future we upgrade to that version.
|
||||||
// Docker service might not exist locally
|
|
||||||
// You can run the # Installation command for docker service create mentioned in the below docs to test it locally:
|
|
||||||
// https://docs.dokploy.com/docs/core/manual-installation
|
|
||||||
return DEFAULT_UPDATE_DATA;
|
return DEFAULT_UPDATE_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
export interface ExecErrorDetails {
|
||||||
|
command: string;
|
||||||
|
stdout?: string;
|
||||||
|
stderr?: string;
|
||||||
|
exitCode?: number;
|
||||||
|
originalError?: Error;
|
||||||
|
serverId?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ExecError extends Error {
|
||||||
|
public readonly command: string;
|
||||||
|
public readonly stdout?: string;
|
||||||
|
public readonly stderr?: string;
|
||||||
|
public readonly exitCode?: number;
|
||||||
|
public readonly originalError?: Error;
|
||||||
|
public readonly serverId?: string | null;
|
||||||
|
|
||||||
|
constructor(message: string, details: ExecErrorDetails) {
|
||||||
|
super(message);
|
||||||
|
this.name = "ExecError";
|
||||||
|
this.command = details.command;
|
||||||
|
this.stdout = details.stdout;
|
||||||
|
this.stderr = details.stderr;
|
||||||
|
this.exitCode = details.exitCode;
|
||||||
|
this.originalError = details.originalError;
|
||||||
|
this.serverId = details.serverId;
|
||||||
|
|
||||||
|
// Maintains proper stack trace for where our error was thrown (only available on V8)
|
||||||
|
if (Error.captureStackTrace) {
|
||||||
|
Error.captureStackTrace(this, ExecError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a formatted error message with all details
|
||||||
|
*/
|
||||||
|
getDetailedMessage(): string {
|
||||||
|
const parts = [
|
||||||
|
`Command: ${this.command}`,
|
||||||
|
this.exitCode !== undefined ? `Exit Code: ${this.exitCode}` : null,
|
||||||
|
this.serverId ? `Server ID: ${this.serverId}` : "Location: Local",
|
||||||
|
this.stderr ? `Stderr: ${this.stderr}` : null,
|
||||||
|
this.stdout ? `Stdout: ${this.stdout}` : null,
|
||||||
|
].filter(Boolean);
|
||||||
|
|
||||||
|
return `${this.message}\n${parts.join("\n")}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this error is from a remote execution
|
||||||
|
*/
|
||||||
|
isRemote(): boolean {
|
||||||
|
return !!this.serverId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,43 @@ import { exec, execFile } from "node:child_process";
|
|||||||
import util from "node:util";
|
import util from "node:util";
|
||||||
import { findServerById } from "@dokploy/server/services/server";
|
import { findServerById } from "@dokploy/server/services/server";
|
||||||
import { Client } from "ssh2";
|
import { Client } from "ssh2";
|
||||||
|
import { ExecError } from "./ExecError";
|
||||||
|
|
||||||
export const execAsync = util.promisify(exec);
|
// Re-export ExecError for easier imports
|
||||||
|
export { ExecError } from "./ExecError";
|
||||||
|
|
||||||
|
const execAsyncBase = util.promisify(exec);
|
||||||
|
|
||||||
|
export const execAsync = async (
|
||||||
|
command: string,
|
||||||
|
options?: { cwd?: string; env?: NodeJS.ProcessEnv; shell?: string },
|
||||||
|
): Promise<{ stdout: string; stderr: string }> => {
|
||||||
|
try {
|
||||||
|
const result = await execAsyncBase(command, options);
|
||||||
|
return {
|
||||||
|
stdout: result.stdout.toString(),
|
||||||
|
stderr: result.stderr.toString(),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
// @ts-ignore - exec error has these properties
|
||||||
|
const exitCode = error.code;
|
||||||
|
// @ts-ignore
|
||||||
|
const stdout = error.stdout?.toString() || "";
|
||||||
|
// @ts-ignore
|
||||||
|
const stderr = error.stderr?.toString() || "";
|
||||||
|
|
||||||
|
throw new ExecError(`Command execution failed: ${error.message}`, {
|
||||||
|
command,
|
||||||
|
stdout,
|
||||||
|
stderr,
|
||||||
|
exitCode,
|
||||||
|
originalError: error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
interface ExecOptions {
|
interface ExecOptions {
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
@@ -21,7 +56,16 @@ export const execAsyncStream = (
|
|||||||
|
|
||||||
const childProcess = exec(command, options, (error) => {
|
const childProcess = exec(command, options, (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(
|
||||||
|
new ExecError(`Command execution failed: ${error.message}`, {
|
||||||
|
command,
|
||||||
|
stdout: stdoutComplete,
|
||||||
|
stderr: stderrComplete,
|
||||||
|
// @ts-ignore
|
||||||
|
exitCode: error.code,
|
||||||
|
originalError: error,
|
||||||
|
}),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve({ stdout: stdoutComplete, stderr: stderrComplete });
|
resolve({ stdout: stdoutComplete, stderr: stderrComplete });
|
||||||
@@ -45,7 +89,14 @@ export const execAsyncStream = (
|
|||||||
|
|
||||||
childProcess.on("error", (error) => {
|
childProcess.on("error", (error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
reject(error);
|
reject(
|
||||||
|
new ExecError(`Command execution error: ${error.message}`, {
|
||||||
|
command,
|
||||||
|
stdout: stdoutComplete,
|
||||||
|
stderr: stderrComplete,
|
||||||
|
originalError: error,
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -108,7 +159,14 @@ export const execAsyncRemote = async (
|
|||||||
conn.exec(command, (err, stream) => {
|
conn.exec(command, (err, stream) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
onData?.(err.message);
|
onData?.(err.message);
|
||||||
throw err;
|
reject(
|
||||||
|
new ExecError(`Remote command execution failed: ${err.message}`, {
|
||||||
|
command,
|
||||||
|
serverId,
|
||||||
|
originalError: err,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
stream
|
stream
|
||||||
.on("close", (code: number, _signal: string) => {
|
.on("close", (code: number, _signal: string) => {
|
||||||
@@ -116,7 +174,18 @@ export const execAsyncRemote = async (
|
|||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
resolve({ stdout, stderr });
|
resolve({ stdout, stderr });
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`Error occurred ❌: ${stderr}`));
|
reject(
|
||||||
|
new ExecError(
|
||||||
|
`Remote command failed with exit code ${code}`,
|
||||||
|
{
|
||||||
|
command,
|
||||||
|
stdout,
|
||||||
|
stderr,
|
||||||
|
exitCode: code,
|
||||||
|
serverId,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on("data", (data: string) => {
|
.on("data", (data: string) => {
|
||||||
@@ -132,17 +201,25 @@ export const execAsyncRemote = async (
|
|||||||
.on("error", (err) => {
|
.on("error", (err) => {
|
||||||
conn.end();
|
conn.end();
|
||||||
if (err.level === "client-authentication") {
|
if (err.level === "client-authentication") {
|
||||||
onData?.(
|
const errorMsg = `Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`;
|
||||||
`Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
|
onData?.(errorMsg);
|
||||||
);
|
|
||||||
reject(
|
reject(
|
||||||
new Error(
|
new ExecError(errorMsg, {
|
||||||
`Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
|
command,
|
||||||
),
|
serverId,
|
||||||
|
originalError: err,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
onData?.(`SSH connection error: ${err.message}`);
|
const errorMsg = `SSH connection error: ${err.message}`;
|
||||||
reject(new Error(`SSH connection error: ${err.message}`));
|
onData?.(errorMsg);
|
||||||
|
reject(
|
||||||
|
new ExecError(errorMsg, {
|
||||||
|
command,
|
||||||
|
serverId,
|
||||||
|
originalError: err,
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.connect({
|
.connect({
|
||||||
|
|||||||
@@ -159,7 +159,6 @@ export const cloneGithubRepository = async ({
|
|||||||
const octokit = authGithub(githubProvider);
|
const octokit = authGithub(githubProvider);
|
||||||
const token = await getGithubToken(octokit);
|
const token = await getGithubToken(octokit);
|
||||||
const repoclone = `github.com/${owner}/${repository}.git`;
|
const repoclone = `github.com/${owner}/${repository}.git`;
|
||||||
// await recreateDirectory(outputPath);
|
|
||||||
command += `rm -rf ${outputPath};`;
|
command += `rm -rf ${outputPath};`;
|
||||||
command += `mkdir -p ${outputPath};`;
|
command += `mkdir -p ${outputPath};`;
|
||||||
const cloneUrl = `https://oauth2:${token}@${repoclone}`;
|
const cloneUrl = `https://oauth2:${token}@${repoclone}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user