Skip to content

Commit

Permalink
Merge pull request #216 from kubukoz/dynamic-service-errors
Browse files Browse the repository at this point in the history
Support service errors in dynamic schema index
  • Loading branch information
Baccata authored May 17, 2022
2 parents 7f31df8 + cdabf9f commit 069151e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
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") {
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

0 comments on commit 069151e

Please sign in to comment.