Skip to content

Commit 47153c9

Browse files
committed
Improve error handling of createBuildStream
Change-type: patch
1 parent 026f372 commit 47153c9

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

lib/build/builder.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,20 @@ export default class Builder {
9898
const buildPromise = (async () => {
9999
const daemonStream = await this.docker.buildImage(internal, buildOpts);
100100

101-
await new Promise<void>((resolve, reject) => {
102-
const outputStream = getDockerDaemonBuildOutputParserStream(
103-
daemonStream,
104-
layers,
105-
fromTags,
106-
reject,
107-
);
108-
outputStream.on('error', (error: Error) => {
109-
daemonStream.unpipe();
110-
reject(error);
111-
});
112-
outputStream.on('end', () =>
113-
// The 'end' event was observed to be emitted under error
114-
// conditions, hence the test for streamError.
115-
{
116-
if (streamError) {
117-
reject(streamError);
118-
} else {
119-
resolve();
120-
}
121-
},
122-
);
101+
const outputStream = getDockerDaemonBuildOutputParserStream(
102+
daemonStream,
103+
layers,
104+
fromTags,
105+
);
106+
try {
123107
// Connect the output of the docker daemon to the duplex stream
124-
stream.pipeline(outputStream, internal, _.noop);
125-
});
108+
await stream.promises.pipeline(outputStream, internal);
109+
if (streamError != null) {
110+
throw streamError;
111+
}
112+
} finally {
113+
daemonStream.unpipe();
114+
}
126115
})(); // no .catch() here, but rejection is captured by Promise.all() below
127116

128117
// It is helpful for the following promises to run in parallel because
@@ -247,13 +236,11 @@ export default class Builder {
247236
* @param daemonStream: Docker daemon's output stream (dockerode.buildImage)
248237
* @param layers Array to which to push parsed image layer sha strings
249238
* @param fromImageTags Array to which to push parsed FROM image tags info
250-
* @param onError Error callback
251239
*/
252240
function getDockerDaemonBuildOutputParserStream(
253241
daemonStream: NodeJS.ReadableStream,
254242
layers: string[],
255243
fromImageTags: Utils.FromTagInfo[],
256-
onError: (error: Error) => void,
257244
): stream.Duplex {
258245
const fromAliases = new Set();
259246
return stream.pipeline(
@@ -297,10 +284,8 @@ function getDockerDaemonBuildOutputParserStream(
297284
this.push(data.stream);
298285
}
299286
cb();
300-
} catch (error) {
301-
daemonStream.unpipe();
302-
onError(error);
303-
cb(error);
287+
} catch (err) {
288+
cb(err);
304289
}
305290
},
306291
}),

0 commit comments

Comments
 (0)