Skip to content

Commit

Permalink
feat: Better error handling for connection refused (#47)
Browse files Browse the repository at this point in the history
PRISM Mediator unhandled exception when recipient connection refused
Better at error handling when recipient connection is refused
For ATL-4788

Signed-off-by: Fabio <Pinheiro>
  • Loading branch information
FabioPinheiro authored and Fabio committed Apr 30, 2024
1 parent 7c24e54 commit 8149c69
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MessageDispatcherJVM(client: Client) extends MessageDispatcher {
/*context*/
destination: String,
xForwardedHost: Option[String],
): ZIO[Any, DidFail, String] = {
): ZIO[Any, DispatcherError, String] = {
val contentTypeHeader = Headers.contentType(msg.`protected`.obj.typ.getOrElse(MediaTypes.ENCRYPTED).typ)
val xForwardedHostHeader = Headers(xForwardedHost.map(x => Header(MyHeaders.xForwardedHost, x)))
for {
Expand All @@ -37,13 +37,13 @@ class MessageDispatcherJVM(client: Client) extends MessageDispatcher {
content = Body.fromCharSequence(msg.toJson),
)
.tapError(ex => ZIO.logWarning(s"Fail when calling '$destination': ${ex.toString}"))
.mapError(ex => SomeThrowable(ex))
.mapError(ex => DispatcherError(ex))
data <- res.body.asString
.tapError(ex => ZIO.logError(s"Fail parce http response body: ${ex.toString}"))
.mapError(ex => SomeThrowable(ex))
.mapError(ex => DispatcherError(ex))
_ <- res.status.isError match
case true => ZIO.logError(data)
case true => ZIO.logWarning(data)
case false => ZIO.logInfo(data)
} yield (data)
}.provideEnvironment(ZEnvironment(client)) // .host()
}.provideEnvironment(ZEnvironment(client))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.iohk.atala.mediator.comm

case class DispatcherError(error: String)
object DispatcherError {
def apply(throwable: Throwable) = new DispatcherError(throwable.getClass.getName() + ":" + throwable.getMessage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ trait MessageDispatcher {
/*context*/
destination: String,
xForwardedHost: Option[String],
): ZIO[Any, DidFail, String]
): ZIO[Any, DispatcherError, String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ trait ProtocolExecuterWithServices[-R <: ProtocolExecuter.Services] extends Prot
// Some(url.drop(8).split(':').head.split('/').head)
// case _ => None
)
.mapError(fail => MediatorDidError(fail))
.catchAll { case DispatcherError(error) => ZIO.logWarning(s"Dispatch Error: $error") }
}

} yield (jobToRun)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ case class MediatorAgent(
)
)

// private def _didSubjectAux = id
// private def _keyStoreAux = keyStore.keys.toSeq
// val indentityLayer = ZLayer.succeed(new Agent {
// override def id: DID = _didSubjectAux
// override def keys: Seq[PrivateKey] = _keyStoreAux
// })

val messageDispatcherLayer: ZLayer[Client, MediatorThrowable, MessageDispatcher] =
MessageDispatcherJVM.layer.mapError(ex => MediatorThrowable(ex))

Expand Down Expand Up @@ -278,10 +271,7 @@ object MediatorAgent {
Request,
Response
]
} /* ++ Http.fromResource(s"public/webapp-fastopt-bundle.js.gz").when {
case Method.GET -> !! / "public" / "webapp-fastopt-bundle.js.gz" => true
case _ => false
} */ ++ Http
} ++ Http
.fromResource(s"public/webapp-fastopt-bundle.js.gz")
.map(e =>
e.setHeaders(
Expand All @@ -297,19 +287,6 @@ object MediatorAgent {
case Method.GET -> !! / "public" / "webapp-fastopt-bundle.js" => true
case _ => false
}
// ++ {
// Http
// .fromResource(s"public/webapp-fastopt-bundle.js")
// // .map(e => e.addHeader())
// .when {
// case Method.GET -> !! / "public" / path => true
// // Response(
// // body = Body.fromStream(ZStream.fromIterator(Source.fromResource(s"public/$path").iter).map(_.toByte)),
// // headers = Headers(HeaderNames.contentType, HeaderValues.applicationJson),
// // )
// case _ => false
// }
// }
@@
HttpAppMiddleware.cors(
zio.http.middleware.Cors.CorsConfig(
Expand Down
5 changes: 1 addition & 4 deletions webapp/src/main/scala/io/iohk/atala/mediator/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ object App {
div(
AppUtils.drawer(linkPages, MyRouter.router.currentPageSignal),
AppUtils.drawerScrim,
AppUtils.topBarHeader(MyRouter.router.currentPageSignal.map { case p: MediatorPage.type =>
"IOHK DID Comm Mediator"
// case p => p.title
}),
AppUtils.topBarHeader(MyRouter.router.currentPageSignal.map { case p: MediatorPage.type => "IOHK Mediator" }),
mainTag(
className("mdc-top-app-bar--fixed-adjust"),
child <-- $selectedApp.signal
Expand Down

0 comments on commit 8149c69

Please sign in to comment.