Skip to content

Commit 10320ef

Browse files
authored
Merge pull request kubernetes-client#697 from brendandburns/pf
Fix error handling.
2 parents caa21ed + e8e510c commit 10320ef

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

examples/src/main/java/io/kubernetes/client/examples/WebSocketsExample.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public void close() {
4949

5050
public void bytesMessage(InputStream is) {}
5151

52+
public void failure(Exception ex) {
53+
ex.printStackTrace();
54+
}
55+
5256
public void textMessage(Reader in) {
5357
try {
5458
BufferedReader reader = new BufferedReader(in);

util/src/main/java/io/kubernetes/client/Copy.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public void copyDirectoryFromPod(
143143
}
144144
}
145145
}
146+
try {
147+
int status = proc.waitFor();
148+
if (status != 0) {
149+
throw new IOException("Copy command failed with status " + status);
150+
}
151+
} catch (InterruptedException ex) {
152+
throw new IOException(ex);
153+
}
146154
}
147155

148156
public static void copyFileFromPod(String namespace, String pod, String srcPath, Path dest)

util/src/main/java/io/kubernetes/client/Exec.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ protected void handleMessage(int stream, InputStream inStream) throws IOExceptio
278278
} else super.handleMessage(stream, inStream);
279279
}
280280

281+
@Override
282+
public void failure(Exception ex) {
283+
super.failure(ex);
284+
// TODO, it's possible we should suppress this error message, but currently there's
285+
// no good place to surface the message, and without it, this will be really hard to
286+
// debug.
287+
ex.printStackTrace();
288+
synchronized (ExecProcess.this) {
289+
// Try for a pretty unique error code, so if someone searches they'll find this
290+
// code.
291+
statusCode = -1975219;
292+
isAlive = false;
293+
ExecProcess.this.notifyAll();
294+
}
295+
}
296+
281297
@Override
282298
public void close() {
283299
// notify of process completion

util/src/main/java/io/kubernetes/client/util/WebSocketStreamHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class WebSocketStreamHandler implements WebSockets.SocketListener, Closea
4343
private final Map<Integer, PipedOutputStream> pipedOutput = new HashMap<>();
4444
private final Map<Integer, OutputStream> output = new HashMap<>();
4545
private WebSocket socket;
46+
private Exception error;
4647

4748
@SuppressWarnings("unused")
4849
private String protocol;
@@ -88,6 +89,15 @@ protected void handleMessage(int stream, InputStream inStream) throws IOExceptio
8889
ByteStreams.copy(inStream, out);
8990
}
9091

92+
@Override
93+
public void failure(Exception ex) {
94+
this.error = ex;
95+
}
96+
97+
public Exception getError() {
98+
return this.error;
99+
}
100+
91101
@Override
92102
public synchronized void close() {
93103
if (state != State.CLOSED) {

util/src/main/java/io/kubernetes/client/util/WebSockets.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public interface SocketListener {
6262
*/
6363
public void textMessage(Reader in);
6464

65+
/**
66+
* Called when there has been a failure
67+
*
68+
* @param the exception associated with the failure.
69+
*/
70+
public void failure(Exception ex);
71+
6572
/** Called when the stream is closed. */
6673
public void close();
6774
}
@@ -147,7 +154,7 @@ public void onClose(int code, String reason) {
147154

148155
@Override
149156
public void onFailure(IOException e, Response res) {
150-
e.printStackTrace();
157+
listener.failure(e);
151158
listener.close();
152159
}
153160
}

util/src/test/java/io/kubernetes/client/ExecTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
152152
.withQueryParam("tty", equalTo("false"))
153153
.withQueryParam("command", equalTo("cmd")));
154154

155-
assertEquals(-1, p.exitValue());
155+
assertEquals(-1975219, p.exitValue());
156156
}
157157

158158
@Test

0 commit comments

Comments
 (0)