From 3fddd2bdf968d477515af961ca17913979102cc0 Mon Sep 17 00:00:00 2001
From: Athanasius <github@miggy.org>
Date: Wed, 10 Mar 2021 13:49:34 +0000
Subject: [PATCH] try/except ModulesInfo.json load & fully document

---
 PLUGINS.md |  9 +++++++++
 monitor.py | 10 ++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/PLUGINS.md b/PLUGINS.md
index e55b2b42..71c0f6a6 100644
--- a/PLUGINS.md
+++ b/PLUGINS.md
@@ -427,6 +427,7 @@ Content of `state` (updated to the current journal entry):
 | `Rebuy`        |            `int`            | Current ship's rebuy cost                                                                                       |
 | `Modules`      |           `dict`            | Currently fitted modules                                                                                        |
 | `NavRoute`     |           `dict`            | Last plotted multi-hop route                                                                                    |
+| `ModuleInfo`   |           `dict`            | Last loaded ModulesInfo.json data                                                                               |
 
 ##### Synthetic Events
 
@@ -479,6 +480,11 @@ Examples of this are:
    }
     ```
 
+1. Every `ModuleInfo` event contains the full data as loaded from the
+  `ModulesInfo.json` file.  It's also available as `monitor.stat['ModuleInfo']`
+   (noting that we used the singular form there to stay consistent with the
+   Journal event name).
+
 #### Player Dashboard
 
 ```python
@@ -515,6 +521,9 @@ New in version 5.0.0:
 `NavRoute` contains the `json.load()` of `NavRoute.json` as indicated by a journal
 `NavRoute` event.
 
+`ModuleInfo` contains the `json.load()` of `ModulesInfo.json` as indicated by a
+Journal `ModuleInfo` event.
+
 #### Getting Commander Data
 
 ```python
diff --git a/monitor.py b/monitor.py
index 11afd2f9..e6f5f678 100644
--- a/monitor.py
+++ b/monitor.py
@@ -682,8 +682,14 @@ class EDLogs(FileSystemEventHandler):  # type: ignore # See below
 
             elif event_type == 'ModuleInfo':
                 with open(join(self.currentdir, 'ModulesInfo.json'), 'rb') as mf:  # type: ignore
-                    entry = json.load(mf)
-                    self.state['ModuleInfo'] = entry
+                    try:
+                        entry = json.load(mf)
+
+                    except json.JSONDecodeError:
+                        logger.exception('Failed decoding ModulesInfo.json', exc_info=True)
+
+                    else:
+                        self.state['ModuleInfo'] = entry
 
             elif event_type in ('CollectCargo', 'MarketBuy', 'BuyDrones', 'MiningRefined'):
                 commodity = self.canonicalise(entry['Type'])