Skip to content

Commit

Permalink
Propagate error response body to RTE (Issue scalaj#134)
Browse files Browse the repository at this point in the history
Propagate response body to RuntimException to make sure the entire
reponse is logged. Logging usually revolves around getMessage whereas
the current code only overrides toString via Scala case class.
  • Loading branch information
gerashegalov committed Jul 16, 2018
1 parent 8520c03 commit 6aa2369
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/scalaj/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ case class HttpStatusException(
code: Int,
statusLine: String,
body: String
) extends RuntimeException(code + " Error: " + statusLine)
) extends RuntimeException(code + " Error: " + statusLine + " Details: " + body)

/** Result of executing a [[scalaj.http.HttpRequest]]
* @tparam T the body response since it can be parsed directly to things other than String
Expand Down
13 changes: 11 additions & 2 deletions src/test/scala/scalaj/http/HttpTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package scalaj.http
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, IOException}
import java.net.{HttpCookie, InetSocketAddress, Proxy}
import java.util.zip.GZIPOutputStream

import javax.servlet.{ServletRequest, ServletResponse}
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}

import org.eclipse.jetty.security.{ConstraintMapping, ConstraintSecurityHandler, HashLoginService}
import org.eclipse.jetty.security.authentication.{BasicAuthenticator, DigestAuthenticator, LoginAuthenticator}
import org.eclipse.jetty.server.{Request, Server}
Expand All @@ -15,9 +15,10 @@ import org.eclipse.jetty.servlets.ProxyServlet
import org.eclipse.jetty.util.security.{Constraint, Credential}
import org.junit.Assert._
import org.junit.Test

import scalaj.http.HttpConstants._

import scala.util.Try


class HttpTest {

Expand Down Expand Up @@ -470,6 +471,14 @@ class HttpTest {
HttpResponse("hi", 400, Map.empty).throwError
}

@Test
def throwServerErrorThrowsWithStatusAndBody: Unit = {
val httpResp = HttpResponse("stacktrace", 400, Map("Status" -> IndexedSeq("HTTP/1.1 400 Bad Request")))
val statusException = Try(httpResp.throwError).failed.get
assertEquals(s"${httpResp.code} Error: ${httpResp.headers("Status")(0)} Details: ${httpResp.body}",
statusException.getMessage)
}

@Test
def throwErrorOkWith200: Unit = {
assertEquals(200, HttpResponse("hi", 200, Map.empty).throwError.code)
Expand Down

0 comments on commit 6aa2369

Please sign in to comment.