Skip to content

Commit 8eb6e89

Browse files
committed
Handle RPC deserialization errors in PythonRewriteRpc#parseProject()
1 parent b8da3dd commit 8eb6e89

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

rewrite-python/src/main/java/org/openrewrite/python/rpc/PythonRewriteRpc.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import org.openrewrite.python.marker.PythonResolutionResult;
2626
import org.openrewrite.python.marker.PythonResolutionResult.Dependency;
2727
import org.openrewrite.python.marker.PythonResolutionResult.ResolvedDependency;
28+
import org.openrewrite.marker.Markers;
2829
import org.openrewrite.python.tree.Py;
2930
import org.openrewrite.rpc.RewriteRpc;
3031
import org.openrewrite.rpc.RewriteRpcProcess;
3132
import org.openrewrite.rpc.RewriteRpcProcessManager;
33+
import org.openrewrite.tree.ParseError;
3234
import org.openrewrite.tree.ParsingEventListener;
3335
import org.openrewrite.tree.ParsingExecutionContextView;
3436

@@ -41,6 +43,7 @@
4143
import java.io.InputStream;
4244
import java.io.PrintStream;
4345
import java.io.UncheckedIOException;
46+
import java.nio.charset.StandardCharsets;
4447
import java.nio.file.Files;
4548
import java.nio.file.Path;
4649
import java.nio.file.Paths;
@@ -204,9 +207,24 @@ public boolean tryAdvance(Consumer<? super SourceFile> action) {
204207
ParseProjectResponse.Item item = response.get(index);
205208
index++;
206209

207-
SourceFile sourceFile = getObject(item.getId(), item.getSourceFileType());
208-
// for status update messages
209-
parsingListener.startedParsing(Parser.Input.fromFile(sourceFile.getSourcePath()));
210+
SourceFile sourceFile;
211+
try {
212+
sourceFile = getObject(item.getId(), item.getSourceFileType());
213+
parsingListener.startedParsing(Parser.Input.fromFile(sourceFile.getSourcePath()));
214+
} catch (Exception e) {
215+
sourceFile = new ParseError(
216+
Tree.randomId(),
217+
new Markers(Tree.randomId(), Collections.singletonList(
218+
ParseExceptionResult.build(PythonParser.class, e, null))),
219+
Paths.get("parse-error-" + (index - 1)),
220+
null,
221+
StandardCharsets.UTF_8.name(),
222+
false,
223+
null,
224+
e.getMessage(),
225+
null
226+
);
227+
}
210228
action.accept(sourceFile);
211229
return true;
212230
}

0 commit comments

Comments
 (0)