Skip to content

Commit

Permalink
Merge pull request http4s/http4s#1424 from aeons/kleislis-all-over
Browse files Browse the repository at this point in the history
Deprecate Service and remove MaybeResponse
  • Loading branch information
aeons authored Oct 2, 2017
2 parents 4efc25e + f75849b commit ddd3551
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions servlet/src/main/scala/org/http4s/servlet/Http4sServlet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class Http4sServlet[F[_]](
val response =
Response[F](Status.BadRequest)
.withBody(parseFailure.sanitized)
.widen[MaybeResponse[F]]
renderResponse(response, servletResponse, bodyWriter)
}

Expand All @@ -101,11 +100,12 @@ class Http4sServlet[F[_]](
val response = F.shift(executionContext) >> F
.delay(
try serviceFn(request)
// Handle message failures coming out of the service as failed tasks
.recoverWith(serviceErrorHandler(request).andThen(_.widen[MaybeResponse[F]]))
.getOrElse(Response.notFound)
// Handle message failures coming out of the service as failed tasks
.recoverWith(serviceErrorHandler(request))
catch
// Handle message failures _thrown_ by the service, just in case
serviceErrorHandler(request).andThen(_.widen[MaybeResponse[F]])
serviceErrorHandler(request)
)
.flatten

Expand All @@ -123,7 +123,6 @@ class Http4sServlet[F[_]](
val response =
Response[F](Status.InternalServerError)
.withBody("Service timed out.")
.widen[MaybeResponse[F]]
renderResponse(response, servletResponse, bodyWriter)
} else {
logger.warn(
Expand All @@ -138,17 +137,16 @@ class Http4sServlet[F[_]](
}

private def renderResponse(
response: F[MaybeResponse[F]],
response: F[Response[F]],
servletResponse: HttpServletResponse,
bodyWriter: BodyWriter[F]): F[Unit] =
response.flatMap { maybeResponse =>
val r = maybeResponse.orNotFound
response.flatMap { resp =>
// Note: the servlet API gives us no undeprecated method to both set
// a body and a status reason. We sacrifice the status reason.
servletResponse.setStatus(r.status.code)
for (header <- r.headers if header.isNot(`Transfer-Encoding`))
servletResponse.setStatus(resp.status.code)
for (header <- resp.headers if header.isNot(`Transfer-Encoding`))
servletResponse.addHeader(header.name.toString, header.value)
bodyWriter(r)
bodyWriter(resp)
}

private def errorHandler(
Expand All @@ -159,7 +157,7 @@ class Http4sServlet[F[_]](

case t: Throwable =>
logger.error(t)("Error processing request")
val response = F.pure(Response[F](Status.InternalServerError).asMaybeResponse)
val response = F.pure(Response[F](Status.InternalServerError))
// We don't know what I/O mode we're in here, and we're not rendering a body
// anyway, so we use a NullBodyWriter.
async
Expand Down

0 comments on commit ddd3551

Please sign in to comment.