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

Feature: add foldCause to HttpError #1066

Merged
merged 2 commits into from
Feb 20, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions zio-http/src/main/scala/zhttp/http/HttpError.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package zhttp.http

import zhttp.http.HttpError.HTTPErrorWithCause

sealed abstract class HttpError(val status: Status, val message: String) extends Throwable(message) { self =>
def foldCause[A](a: A)(f: Throwable => A): A = self match {
tusharmath marked this conversation as resolved.
Show resolved Hide resolved
case error: HTTPErrorWithCause =>
error.cause match {
case Some(throwable) => f(throwable)
case None => a
}
case _ => a

}

def toResponse: Response = Response.fromHttpError(self)
}

Expand Down
21 changes: 21 additions & 0 deletions zio-http/src/test/scala/zhttp/http/HttpErrorSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package zhttp.http

import zio.test.Assertion.equalTo
import zio.test.{DefaultRunnableSpec, assert}

object HttpErrorSpec extends DefaultRunnableSpec {
def spec = suite("HttpError") {
suite("foldCause") {
test("should fold the cause") {
val error = HttpError.InternalServerError(cause = Option(new Error("Internal server error")))
val result = error.foldCause("")(cause => cause.getMessage)
assert(result)(equalTo("Internal server error"))
} +
test("should fold with no cause") {
val error = HttpError.NotFound(!!)
val result = error.foldCause("Page not found")(cause => cause.getMessage)
assert(result)(equalTo("Page not found"))
}
}
}
}