Skip to content

Move Throwable catch to ExtraProtobufGenerator.run #1637

New issue

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

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

Already on GitHub? # to your account

Merged
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 @@ -41,14 +41,30 @@ class CustomProtobufGenerator(

object ExtraProtobufGenerator extends ProtocCodeGenerator {
override def run(req: Array[Byte]): Array[Byte] = {
val registry = ExtensionRegistry.newInstance()
Scalapb.registerAllExtensions(registry)
val request = CodeGeneratorRequest.parseFrom(req)
handleCodeGeneratorRequest(request).toByteArray
val b = CodeGeneratorResponse.newBuilder

try {
val registry = ExtensionRegistry.newInstance()
Scalapb.registerAllExtensions(registry)
val request = CodeGeneratorRequest.parseFrom(req)
handleCodeGeneratorRequest(request, b)

} catch {
case e: Throwable =>
// Yes, we want to catch _all_ errors and send them back to the
// requestor. Otherwise uncaught errors will cause the generator to
// die and the worker invoking it to hang.
val stackStream = new java.io.ByteArrayOutputStream
e.printStackTrace(new java.io.PrintStream(stackStream))
b.setError(stackStream.toString())
}
b.build.toByteArray
}

def handleCodeGeneratorRequest(request: CodeGeneratorRequest): CodeGeneratorResponse = {
val b = CodeGeneratorResponse.newBuilder
def handleCodeGeneratorRequest(
request: CodeGeneratorRequest,
b: CodeGeneratorResponse.Builder
) = {
ProtobufGenerator.parseParameters(request.getParameter) match {
case Right(params) =>
try {
Expand All @@ -73,18 +89,9 @@ object ExtraProtobufGenerator extends ProtocCodeGenerator {
} catch {
case e: GeneratorException =>
b.setError(e.message)
case e: Throwable =>
// Yes, we want to catch _all_ errors and send them back to the
// requestor. Otherwise uncaught errors will cause the generator to
// die and the worker invoking it to hang.
val stackStream = new java.io.ByteArrayOutputStream
e.printStackTrace(new java.io.PrintStream(stackStream))
b.setError(stackStream.toString())
}
case Left(error) =>
b.setError(error)
}
b.build
}

}