Skip to content

Commit 69bb4f9

Browse files
committed
chore: improve cli error handling
1 parent f513a14 commit 69bb4f9

File tree

1 file changed

+133
-128
lines changed

1 file changed

+133
-128
lines changed

server/engine/src/main/java/org/eclipse/lsp/cobol/cli/command/CliAnalysis.java

+133-128
Original file line numberDiff line numberDiff line change
@@ -40,139 +40,144 @@
4040
*/
4141
@CommandLine.Command(name = "analysis", description = "analyse cobol source")
4242
public class CliAnalysis implements Callable<Integer> {
43-
@CommandLine.ParentCommand
44-
private Cli parent;
45-
46-
@CommandLine.Option(
47-
names = {"-s", "--source"},
48-
required = true,
49-
description = "The COBOL program file.")
50-
private File src;
51-
52-
@CommandLine.ArgGroup(multiplicity = "1")
53-
private Args args;
54-
55-
@CommandLine.Option(
56-
description = "Supported dialect values: ${COMPLETION-CANDIDATES}",
57-
names = {"-d", "--dialect"},
58-
defaultValue = "COBOL")
59-
private CobolLanguageId dialect;
60-
61-
@Override
62-
public Integer call() throws Exception {
63-
if (args.workspaceConfig != null)
64-
parent.initProcessorGroupsReader(args.workspaceConfig.workspace);
65-
66-
Injector diCtx = Guice.createInjector(new CliModule());
67-
CliClientProvider cliClientProvider = diCtx.getInstance(CliClientProvider.class);
68-
69-
cliClientProvider.setCpyPaths(createCopybooksPaths());
70-
cliClientProvider.setCpyExt(createCopybooksExtensions());
71-
JsonObject result = new JsonObject();
72-
73-
Cli.Result analysisResult = parent.runAnalysis(src, dialect, diCtx, true);
74-
parent.addTiming(result, analysisResult.ctx.getBenchmarkSession());
75-
JsonArray diagnostics = new JsonArray();
76-
analysisResult
77-
.ctx
78-
.getAccumulatedErrors()
79-
.forEach(
80-
err -> {
81-
JsonObject diagnostic = CliUtils.diagnosticToJson(err);
82-
diagnostics.add(diagnostic);
83-
});
84-
result.add("diagnostics", diagnostics);
85-
result.addProperty("uri", analysisResult.ctx.getExtendedDocument().getUri());
86-
result.addProperty("language", analysisResult.ctx.getLanguageId().getId());
87-
result.addProperty("lines", String.valueOf(analysisResult.ctx.getExtendedDocument().toString().split("\n").length));
88-
result.addProperty("size", String.valueOf(analysisResult.ctx.getExtendedDocument().toString().length()));
89-
collectGcAndMemoryStats(result);
90-
System.out.println(CliUtils.GSON.toJson(result));
91-
return 0;
92-
}
93-
94-
private void collectGcAndMemoryStats(JsonObject result) {
95-
long totalGarbageCollections = 0;
96-
long garbageCollectionTime = 0;
97-
98-
for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
99-
long count = gc.getCollectionCount();
100-
if (count >= 0) {
101-
totalGarbageCollections += count;
102-
}
103-
long time = gc.getCollectionTime();
104-
if (time >= 0) {
105-
garbageCollectionTime += time;
106-
}
107-
}
108-
result.addProperty("gc.count", totalGarbageCollections);
109-
// milliseconds to seconds
110-
result.addProperty("gc.time", garbageCollectionTime * 0.001);
111-
112-
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
113-
long total = 0;
114-
for (MemoryPoolMXBean memoryPoolMXBean : pools) {
115-
if (memoryPoolMXBean.getType() == MemoryType.HEAP) {
116-
long peakUsed = memoryPoolMXBean.getPeakUsage().getUsed();
117-
total = total + peakUsed;
118-
}
119-
}
120-
result.addProperty("memory.heap_peak", total / 1024 / 1024);
121-
}
43+
@CommandLine.ParentCommand
44+
private Cli parent;
12245

123-
/**
124-
* WorkspaceConfig options
125-
*/
126-
static class WorkspaceConfig {
12746
@CommandLine.Option(
128-
description = "Path to workspace folder.",
129-
names = {"-ws", "--workspace"})
130-
private Path workspace;
131-
}
132-
133-
/**
134-
* explicit config options
135-
*/
136-
static class ExplicitConfig {
137-
@CommandLine.Option(
138-
names = {"-cf", "--copybook-folder"},
139-
description = "Path to the copybook folder.")
140-
private File[] cpyPaths = {};
47+
names = {"-s", "--source"},
48+
required = true,
49+
description = "The COBOL program file.")
50+
private File src;
51+
52+
@CommandLine.ArgGroup(multiplicity = "1")
53+
private Args args;
14154

