mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 16:23:12 +09:00
Improve error message from statistics_exporter
If the JSON parse failed it will usually be because Factorio returned an error message instead of JSON, pass this along for easier debugging.
This commit is contained in:
1 parent
37d8eedc0d
commit
610859eb3b
2 files changed
+24
-1
No files matched your search
@@ -23,7 +23,12 @@ const instanceGameFlowStatistics = new Gauge(
|
|||||||
class InstancePlugin extends plugin.BaseInstancePlugin {
|
class InstancePlugin extends plugin.BaseInstancePlugin {
|
||||||
async onMetrics() {
|
async onMetrics() {
|
||||||
let string = await this.instance.server.sendRcon('/sc remote.call("statistics_exporter", "export")');
|
let string = await this.instance.server.sendRcon('/sc remote.call("statistics_exporter", "export")');
|
||||||
let stats = JSON.parse(string);
|
let stats;
|
||||||
|
try {
|
||||||
|
stats = JSON.parse(string);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Error parsing statistics JSON: ${err.message}, content "${string}"`);
|
||||||
|
}
|
||||||
|
|
||||||
let instanceId = this.instance.config.id;
|
let instanceId = this.instance.config.id;
|
||||||
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
|
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
|
||||||
|
|||||||
@@ -48,6 +48,24 @@ describe("statistics_exporter plugin", function() {
|
|||||||
'7357', 'pollution_statistics', 'input', 'boiler').get(),
|
'7357', 'pollution_statistics', 'input', 'boiler').get(),
|
||||||
2000);
|
2000);
|
||||||
});
|
});
|
||||||
|
it("should pass on JSON parse errors", async function() {
|
||||||
|
let string = 'An error occured\n';
|
||||||
|
instancePlugin.instance.server.rconCommandResults.set(
|
||||||
|
'/sc remote.call("statistics_exporter", "export")', string
|
||||||
|
);
|
||||||
|
|
||||||
|
let errorMessage;
|
||||||
|
try {
|
||||||
|
JSON.parse(string);
|
||||||
|
} catch (err) {
|
||||||
|
errorMessage = err.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
instancePlugin.onMetrics(),
|
||||||
|
new Error(`Error parsing statistics JSON: ${errorMessage}, content "${string}"`)
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user