Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cancel HttpError #2319

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/dsl/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ Response.text("Hello World!").status(Status.NOT_FOUND)
Response.ok.updateHeaders(_ => Headers("key", "value"))
```

### Response from HttpError
### Response from Http Errors

`fromHttpError` creates a response with provided `HttpError`
`error` creates a response with a provided status code and message.

```scala mdoc
Response.fromHttpError(HttpError.BadRequest())
Response.error(Status.BadRequest, "It's not good!")
```

## Adding Cookie to Response
Expand Down
4 changes: 2 additions & 2 deletions docs/dsl/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ Handler.status(Status.Ok)

### Handler.error

Creates a `Handler` that always fails with the given `HttpError`.
Creates a `Handler` that always fails with the given error.

```scala mdoc:silent
Handler.error(HttpError.Forbidden())
Handler.error(Status.Forbidden)
```

### Handler.response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class EndpointBenchmark {
Method.GET / "first" / int("id1") / "second" / int("id2") / "third" / int("id3") / "fourth" / int(
"id4",
) / "fifth" / int("id5") / "sixth" / int("id6") / "seventh" / int("id7") ->
handler { (id1: Int, id2: Int, id3: Int, id4: Int, id5: Int, id6: Int, id7: Int, req: Request) =>
handler { (_: Int, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int, _: Request) =>
ZIO.succeed(Response.ok)
},
).toHttpApp
Expand Down
47 changes: 37 additions & 10 deletions zio-http/src/main/scala/zio/http/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,17 @@ object Handler {
}
}

/**
* Creates a handler which always responds with a 400 status code.
*/
def badRequest: Handler[Any, Nothing, Any, Response] =
error(Status.BadRequest)

/**
* Creates a handler which always responds with a 400 status code.
*/
def badRequest(message: => String): Handler[Any, Nothing, Any, Response] =
error(HttpError.BadRequest(message))
error(Status.BadRequest, message)

/**
* Returns a handler that dies with the specified `Throwable`. This method can
Expand All @@ -739,16 +745,16 @@ object Handler {
die(new RuntimeException(message))

/**
* Creates a handler with HttpError.
* Creates a handler with an error and the default error message.
*/
def error(error: => HttpError): Handler[Any, Nothing, Any, Response] =
response(Response.fromHttpError(error))
def error(status: => Status.Error): Handler[Any, Nothing, Any, Response] =
response(Response.error(status))

/**
* Creates a handler that responds with 500 status code
* Creates a handler with an error and the specified error message.
*/
def errorMessage(message: => String): Handler[Any, Nothing, Any, Response] =
error(HttpError.InternalServerError(message))
def error(status: => Status.Error, message: => String): Handler[Any, Nothing, Any, Response] =
response(Response.error(status, message))

/**
* Creates a Handler that always fails
Expand All @@ -773,11 +779,17 @@ object Handler {
}
}

/**
* Creates a handler that responds with 403 - Forbidden status code
*/
def forbidden: Handler[Any, Nothing, Any, Response] =
error(Status.Forbidden)

/**
* Creates a handler that responds with 403 - Forbidden status code
*/
def forbidden(message: => String): Handler[Any, Nothing, Any, Response] =
error(HttpError.Forbidden(message))
error(Status.Forbidden, message)

def from[H](handler: => H)(implicit h: ToHandler[H]): Handler[h.Env, h.Err, h.In, h.Out] =
h.toHandler(handler)
Expand Down Expand Up @@ -984,21 +996,36 @@ object Handler {
override def apply(in: A): ZIO[Any, Nothing, A] = Exit.succeed(in)
}

def internalServerError: Handler[Any, Nothing, Any, Response] =
error(Status.InternalServerError)

def internalServerError(message: => String): Handler[Any, Nothing, Any, Response] =
error(Status.InternalServerError, message)

/**
* Creates a handler which always responds with a 405 status code.
*/
def methodNotAllowed: Handler[Any, Nothing, Any, Response] =
error(Status.MethodNotAllowed)

/**
* Creates a handler which always responds with a 405 status code.
*/
def methodNotAllowed(message: => String): Handler[Any, Nothing, Any, Response] =
error(HttpError.MethodNotAllowed(message))
error(Status.MethodNotAllowed, message)

/**
* Creates a handler that fails with a NotFound exception.
*/
def notFound: Handler[Any, Nothing, Request, Response] =
Handler
.fromFunctionHandler[Request] { request =>
error(HttpError.NotFound(request.url.path.encode))
error(Status.NotFound, request.url.path.encode)
}

def notFound(message: => String): Handler[Any, Nothing, Any, Response] =
error(Status.NotFound, message)

/**
* Creates a handler which always responds with a 200 status code.
*/
Expand Down
22 changes: 3 additions & 19 deletions zio-http/src/main/scala/zio/http/HandlerAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -906,19 +906,14 @@ private[http] trait HandlerAspects extends zio.http.internal.HeaderModifier[Hand

private def replaceErrorResponse(request: Request, response: Response): Response = {
def htmlResponse: Body = {
val message: String = response.httpError.map(_.message).getOrElse("")
val message: String = response.header(Header.Warning).map(_.text).getOrElse("")
val data = Template.container(s"${response.status}") {
div(
div(
styles := Seq("text-align" -> "center"),
div(s"${response.status.code}", styles := Seq("font-size" -> "20em")),
div(message),
),
div(
response.httpError.get.foldCause(div()) { throwable =>
div(h3("Cause:"), pre(prettify(throwable)))
},
),
)
}
Body.fromString("<!DOCTYPE html>" + data.encode)
Expand Down Expand Up @@ -947,23 +942,12 @@ private[http] trait HandlerAspects extends zio.http.internal.HeaderModifier[Hand
response
}

private def prettify(throwable: Throwable): String = {
val sw = new StringWriter
throwable.printStackTrace(new PrintWriter(sw))
s"${sw.toString}"
}

private def formatCause(response: Response): String =
response.httpError.get.foldCause("") { throwable =>
s"${scala.Console.BOLD}Cause: ${scala.Console.RESET}\n ${prettify(throwable)}"
}

private def formatErrorMessage(response: Response) = {
val errorMessage: String = response.httpError.map(_.message).getOrElse("")
val errorMessage: String = response.header(Header.Warning).map(_.text).getOrElse("")
val status = response.status.code
s"${scala.Console.BOLD}${scala.Console.RED}${response.status} ${scala.Console.RESET} - " +
s"${scala.Console.BOLD}${scala.Console.CYAN}$status ${scala.Console.RESET} - " +
s"$errorMessage\n${formatCause(response)}"
s"$errorMessage"
}

private[http] val defaultBoundaries = MetricKeyType.Histogram.Boundaries.fromChunk(
Expand Down
148 changes: 0 additions & 148 deletions zio-http/src/main/scala/zio/http/HttpError.scala

This file was deleted.