Skip to content

Commit f582537

Browse files
authored
remove TamerError from signatures, run and runWith lift errors into failures, examples remove .exitCode which converts failures into exit code 1 and swallows traces (#1377)
1 parent 544ba80 commit f582537

File tree

20 files changed

+42
-62
lines changed

20 files changed

+42
-62
lines changed

core/src/main/scala/tamer/Registry.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ object Registry {
9292
override final def verifySchema(id: Int, schema: Schema): Task[Unit] = ZIO.unit
9393
}
9494

95-
private[tamer] final val fakeRegistryZIO: ZIO[Scope, TamerError, Registry] = ZIO.succeed(Registry.FakeRegistry)
95+
private[tamer] final val fakeRegistryZIO: RIO[Scope, Registry] = ZIO.succeed(Registry.FakeRegistry)
9696
}
9797

98-
final case class RegistryProvider(from: RegistryConfig => ZIO[Scope, TamerError, Registry])
98+
final case class RegistryProvider(from: RegistryConfig => RIO[Scope, Registry])
9999

100100
object RegistryProvider {
101101
implicit final val defaultRegistryProvider: RegistryProvider = RegistryProvider { config =>

core/src/main/scala/tamer/Serdes.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ sealed trait Serdes[K, V, SV] {
3232
" - \u001b[36mtamer.RegistryProvider\u001b[0m\n"
3333
)
3434
sealed trait SerdesProvider[K, V, SV] {
35-
def using(maybeRegistryConfig: Option[RegistryConfig]): ZIO[Scope, TamerError, Serdes[K, V, SV]]
35+
def using(maybeRegistryConfig: Option[RegistryConfig]): RIO[Scope, Serdes[K, V, SV]]
3636
}
3737

3838
object SerdesProvider {
3939
implicit final def serdesProviderFromCodecs[K: Codec, V: Codec, SV: Codec](
4040
implicit SK: Codec[Tamer.StateKey],
4141
registryProvider: RegistryProvider
4242
): SerdesProvider[K, V, SV] = new SerdesProvider[K, V, SV] {
43-
override final def using(maybeRegistryConfig: Option[RegistryConfig]): ZIO[Scope, TamerError, Serdes[K, V, SV]] =
43+
override final def using(maybeRegistryConfig: Option[RegistryConfig]): RIO[Scope, Serdes[K, V, SV]] =
4444
maybeRegistryConfig.fold(Registry.fakeRegistryZIO)(registryProvider.from(_)).map { registry =>
4545
new Serdes[K, V, SV] {
4646
override final val keySerializer: Serializer[Any, K] = Serde.key[K].using(registry)

core/src/main/scala/tamer/Setup.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ abstract class Setup[-R, K: Tag, V: Tag, SV: Tag](implicit val serdesProvider: S
99
val repr: String = "no repr string implemented, if you want a neat description of the source configuration please implement it"
1010
def iteration(currentState: SV, queue: Enqueue[NonEmptyChunk[(K, V)]]): RIO[R, SV]
1111

12-
final val run: ZIO[R with KafkaConfig, TamerError, Unit] = runLoop.provideSomeLayer(Tamer.live(this))
13-
final def runWith[E >: TamerError, R1](layer: Layer[E, R with KafkaConfig with R1]): IO[E, Unit] =
14-
runLoop.provideLayer(layer >>> Tamer.live(this))
12+
final val run: RIO[R with KafkaConfig, Unit] = runLoop.provideSomeLayer(Tamer.live(this)).orDie
13+
final def runWith[R1](layer: TaskLayer[R with KafkaConfig with R1]): Task[Unit] = runLoop.provideLayer(layer >>> Tamer.live(this)).orDie
1514
}

core/src/main/scala/tamer/Tamer.scala

+10-23
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import zio.kafka.serde.{Serde => ZSerde, Serializer}
1313
import zio.stream.{Stream, ZStream}
1414

1515
trait Tamer {
16-
def runLoop: IO[TamerError, Unit]
16+
def runLoop: Task[Unit]
1717
}
1818

1919
object Tamer {
@@ -24,11 +24,6 @@ object Tamer {
2424

2525
private[this] final val tenTimes = Schedule.recurs(10L) && Schedule.exponential(100.milliseconds) // FIXME make configurable
2626

27-
private[this] final val tamerErrors: PartialFunction[Throwable, TamerError] = {
28-
case ke: KafkaException => TamerError(ke.getLocalizedMessage, ke)
29-
case te: TamerError => te
30-
}
31-
3227
private[this] implicit final class OffsetOps(private val _underlying: Offset) extends AnyVal {
3328
def info: String = s"${_underlying.topicPartition}@${_underlying.offset}"
3429
}
@@ -203,16 +198,12 @@ object Tamer {
203198
runSink <&> runSource
204199
}
205200

206-
override val runLoop: IO[TamerError, Unit] = {
207-
val logic = for {
208-
log <- logTask
209-
_ <- log.info(s"initializing Tamer with setup: \n$repr")
210-
queue <- Queue.bounded[(TransactionInfo, Chunk[(K, V)])](config.bufferSize)
211-
_ <- runLoop(queue, log)
212-
} yield ()
213-
214-
logic.refineOrDie(tamerErrors)
215-
}
201+
override val runLoop: Task[Unit] = for {
202+
log <- logTask
203+
_ <- log.info(s"initializing Tamer with setup: \n$repr")
204+
queue <- Queue.bounded[(TransactionInfo, Chunk[(K, V)])](config.bufferSize)
205+
_ <- runLoop(queue, log)
206+
} yield ()
216207
}
217208

218209
object LiveTamer {
@@ -224,7 +215,7 @@ object Tamer {
224215
stateKey: Int,
225216
iterationFunction: (SV, Enqueue[NonEmptyChunk[(K, V)]]) => Task[SV],
226217
repr: String
227-
): ZIO[Scope, TamerError, LiveTamer[K, V, SV]] = {
218+
): RIO[Scope, LiveTamer[K, V, SV]] = {
228219

229220
val KafkaConfig(brokers, _, closeTimeout, _, _, StateConfig(_, groupId, clientId), transactionalId, properties) = config
230221

@@ -251,9 +242,7 @@ object Tamer {
251242
.mapError(TamerError("Could not build Kafka client", _))
252243
}
253244

254-
private[tamer] final def getLayer[R, K: Tag, V: Tag, SV: Tag](
255-
setup: Setup[R, K, V, SV]
256-
): ZLayer[R with KafkaConfig, TamerError, Tamer] =
245+
private[tamer] final def getLayer[R, K: Tag, V: Tag, SV: Tag](setup: Setup[R, K, V, SV]): RLayer[R with KafkaConfig, Tamer] =
257246
ZLayer.scoped[R with KafkaConfig] {
258247
for {
259248
config <- ZIO.service[KafkaConfig]
@@ -265,7 +254,5 @@ object Tamer {
265254
}
266255
}
267256

268-
final def live[R, K: Tag, V: Tag, SV: Tag](
269-
setup: Setup[R, K, V, SV]
270-
): ZLayer[R with KafkaConfig, TamerError, Tamer] = LiveTamer.getLayer(setup)
257+
final def live[R, K: Tag, V: Tag, SV: Tag](setup: Setup[R, K, V, SV]): RLayer[R with KafkaConfig, Tamer] = LiveTamer.getLayer(setup)
271258
}

core/src/main/scala/tamer/config.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ object KafkaConfig {
6969
KafkaConfig(brokers, maybeRegistry, closeTimeout, bufferSize, sink, state, transactionalId)
7070
}.nested("kafka")
7171

72-
final val fromEnvironment: Layer[TamerError, KafkaConfig] = ZLayer {
72+
final val fromEnvironment: TaskLayer[KafkaConfig] = ZLayer {
7373
ZIO.config(kafkaConfigValue).mapError(ce => TamerError(ce.getMessage(), ce))
7474
}
7575
}

core/src/main/scala/tamer/package.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import zio.ZIO
1+
import zio.{RIO, ZIO}
22

33
package object tamer {
4-
final val runLoop: ZIO[Tamer, TamerError, Unit] = ZIO.serviceWithZIO(_.runLoop)
4+
final val runLoop: RIO[Tamer, Unit] = ZIO.serviceWithZIO(_.runLoop)
55

66
implicit final class HashableOps[A](private val _underlying: A) extends AnyVal {
77
def hash(implicit A: Hashable[A]): Int = A.hash(_underlying)

core/src/test/scala/tamer/TamerSpec.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object TamerSpec extends ZIOSpecDefault with TamerSpecGen {
1919
override final val initialState = State(0)
2020
override final val stateKey = 0
2121
override final val recordKey = (s: State, _: Value) => Key(s.state + 1)
22-
override final def iteration(s: State, q: Enqueue[NonEmptyChunk[(Key, Value)]]): ZIO[Ref[Log], TamerError, State] =
22+
override final def iteration(s: State, q: Enqueue[NonEmptyChunk[(Key, Value)]]): RIO[Ref[Log], State] =
2323
ZIO.service[Ref[Log]].flatMap { variable =>
2424
val cursor = s.state + 1
2525
if (cursor <= 10)

db/src/main/scala/tamer/db/config.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object DbConfig {
1616
DbConfig(driver, uri, username, password, fetchChunkSize)
1717
}
1818

19-
final val fromEnvironment: Layer[TamerError, DbConfig] = ZLayer {
19+
final val fromEnvironment: TaskLayer[DbConfig] = ZLayer {
2020
ZIO.config(dbConfigValue).mapError(ce => TamerError(ce.getMessage(), ce))
2121
}
2222
}

db/src/main/scala/tamer/db/package.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ package object db {
1919
def -(d: Duration): Instant = instant.minus(d)
2020
}
2121

22-
final val hikariLayer: ZLayer[DbConfig, TamerError, Transactor[Task]] =
23-
ZLayer.scoped.apply {
22+
final val hikariLayer: RLayer[DbConfig, Transactor[Task]] =
23+
ZLayer.scoped {
2424
ZIO
2525
.service[DbConfig]
2626
.zip(ZIO.descriptor.map(_.executor.asExecutionContext))
2727
.flatMap { case (config, ec) => mkTransactor(config, ec) }
2828
}
2929

30-
final def mkTransactor(config: DbConfig, connectEC: ExecutionContext): ZIO[Scope, TamerError, HikariTransactor[Task]] =
30+
final def mkTransactor(config: DbConfig, connectEC: ExecutionContext): RIO[Scope, HikariTransactor[Task]] =
3131
newHikariTransactor[Task](config.driver, config.uri, config.username, config.password.value.asString, connectEC).toScopedZIO
3232
.refineToOrDie[SQLException]
3333
.mapError(sqle => TamerError(sqle.getLocalizedMessage, sqle))
3434

35-
final val dbLayerFromEnvironment: Layer[TamerError, DbConfig with Transactor[Task]] =
35+
final val dbLayerFromEnvironment: TaskLayer[DbConfig with Transactor[Task]] =
3636
DbConfig.fromEnvironment >+> hikariLayer
3737
}

example/src/main/scala/tamer/db/DatabaseGeneralized.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ object DatabaseGeneralized extends ZIOAppDefault {
2020
val mostRecent = results.sortBy(_.modifiedAt).max.timestamp
2121
Clock.instant.map(now => MyState(mostRecent, (mostRecent + 5.minutes).or(now)))
2222
}
23-
).runWith(dbLayerFromEnvironment ++ KafkaConfig.fromEnvironment).exitCode
23+
).runWith(dbLayerFromEnvironment ++ KafkaConfig.fromEnvironment)
2424
}
2525
}

example/src/main/scala/tamer/db/DatabaseSimple.scala

-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ object DatabaseSimple extends ZIOAppDefault {
1515
sql"""SELECT id, name, description, modified_at FROM users WHERE modified_at > ${window.from} AND modified_at <= ${window.to}""".query[Row]
1616
)(recordKey = (_, v) => v.id, from = Instant.parse("2020-01-01T00:00:00.00Z"), tumblingStep = 5.days)
1717
.runWith(dbLayerFromEnvironment ++ KafkaConfig.fromEnvironment)
18-
.exitCode
1918
}

example/src/main/scala/tamer/oci/objectstorage/OciObjectStorageSimple.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object OciObjectStorageSimple extends ZIOAppDefault {
2222
},
2323
objectName = _.current,
2424
startAfter = _.startAfter
25-
).runWith(objectStorageLayer(US_PHOENIX_1, ObjectStorageAuth.fromConfigFileDefaultProfile) ++ KafkaConfig.fromEnvironment).exitCode
25+
).runWith(objectStorageLayer(US_PHOENIX_1, ObjectStorageAuth.fromConfigFileDefaultProfile) ++ KafkaConfig.fromEnvironment)
2626
}

example/src/main/scala/tamer/rest/RESTBasicAuth.scala

-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ object RESTBasicAuth extends ZIOAppDefault {
3131
increment = 2
3232
)
3333
.runWith(restLive() ++ KafkaConfig.fromEnvironment)
34-
.exitCode
3534
}

example/src/main/scala/tamer/rest/RESTCustomAuth.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object RESTCustomAuth extends ZIOAppDefault {
1818

1919
val authentication: Authentication[SttpClient] = new Authentication[SttpClient] {
2020
override def addAuthentication(request: SttpRequest, bearerToken: Option[String]): SttpRequest = request.auth.bearer(bearerToken.getOrElse(""))
21-
override def setSecret(secretRef: Ref[Option[String]]): ZIO[SttpClient, TamerError, Unit] = {
21+
override def setSecret(secretRef: Ref[Option[String]]): RIO[SttpClient, Unit] = {
2222
val fetchToken = send(basicRequest.get(uri"http://localhost:9395/auth").auth.basic("user", "pass"))
2323
.flatMap(_.body match {
2424
case Left(error) => ZIO.fail(TamerError(error))
@@ -43,5 +43,4 @@ object RESTCustomAuth extends ZIOAppDefault {
4343
increment = 2
4444
)
4545
.runWith(restLive() ++ KafkaConfig.fromEnvironment)
46-
.exitCode
4746
}

example/src/main/scala/tamer/rest/RESTDynamicData.scala

+9-10
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ object RESTDynamicData extends ZIOAppDefault {
1515
ZIO.attempt(body.split(",").toList.filterNot(_.isBlank))
1616
}
1717

18-
def program(now: Instant) = RESTSetup
19-
.periodicallyPaginated(
20-
baseUrl = "http://localhost:9395/dynamic-pagination",
21-
pageDecoder = pageDecoder,
22-
periodStart = now
23-
)((_, data) => data)
24-
.runWith(restLive() ++ KafkaConfig.fromEnvironment)
25-
.exitCode
26-
27-
override final val run = Clock.instant.flatMap(program(_))
18+
override final val run = Clock.instant.flatMap { now =>
19+
RESTSetup
20+
.periodicallyPaginated(
21+
baseUrl = "http://localhost:9395/dynamic-pagination",
22+
pageDecoder = pageDecoder,
23+
periodStart = now
24+
)((_, data) => data)
25+
.runWith(restLive() ++ KafkaConfig.fromEnvironment)
26+
}
2827
}

example/src/main/scala/tamer/rest/RESTSimple.scala

-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ object RESTSimple extends ZIOAppDefault {
2020
fixedPageElementCount = Some(3)
2121
)
2222
.runWith(restLive() ++ KafkaConfig.fromEnvironment)
23-
.exitCode
2423
}

example/src/main/scala/tamer/s3/S3Generalized.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ object S3Generalized extends ZIOAppDefault {
4848
recordKey = (l: Long, _: String) => l,
4949
selectObjectForState = (l: Long, _: Keys) => internals.selectObjectForInstant(l),
5050
stateFold = internals.getNextState
51-
).runWith(Scope.default >>> (liveZIO(AF_SOUTH_1, s3.providers.default, Some(new URI("http://localhost:9000"))) ++ myKafkaConfigLayer)).exitCode
51+
).runWith(Scope.default >>> (liveZIO(AF_SOUTH_1, s3.providers.default, Some(new URI("http://localhost:9000"))) ++ myKafkaConfigLayer))
5252
}

example/src/main/scala/tamer/s3/S3Simple.scala

-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ object S3Simple extends ZIOAppDefault {
1919
dateTimeFormatter = ZonedDateTimeFormatter.fromPattern("yyyy-MM-dd HH:mm:ss", ZoneId.of("Europe/Rome"))
2020
)
2121
.runWith(Scope.default >>> (liveZIO(AF_SOUTH_1, s3.providers.default, Some(new URI("http://localhost:9000"))) ++ KafkaConfig.fromEnvironment))
22-
.exitCode
2322
}

oci-objectstorage/src/main/scala/tamer/oci/objectstorage/package.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package tamer
22
package oci
33

44
import com.oracle.bmc.Region
5-
import zio.{RIO, ZLayer}
5+
import zio.{RIO, RLayer, ZLayer}
66
import zio.oci.objectstorage._
77

88
package object objectstorage {
9-
final def objectStorageLayer[R](region: Region, auth: RIO[R, ObjectStorageAuth]): ZLayer[R, TamerError, ObjectStorage] =
9+
final def objectStorageLayer[R](region: Region, auth: RIO[R, ObjectStorageAuth]): RLayer[R, ObjectStorage] =
1010
ZLayer(auth).flatMap(auth => ObjectStorage.live(ObjectStorageSettings(region, auth.get))).mapError { e =>
1111
TamerError(e.getLocalizedMessage, e)
1212
}

rest/src/main/scala/tamer/rest/package.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import sttp.capabilities.{Effect, WebSockets}
55
import sttp.capabilities.zio.ZioStreams
66
import sttp.client4.{BackendOptions, Request, Response}
77
import sttp.client4.httpclient.zio._
8-
import zio.{Layer, Ref, Task}
8+
import zio.{Ref, Task, TaskLayer}
99

1010
package object rest {
1111
type EphemeralSecretCache = Ref[Option[String]]
@@ -16,7 +16,7 @@ package object rest {
1616
options: BackendOptions = BackendOptions.Default,
1717
customizeRequest: HttpRequest => HttpRequest = identity,
1818
customEncodingHandler: HttpClientZioBackend.ZioEncodingHandler = PartialFunction.empty
19-
): Layer[TamerError, SttpClient with EphemeralSecretCache] =
19+
): TaskLayer[SttpClient with EphemeralSecretCache] =
2020
HttpClientZioBackend
2121
.layer(options, customizeRequest, customEncodingHandler)
2222
.mapError(e => TamerError(e.getLocalizedMessage(), e)) ++ EphemeralSecretCache.live

0 commit comments

Comments
 (0)