14255
@CommandLine.Option(
143-
names = {"-ce", "--copybook-extension"},
144-
description = "List of copybook paths.")
145-
private String[] cpyExt = {"", ".cpy"};
146-
}
147-
148-
/**
149-
* options for analysis command
150-
*/
151-
static class Args {
152-
@CommandLine.ArgGroup(exclusive = false)
153-
ExplicitConfig explicitConfig;
154-
155-
@CommandLine.ArgGroup(exclusive = false)
156-
WorkspaceConfig workspaceConfig;
157-
}
158-
159-
private List<File> createCopybooksPaths() {
160-
if (args.workspaceConfig != null && parent.processorGroupsResolver != null) {
161-
return parent
162-
.processorGroupsResolver
163-
.resolveCopybooksPaths(src.toPath(), args.workspaceConfig.workspace)
164-
.stream()
165-
.map(Path::toFile)
166-
.collect(Collectors.toList());
56+
description = "Supported dialect values: ${COMPLETION-CANDIDATES}",
57+
names = {"-d", "--dialect"},
58+
defaultValue = "COBOL")
59+
private CobolLanguageId dialect;
60+
61+
@Override
62+
public Integer call() throws Exception {
63+
if (args.workspaceConfig != null)
64+
parent.initProcessorGroupsReader(args.workspaceConfig.workspace);
65+
66+
Injector diCtx = Guice.createInjector(new CliModule());
67+
CliClientProvider cliClientProvider = diCtx.getInstance(CliClientProvider.class);
68+
69+
cliClientProvider.setCpyPaths(createCopybooksPaths());
70+
cliClientProvider.setCpyExt(createCopybooksExtensions());
71+
JsonObject result = new JsonObject();
72+
result.addProperty("uri", src.toURI().toString());
73+
try {
74+
Cli.Result analysisResult = parent.runAnalysis(src, dialect, diCtx, true);
75+
parent.addTiming(result, analysisResult.ctx.getBenchmarkSession());
76+
JsonArray diagnostics = new JsonArray();
77+
analysisResult
78+
.ctx
79+
.getAccumulatedErrors()
80+
.forEach(
81+
err -> {
82+
JsonObject diagnostic = CliUtils.diagnosticToJson(err);
83+
diagnostics.add(diagnostic);
84+
});
85+
result.add("diagnostics", diagnostics);
86+
result.addProperty("language", analysisResult.ctx.getLanguageId().getId());
87+
result.addProperty("lines", String.valueOf(analysisResult.ctx.getExtendedDocument().toString().split("\n").length));
88+
result.addProperty("size", String.valueOf(analysisResult.ctx.getExtendedDocument().toString().length()));
89+
collectGcAndMemoryStats(result);
90+
System.out.println(CliUtils.GSON.toJson(result));
91+
return 0;
92+
} catch (Exception e) {
93+
result.addProperty("crash", e.getMessage() != null && e.getMessage().isEmpty() ? "error" : e.getMessage());
94+
System.out.println(CliUtils.GSON.toJson(result));
95+
return 1;
96+
}
97+
}
98+
99+
private void collectGcAndMemoryStats(JsonObject result) {
100+
long totalGarbageCollections = 0;
101+
long garbageCollectionTime = 0;
102+
103+
for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
104+
long count = gc.getCollectionCount();
105+
if (count >= 0) {
106+
totalGarbageCollections += count;
107+
}
108+
long time = gc.getCollectionTime();
109+
if (time >= 0) {
110+
garbageCollectionTime += time;
111+
}
112+
}
113+
result.addProperty("gc.count", totalGarbageCollections);
114+
// milliseconds to seconds
115+
result.addProperty("gc.time", garbageCollectionTime * 0.001);
116+
117+
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
118+
long total = 0;
119+
for (MemoryPoolMXBean memoryPoolMXBean : pools) {
120+
if (memoryPoolMXBean.getType() == MemoryType.HEAP) {
121+
long peakUsed = memoryPoolMXBean.getPeakUsage().getUsed();
122+
total = total + peakUsed;
123+
}
124+
}
125+
result.addProperty("memory.heap_peak", total / 1024 / 1024);
126+
}
127+
128+
/**
129+
* WorkspaceConfig options
130+
*/
131+
static class WorkspaceConfig {
132+
@CommandLine.Option(
133+
description = "Path to workspace folder.",
134+
names = {"-ws", "--workspace"})
135+
private Path workspace;
136+
}
137+
138+
/**
139+
* explicit config options
140+
*/
141+
static class ExplicitConfig {
142+
@CommandLine.Option(
143+
names = {"-cf", "--copybook-folder"},
144+
description = "Path to the copybook folder.")
145+
private File[] cpyPaths = {};
146+
147+
@CommandLine.Option(
148+
names = {"-ce", "--copybook-extension"},
149+
description = "List of copybook paths.")
150+
private String[] cpyExt = {"", ".cpy"};
151+
}
152+
153+
/**
154+
* options for analysis command
155+
*/
156+
static class Args {
157+
@CommandLine.ArgGroup(exclusive = false)
158+
ExplicitConfig explicitConfig;
159+
160+
@CommandLine.ArgGroup(exclusive = false)
161+
WorkspaceConfig workspaceConfig;
162+
}
163+
164+
private List<File> createCopybooksPaths() {
165+
if (args.workspaceConfig != null && parent.processorGroupsResolver != null) {
166+
return parent
167+
.processorGroupsResolver
168+
.resolveCopybooksPaths(src.toPath(), args.workspaceConfig.workspace)
169+
.stream()
170+
.map(Path::toFile)
171+
.collect(Collectors.toList());
172+
}
173+
return Arrays.asList(args.explicitConfig.cpyPaths);
167174
}
168-
return Arrays.asList(args.explicitConfig.cpyPaths);
169-
}
170175

171-
private List<String> createCopybooksExtensions() {
172-
if (args.workspaceConfig != null && parent.processorGroupsResolver != null) {
173-
return parent.processorGroupsResolver.resolveCopybooksExtensions(
174-
src.toPath(), args.workspaceConfig.workspace);
176+
private List<String> createCopybooksExtensions() {
177+
if (args.workspaceConfig != null && parent.processorGroupsResolver != null) {
178+
return parent.processorGroupsResolver.resolveCopybooksExtensions(
179+
src.toPath(), args.workspaceConfig.workspace);
180+
}
181+
return Arrays.asList(args.explicitConfig.cpyExt);
175182
}
176-
return Arrays.asList(args.explicitConfig.cpyExt);
177-
}
178183
}

0 commit comments

Comments
 (0)