blurstudio / hab

A application environment and launcher
GNU Lesser General Public License v3.0
25 stars 3 forks source link

If corrupt user prefs are encountered, reset prefs instead of excepting #84

Closed MHendricks closed 10 months ago

MHendricks commented 10 months ago

If the .hab_user_prefs.json file exists but is empty or is an invalid json document, hab(and especially hab-gui) breaks.

To replicate, if I clear the contents of my .hab_user_prefs.json file (but not delete it) I get this error. I get a similar error if I remove the closing }. I'm using pyjson5, but similar errors get raised if using python's json implementation.

$ hab -v dump -
Hab encountered an error:
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\blur\dev\hab_\hab\__main__.py", line 13, in <module>
    sys.exit(hab.cli.cli(prog_name="python -m hab"))
  File "C:\blur\dev\hab_\hab\cli.py", line 660, in cli
    return _cli(*args, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
  File "C:\Program Files\Python39\lib\site-packages\click\core.py", line 2356, in process_value
    value = self.type_cast_value(ctx, value)
  File "C:\blur\dev\hab_\hab\cli.py", line 75, in type_cast_value
    uri_check = ctx.obj.resolver.user_prefs().uri_check()
  File "C:\blur\dev\hab_\hab\user_prefs.py", line 143, in uri_check
    self.load()
  File "C:\blur\dev\hab_\hab\user_prefs.py", line 111, in load
    data = utils.json.load(fle)
  File "src/_legacy.pyx", line 53, in pyjson5.load
  File "src/_exports.pyx", line 286, in pyjson5.decode_io
  File "src/_decoder.pyx", line 913, in pyjson5._decode_callback
  File "src/_decoder.pyx", line 815, in pyjson5._decode_all
pyjson5.Json5EOF: ('No JSON data found near 0', None, None)

Checklist

Types of Changes

Proposed Changes

  1. Remove the pyjson5 support for user_prefs as the file is always written using python's json, simplifying exception handling.
  2. Capture json exceptions reading the .hab_user_prefs.json file, create a logging.WARNING message about resetting prefs for visibility, and create a logging.INFO message with the traceback.