Skip to content

Commit 1c635c1

Browse files
committed
replace callback with promise in Log
return HttpError in Log
1 parent 6b713dc commit 1c635c1

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-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: 16 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
/**
@@ -56,7 +56,6 @@ export class Log {
5656
podName: string,
5757
containerName: string,
5858
stream: Writable,
59-
done: (err: any) => void,
6059
options: LogOptions = {},
6160
): Promise<request.Request> {
6261
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`;
@@ -77,20 +76,20 @@ export class Log {
7776
};
7877
await this.config.applyToRequest(requestOptions);
7978

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-
}
79+
return new Promise((resolve, reject) => {
80+
const req = request(requestOptions, (error, response, body) => {
81+
if (error) {
82+
reject(error);
83+
} else if (response.statusCode !== 200) {
84+
body = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status');
85+
reject(new HttpError(response, body, response.statusCode));
86+
}
87+
}).on('response', (response) => {
88+
if (response.statusCode === 200) {
89+
req.pipe(stream);
90+
resolve(req);
91+
}
92+
});
9293
});
93-
94-
return req;
9594
}
9695
}

0 commit comments

Comments
 (0)