Skip to content

Commit f17e404

Browse files
authored
Merge pull request #703 from Legion2/log-error
Replace callback with promise in Log
2 parents 1adb9a7 + de1cbb5 commit f17e404

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

examples/follow-logs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ logStream.on('data', (chunk) => {
1313
process.stdout.write(chunk);
1414
});
1515

16-
log.log('default', 'pod1', 'container1', logStream, (err) => {console.log(err)}, {follow: true, tailLines: 50, pretty: false, timestamps: false})
16+
log.log('default', 'pod1', 'container1', logStream, {follow: true, tailLines: 50, pretty: false, timestamps: false})
17+
.catch(err => {console.log(err)})
1718
.then(req => {
1819
// disconnects after 5 seconds
1920
setTimeout(function(){
2021
req.abort();
2122
}, 5000);
2223
});
23-

src/log.ts

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import request = require('request');
1+
import * as request from 'request';
22
import { Writable } from 'stream';
3-
43
import { KubeConfig } from './config';
4+
import { HttpError, ObjectSerializer } from './gen/api';
55

66
export interface LogOptions {
77
/**
@@ -51,14 +51,37 @@ export class Log {
5151
this.config = config;
5252
}
5353

54+
public async log(
55+
namespace: string,
56+
podName: string,
57+
containerName: string,
58+
stream: Writable,
59+
options?: LogOptions,
60+
): Promise<request.Request>;
61+
/** @deprecated done callback is deprecated */
5462
public async log(
5563
namespace: string,
5664
podName: string,
5765
containerName: string,
5866
stream: Writable,
5967
done: (err: any) => void,
60-
options: LogOptions = {},
68+
options?: LogOptions,
69+
): Promise<request.Request>;
70+
public async log(
71+
namespace: string,
72+
podName: string,
73+
containerName: string,
74+
stream: Writable,
75+
doneOrOptions?: ((err: any) => void) | LogOptions,
76+
options?: LogOptions,
6177
): Promise<request.Request> {
78+
let done: (err: any) => void = () => undefined;
79+
if (typeof doneOrOptions === 'function') {
80+
done = doneOrOptions;
81+
} else {
82+
options = doneOrOptions;
83+
}
84+
6285
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`;
6386

6487
const cluster = this.config.getCurrentCluster();
@@ -77,20 +100,24 @@ export class Log {
77100
};
78101
await this.config.applyToRequest(requestOptions);
79102

80-
const req = request(requestOptions, (error, response, body) => {
81-
if (error) {
82-
done(error);
83-
} else if (response && response.statusCode !== 200) {
84-
done(body);
85-
} else {
86-
done(null);
87-
}
88-
}).on('response', (response) => {
89-
if (response.statusCode === 200) {
90-
req.pipe(stream);
91-
}
103+
return new Promise((resolve, reject) => {
104+
const req = request(requestOptions, (error, response, body) => {
105+
if (error) {
106+
reject(error);
107+
done(error);
108+
} else if (response.statusCode !== 200) {
109+
const deserializedBody = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status');
110+
reject(new HttpError(response, deserializedBody, response.statusCode));
111+
done(body);
112+
} else {
113+
done(null);
114+
}
115+
}).on('response', (response) => {
116+
if (response.statusCode === 200) {
117+
req.pipe(stream);
118+
resolve(req);
119+
}
120+
});
92121
});
93-
94-
return req;
95122
}
96123
}

0 commit comments

Comments
 (0)