From 8149c69d3a940be0f463a0c02f57afc9e5aec3a1 Mon Sep 17 00:00:00 2001 From: Fabio Pinheiro Date: Tue, 11 Jul 2023 16:01:35 +0100 Subject: [PATCH] feat: Better error handling for connection refused (#47) PRISM Mediator unhandled exception when recipient connection refused Better at error handling when recipient connection is refused For ATL-4788 Signed-off-by: Fabio --- .../mediator/comm/MessageDispatcherJVM.scala | 10 ++++---- .../atala/mediator/comm/DispatcherFail.scala | 6 +++++ .../mediator/comm/MessageDispatcher.scala | 2 +- .../mediator/actions/ProtocolExecute.scala | 2 +- .../atala/mediator/app/MediatorAgent.scala | 25 +------------------ .../scala/io/iohk/atala/mediator/App.scala | 5 +--- 6 files changed, 15 insertions(+), 35 deletions(-) create mode 100644 http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/DispatcherFail.scala diff --git a/http-utils/jvm/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcherJVM.scala b/http-utils/jvm/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcherJVM.scala index 4401ca10..ed84db7c 100644 --- a/http-utils/jvm/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcherJVM.scala +++ b/http-utils/jvm/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcherJVM.scala @@ -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 { @@ -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)) } diff --git a/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/DispatcherFail.scala b/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/DispatcherFail.scala new file mode 100644 index 00000000..a3be3f29 --- /dev/null +++ b/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/DispatcherFail.scala @@ -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) +} diff --git a/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcher.scala b/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcher.scala index a9926a7f..06c2aece 100644 --- a/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcher.scala +++ b/http-utils/shared/src/main/scala/io/iohk/atala/mediator/comm/MessageDispatcher.scala @@ -13,5 +13,5 @@ trait MessageDispatcher { /*context*/ destination: String, xForwardedHost: Option[String], - ): ZIO[Any, DidFail, String] + ): ZIO[Any, DispatcherError, String] } diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/actions/ProtocolExecute.scala b/mediator/src/main/scala/io/iohk/atala/mediator/actions/ProtocolExecute.scala index 4d4501a6..63ffc2b2 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/actions/ProtocolExecute.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/actions/ProtocolExecute.scala @@ -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) diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala index bfa1553f..7b515275 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala @@ -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)) @@ -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( @@ -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( diff --git a/webapp/src/main/scala/io/iohk/atala/mediator/App.scala b/webapp/src/main/scala/io/iohk/atala/mediator/App.scala index 855a3a29..5d5df89e 100644 --- a/webapp/src/main/scala/io/iohk/atala/mediator/App.scala +++ b/webapp/src/main/scala/io/iohk/atala/mediator/App.scala @@ -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