Skip to content

Commit

Permalink
Merge pull request #79 from kubukoz/feedback-compilation-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Aug 13, 2022
2 parents e54794b + 7af58f7 commit 1a36e44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions core/src/main/scala/playground/CommandProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ object CommandProvider {
}
}
}
.recoverWith { case _: CompilationFailed =>
CommandResultReporter[F].onCompilationFailed
}

}
.merge
}
Expand Down
29 changes: 17 additions & 12 deletions core/src/main/scala/playground/CommandResultReporter.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package playground

import cats.FlatMap
import cats.Id
import cats.Monad
import cats.data.NonEmptyList
import cats.effect.kernel.Ref
import cats.implicits._
Expand All @@ -12,14 +12,24 @@ import playground.smithyql.WithSource

import Runner.Issue.ProtocolIssues

trait CommandResultReporter[F[_]] {
type RequestId
def onUnsupportedProtocol(issues: ProtocolIssues): F[Unit]
def onIssues(issues: NonEmptyList[Throwable]): F[Unit]
def onCompilationFailed: F[Unit]
def onQueryCompiled(parsed: Query[Id], compiled: CompiledInput): F[RequestId]
def onQuerySuccess(parsed: Query[Id], requestId: RequestId, output: InputNode[Id]): F[Unit]
def onQueryFailure(e: Throwable, compiled: CompiledInput, requestId: RequestId): F[Unit]
}

object CommandResultReporter {
def apply[F[_]](implicit F: CommandResultReporter[F]): F.type = F

def instance[
F[_]: Feedback: FlatMap: Ref.Make
F[_]: Feedback: Monad: Ref.Make
]: F[CommandResultReporter[F]] = Ref[F].of(0).map(withRequestCounter(_))

def withRequestCounter[F[_]: Feedback: FlatMap](
def withRequestCounter[F[_]: Feedback: Monad](
requestCounter: Ref[F, Int]
): CommandResultReporter[F] =
new CommandResultReporter[F] {
Expand All @@ -43,6 +53,10 @@ object CommandResultReporter {
issues.map(_.toString).mkString_("\n\n")
)

def onCompilationFailed: F[Unit] = Feedback[F].showErrorMessage(
"Couldn't run query because of compilation errors."
)

def onQueryCompiled(parsed: Query[Id], compiled: CompiledInput): F[RequestId] =
Feedback[F].showOutputPanel *>
requestCounter.updateAndGet(_ + 1).flatTap { requestId =>
Expand Down Expand Up @@ -77,12 +91,3 @@ object CommandResultReporter {
}

}

trait CommandResultReporter[F[_]] {
type RequestId
def onUnsupportedProtocol(issues: ProtocolIssues): F[Unit]
def onIssues(issues: NonEmptyList[Throwable]): F[Unit]
def onQueryCompiled(parsed: Query[Id], compiled: CompiledInput): F[RequestId]
def onQuerySuccess(parsed: Query[Id], requestId: RequestId, output: InputNode[Id]): F[Unit]
def onQueryFailure(e: Throwable, compiled: CompiledInput, requestId: RequestId): F[Unit]
}

0 comments on commit 1a36e44

Please sign in to comment.