Skip to content

Commit

Permalink
Fix eventstream exception parser when no errors defined
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Aug 26, 2024
1 parent 119faee commit 25eda46
Showing 1 changed file with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,33 +246,45 @@ private void renderParseEventMethod(

private void renderParseExceptionMethod(
RubyCodeWriter writer, UnionShape eventStreamUnion) {
writer
.openBlock("def parse_exception_event(type, message)")
.write("case type")
.call(() -> {
for (MemberShape memberShape : eventStreamUnion.members()) {
Shape target = model.expectShape(memberShape.getTarget());
if (target.hasTrait(ErrorTrait.class)) {
writer
.write("when '$L'", symbolProvider.toMemberName(memberShape))
.indent()
.write("data = Parsers::EventStream::$L.parse(message)",
symbolProvider.toSymbol(target).getName())
.write("Errors::$L.new(data: data, error_code: '$L')",
symbolProvider.toSymbol(target).getName(),
symbolProvider.toSymbol(memberShape))
.dedent();
}
boolean hasErrorEvents = eventStreamUnion.members().stream()
.anyMatch(m -> m.getMemberTrait(model, ErrorTrait.class).isPresent());
if (hasErrorEvents) {
writer
.openBlock("def parse_exception_event(type, message)")
.write("case type")
.call(() -> {
for (MemberShape memberShape : eventStreamUnion.members()) {
Shape target = model.expectShape(memberShape.getTarget());
if (target.hasTrait(ErrorTrait.class)) {
writer
.write("when '$L'", symbolProvider.toMemberName(memberShape))
.indent()
.write("data = Parsers::EventStream::$L.parse(message)",
symbolProvider.toSymbol(target).getName())
.write("Errors::$L.new(data: data, error_code: '$L')",
symbolProvider.toSymbol(target).getName(),
symbolProvider.toSymbol(memberShape))
.dedent();
}

}
})
.openBlock("else")
.write("data = $T::Unknown.new(name: type, value: message)",
symbolProvider.toSymbol(eventStreamUnion))
.write("Errors::ApiError.new(error_code: type, "
+ "metadata: {data: data})")
.closeBlock("end")
.closeBlock("end");
}
})
.openBlock("else")
.write("data = $T::Unknown.new(name: type, value: message)",
symbolProvider.toSymbol(eventStreamUnion))
.write("Errors::ApiError.new(error_code: type, "
+ "metadata: {data: data})")
.closeBlock("end")
.closeBlock("end");
} else {
writer
.openBlock("def parse_exception_event(type, message)")
.write("data = $T::Unknown.new(name: type, value: message)",
symbolProvider.toSymbol(eventStreamUnion))
.write("Errors::ApiError.new(error_code: type, "
+ "metadata: {data: data})")
.closeBlock("end");
}
}

private void renderParseErrorEventMethod(RubyCodeWriter writer) {
Expand Down

0 comments on commit 25eda46

Please sign in to comment.