Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support service errors in dynamic schema index #216

Merged
merged 2 commits into from
May 17, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ private[dynamic] object Compiler {
val output = shape.output.map(_.target)

val errorId = id.copy(name = id.name + "Error")
val errorUnionLazy = shape.errors.traverse { err =>
val allOperationErrors = (serviceErrors ++ shape.errors.combineAll).toNel

val errorUnionLazy = allOperationErrors.traverse { err =>
err
.collect { case MemberShape(ValidIdRef(id), _) => id }
.zipWithIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ object DynamicJsonProxySpec extends weaver.SimpleIOSuite {

}

test("Dynamic service based proxy supports service errors") {
kubukoz marked this conversation as resolved.
Show resolved Hide resolved
kvStoreResource.use { kvStore =>
val expectedError =
smithy4s.example.UnauthorizedError("*****")

for {
attempt <- kvStore.get("authorized-only-key").attempt
} yield {
expect.same(attempt, Left(expectedError))
}
}

}

}
16 changes: 10 additions & 6 deletions modules/dynamic/test/src/smithy4s/dynamic/KVStoreImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ class KVStoreImpl(ref: Ref[IO, Map[String, String]]) extends KVStore[IO] {
}
.rethrow
def get(key: String): IO[Value] =
ref.get
.map[Either[Throwable, Value]](_.get(key) match {
case None => Left(KeyNotFoundError(key))
case Some(value) => Right(smithy4s.example.Value(value))
})
.rethrow
if (key.contains("authorized-only"))
// This will be redacted by the redacting proxy
IO.raiseError(UnauthorizedError("sensitive"))
else
ref.get
.map[Either[Throwable, Value]](_.get(key) match {
case None => Left(KeyNotFoundError(key))
case Some(value) => Right(smithy4s.example.Value(value))
})
.rethrow
def put(key: String, value: String): IO[Unit] = ref.update(_ + (key -> value))
}
9 changes: 8 additions & 1 deletion sampleSpecs/kvstore.smithy
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace smithy4s.example

service KVStore {
operations: [Get, Put, Delete]
operations: [Get, Put, Delete],
errors: [UnauthorizedError]
}

operation Put {
Expand Down Expand Up @@ -36,6 +37,12 @@ structure Value {
value: String
}

@error("client")
structure UnauthorizedError {
@required
reason: String
}

@error("client")
structure KeyNotFoundError {
@required
Expand Down