Skip to content

Commit 97b3c74

Browse files
handle error message before pipeline run (#273)
handle error message before pipeline run
1 parent bc2d0d6 commit 97b3c74

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

lib/interface/cli/commands/pipeline/run.local.js

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable prefer-promise-reject-errors */
12
const Docker = require('dockerode');
23
const RunBaseCommand = require('./run.base');
34
const path = require('path');
@@ -8,6 +9,8 @@ const { log } = require('../../../../logic').api;
89
const chalk = require('chalk');
910

1011
const regex = /##[0-9a-f]{24}##/i;
12+
const EngineErrorPrefix = 'EngineError';
13+
1114

1215
function _customizer(objValue, srcValue) {
1316
if (Array.isArray(objValue)) {
@@ -39,16 +42,16 @@ class RunLocalCommand extends RunBaseCommand {
3942
return new Promise((resolve, reject) => {
4043
this.docker.pull(DEFAULTS.ENGINE_IMAGE, (err, stream) => {
4144
if (err) {
42-
console.log(chalk.red(`Failed to update codefresh engine: \n ${err}`));
4345
reject(err);
4446
}
4547
if (stream) {
4648
function onFinished(error) {
4749
if (error) {
4850
reject(error);
51+
return;
4952
}
5053
console.log('\nFinished Update ==>\n');
51-
resolve(undefined);
54+
resolve();
5255
}
5356

5457
function onProgress(res) {
@@ -105,22 +108,8 @@ class RunLocalCommand extends RunBaseCommand {
105108
const currentContext = authManager.getCurrentContext();
106109
console.log(`Running pipeline: ${pipelineName}`);
107110

108-
process.stdout.on('data', (chunk) => {
109-
const line = chunk.toString();
110-
const include = line.match(regex);
111-
if (include) {
112-
const workflowId = include[0].substring(2, include[0].length - 2);
113-
log.showWorkflowLogs(workflowId, true)
114-
.then(() => Promise.resolve().then(console.log(`Finished running successfully ${workflowId}`)));
115-
}
116-
});
117-
process.stderr.on('data', (chunk) => {
118-
const line = chunk.toString();
119-
console.error(`Error occurred while running pipeline with error : ${chalk.red(line)}`);
120-
this.status = 'Error';
121-
});
122111
return new Promise((resolve, reject) => {
123-
this.docker.run(DEFAULTS.ENGINE_IMAGE, [], undefined, _.mergeWith({
112+
const eventEmitter = this.docker.run(DEFAULTS.ENGINE_IMAGE, [], undefined, _.mergeWith({
124113
Env: [
125114
`ACCESS_TOKEN=${currentContext.token}`,
126115
`PIPELINE_ID=${pipelineName}`, `BRANCH=${branch}`,
@@ -134,20 +123,52 @@ class RunLocalCommand extends RunBaseCommand {
134123
},
135124
}, injectedOpts, _customizer), (err, data) => {
136125
if (err) {
137-
console.log(chalk.red(`Errored when running pipeline : ${err}`));
126+
console.log(chalk.red(`Error when running pipeline : ${err}`));
138127
// eslint-disable-next-line prefer-promise-reject-errors
139-
reject(1);
128+
resolve(1);
140129
}
141-
}).on('stream', (stream) => {
130+
});
131+
if (this.argv.detach) {
132+
resolve(0);
133+
return;
134+
}
135+
eventEmitter.on('stream', (stream) => {
142136
stream.on('data', (chunk) => {
143137
const line = chunk.toString();
138+
if (line.indexOf(`error: ${EngineErrorPrefix}`) !== -1) {
139+
const error = JSON.parse(line.substring(
140+
line.indexOf(`error: ${EngineErrorPrefix}`) + `error: ${EngineErrorPrefix}`.length,
141+
line.lastIndexOf(EngineErrorPrefix),
142+
));
143+
if (error.code || error.message) {
144+
console.log(chalk.red(`Error when running pipeline code: ${error.code} message: ${error.message}`));
145+
} else {
146+
console.log(chalk.red(`Error when running pipeline: ${error}`));
147+
}
148+
149+
this.status = 'Error';
150+
resolve(1);
151+
}
144152
const include = line.match(regex);
145153
if (include) {
146154
const workflowId = include[0].substring(2, include[0].length - 2);
147155
log.showWorkflowLogs(workflowId, true)
148156
.then(() => resolve(0));
149157
}
150158
});
159+
stream.on('error', (err) => {
160+
if (err) {
161+
console.log(chalk.red(`Pipeline finished with error: ${err}`));
162+
this.status = 'Error';
163+
}
164+
// eslint-disable-next-line prefer-promise-reject-errors
165+
resolve(1);
166+
});
167+
stream.on('end', () => {
168+
if (this.status !== 'Error') {
169+
resolve(0);
170+
}
171+
});
151172
});
152173
});
153174
}

0 commit comments

Comments
 (0)