Skip to content

Commit b0afae3

Browse files
authored
[Bug]: Fixes Issue #520, catches JSON parse error and displays error to user to fix (#521)
Fixes #520 a JSON parsing error that occurs when the AFC.var.unit file becomes corrupted, preventing the system from starting properly. The fix includes proper error handling and user-friendly error messages.
1 parent c2d6c10 commit b0afae3

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2025-08-30]
9+
### Added
10+
- Catch for JSON decode error when trying to read and load AFC.var.unit file
11+
812
## [2025-08-24]
913
### Added
1014
- You can now use the `install-afc.sh` script to delete the `AFC.var.unit` file if necessary. This option is located under

extras/AFC.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
try: from extras.AFC_stats import AFCStats
2828
except: raise error(ERROR_STR.format(import_lib="AFC_stats", trace=traceback.format_exc()))
2929

30-
AFC_VERSION="1.0.28"
30+
AFC_VERSION="1.0.29"
3131

3232
# Class for holding different states so its clear what all valid states are
3333
class State:

extras/AFC_logger.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, printer, afc_obj):
3333
self.afc = afc_obj
3434
self.gcode = printer.lookup_object('gcode')
3535
self.webhooks = printer.lookup_object('webhooks')
36+
printer.register_event_handler( "gcode:request_restart", self._stop)
3637

3738
log_path = printer.start_args['log_file']
3839
dirname = Path(log_path).parent
@@ -48,6 +49,9 @@ def __init__(self, printer, afc_obj):
4849
self.logger.setLevel(logging.DEBUG)
4950
self.print_debug_console = False
5051

52+
def _stop(self, eventtime):
53+
self.afc_ql.stop()
54+
5155
def _add_monotonic(self, message):
5256
return "{:10.3f} {}".format(self.reactor.monotonic(), message)
5357

extras/AFC_prep.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ def PREP(self, gcmd):
7373
## load Unit stored variables
7474
units={}
7575
if os.path.exists('{}.unit'.format(self.afc.VarFile)) and os.stat('{}.unit'.format(self.afc.VarFile)).st_size > 0:
76-
units=json.load(open('{}.unit'.format(self.afc.VarFile)))
76+
try:
77+
units=json.load(open('{}.unit'.format(self.afc.VarFile)))
78+
except json.JSONDecodeError as e:
79+
# Displaying error for user to fix, do not want to continue just in case
80+
# there is actual data in this file as we do not want to overwrite and put
81+
# users boxturtles into a weird state if prep continues.
82+
self.afc.error.AFC_error(f"Error when trying to open and decode {self.afc.VarFile}.unit file.\n" + \
83+
"Please fix file or delete if file is empty, then restart klipper.", False)
84+
self.logger.error("", traceback=f"{e}")
85+
return
86+
7787
else:
7888
error_string = 'Error: {}.unit file not found. Please check the path in the '.format(self.afc.VarFile)
7989
error_string += 'AFC.cfg file and make sure the file and path exists.'

0 commit comments

Comments
 (0)