Skip to content
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ lazy val example =
"dev.zio" %% "zio-config-typesafe" % "1.0.10",
"dev.zio" %% "zio-prelude" % "1.0.0-RC7",
"dev.zio" %% "zio-json" % "0.1.5",
"io.d11" %% "zhttp" % "1.0.0.0-RC17",
"io.d11" %% "zhttp" % "1.0.0.0-RC23",
"io.github.kitlangton" %% "zio-magic" % "0.3.11"
)
)
Expand Down
18 changes: 10 additions & 8 deletions example/src/main/scala/example/ApiError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ import scala.util.control.NoStackTrace

import zhttp.http._

sealed trait ApiError extends NoStackTrace
sealed trait ApiError extends NoStackTrace { self =>
import ApiError._

final def toResponse: Response =
self match {
case CorruptedData | GithubUnreachable => Response.fromHttpError(HttpError.InternalServerError())
case CacheMiss(key) => Response.fromHttpError(HttpError.NotFound(Path.empty / key))
case UnknownProject(path) => Response.fromHttpError(HttpError.NotFound(Path(path)))
}
}

object ApiError {
final case class CacheMiss(key: String) extends ApiError
case object CorruptedData extends ApiError
case object GithubUnreachable extends ApiError
final case class UnknownProject(path: String) extends ApiError

val errorResponse: ApiError => UResponse = {
case CorruptedData | GithubUnreachable => Response.fromHttpError(HttpError.InternalServerError())
case CacheMiss(key) => Response.fromHttpError(HttpError.NotFound(Path.empty / key))
case UnknownProject(path) => Response.fromHttpError(HttpError.NotFound(Path(path)))
}

}
8 changes: 4 additions & 4 deletions example/src/main/scala/example/api/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
package example.api

import example._
import zhttp.http.{HttpApp, _}
import zhttp.http._
import zhttp.service.Server

import zio._
import zio.json._

object Api {

private val app: HttpApp[ContributorsCache, Nothing] = HttpApp.collectM {
case Method.GET -> Root / "repositories" / owner / name / "contributors" =>
private val app: HttpApp[ContributorsCache, Nothing] = Http.collectZIO {
case Method.GET -> !! / "repositories" / owner / name / "contributors" =>
ZIO
.serviceWith[ContributorsCache.Service](_.fetchAll(Repository(Owner(owner), Name(name))))
.mapBoth(ApiError.errorResponse, r => Response.jsonString(r.toJson))
.mapBoth(_.toResponse, r => Response.json(r.toJson))
.merge
}

Expand Down