Skip to content

Commit 220cb3c

Browse files
move connect exception inside
1 parent 35119a0 commit 220cb3c

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

core/src/main/scala/org/apache/spark/deploy/rest/StandaloneRestClient.scala

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ private[deploy] class StandaloneRestClient(master: String) extends Logging {
9090
handleUnexpectedRestResponse(unexpected)
9191
}
9292
} catch {
93-
case unreachable @ (_: FileNotFoundException | _: SocketException | _: ConnectException) =>
93+
case e: SubmitRestConnectionException =>
9494
if (handleConnectionException(m)) {
95-
throw new SubmitRestConnectionException(
96-
s"Unable to connect to server", unreachable)
95+
throw new SubmitRestConnectionException("Unable to connect to server", e)
9796
}
9897
}
9998
}
@@ -120,10 +119,9 @@ private[deploy] class StandaloneRestClient(master: String) extends Logging {
120119
handleUnexpectedRestResponse(unexpected)
121120
}
122121
} catch {
123-
case unreachable @ (_: FileNotFoundException | _: SocketException | _: ConnectException) =>
122+
case e: SubmitRestConnectionException =>
124123
if (handleConnectionException(m)) {
125-
throw new SubmitRestConnectionException(
126-
s"Unable to connect to server", unreachable)
124+
throw new SubmitRestConnectionException("Unable to connect to server", e)
127125
}
128126
}
129127
}
@@ -135,6 +133,7 @@ private[deploy] class StandaloneRestClient(master: String) extends Logging {
135133
submissionId: String,
136134
quiet: Boolean = false): SubmitRestProtocolResponse = {
137135
logInfo(s"Submitting a request for the status of submission $submissionId in $master.")
136+
138137
var handled: Boolean = false
139138
var response: SubmitRestProtocolResponse = null
140139
for (m <- masters if !handled) {
@@ -152,10 +151,9 @@ private[deploy] class StandaloneRestClient(master: String) extends Logging {
152151
handleUnexpectedRestResponse(unexpected)
153152
}
154153
} catch {
155-
case unreachable @ (_: FileNotFoundException | _: SocketException | _: ConnectException) =>
154+
case e: SubmitRestConnectionException =>
156155
if (handleConnectionException(m)) {
157-
throw new SubmitRestConnectionException(
158-
s"Unable to connect to server", unreachable)
156+
throw new SubmitRestConnectionException("Unable to connect to server", e)
159157
}
160158
}
161159
}
@@ -204,11 +202,16 @@ private[deploy] class StandaloneRestClient(master: String) extends Logging {
204202
conn.setRequestProperty("Content-Type", "application/json")
205203
conn.setRequestProperty("charset", "utf-8")
206204
conn.setDoOutput(true)
207-
val out = new DataOutputStream(conn.getOutputStream)
208-
Utils.tryWithSafeFinally {
209-
out.write(json.getBytes(Charsets.UTF_8))
210-
} {
211-
out.close()
205+
try {
206+
val out = new DataOutputStream(conn.getOutputStream)
207+
Utils.tryWithSafeFinally {
208+
out.write(json.getBytes(Charsets.UTF_8))
209+
} {
210+
out.close()
211+
}
212+
} catch {
213+
case e: ConnectException =>
214+
throw new SubmitRestConnectionException("Connect Exception when connect to server", e)
212215
}
213216
readResponse(conn)
214217
}
@@ -246,6 +249,8 @@ private[deploy] class StandaloneRestClient(master: String) extends Logging {
246249
s"Message received from server was not a response:\n${unexpected.toJson}")
247250
}
248251
} catch {
252+
case unreachable @ (_: FileNotFoundException | _: SocketException) =>
253+
throw new SubmitRestConnectionException("Unable to connect to server", unreachable)
249254
case malformed @ (_: JsonProcessingException | _: SubmitRestProtocolException) =>
250255
throw new SubmitRestProtocolException("Malformed response received from server", malformed)
251256
}

core/src/test/scala/org/apache/spark/deploy/rest/StandaloneRestSubmitSuite.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletResponse
2424
import scala.collection.mutable
2525

2626
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
27-
import com.fasterxml.jackson.core.JsonParseException
2827
import com.google.common.base.Charsets
2928
import org.scalatest.{BeforeAndAfterEach, FunSuite}
3029
import org.json4s.JsonAST._
@@ -353,7 +352,7 @@ class StandaloneRestSubmitSuite extends FunSuite with BeforeAndAfterEach {
353352
// server returns malformed response unwittingly
354353
// client should throw an appropriate exception to indicate server failure
355354
val conn1 = sendHttpRequest(submitRequestPath, "POST", json)
356-
intercept[JsonParseException] { client.readResponse(conn1) }
355+
intercept[SubmitRestProtocolException] { client.readResponse(conn1) }
357356
// server attempts to send invalid response, but fails internally on validation
358357
// client should receive an error response as server is able to recover
359358
val conn2 = sendHttpRequest(killRequestPath, "POST")
@@ -363,7 +362,7 @@ class StandaloneRestSubmitSuite extends FunSuite with BeforeAndAfterEach {
363362
// server explodes internally beyond recovery
364363
// client should throw an appropriate exception to indicate server failure
365364
val conn3 = sendHttpRequest(statusRequestPath, "GET")
366-
intercept[JsonParseException] { client.readResponse(conn3) } // empty response
365+
intercept[SubmitRestProtocolException] { client.readResponse(conn3) } // empty response
367366
assert(conn3.getResponseCode === HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
368367
}
369368

0 commit comments

Comments
 (0)