Sublime Forum

Silent fail on load_settings

#1

Hi,

While developing a plugin, I noticed that saving an empty (or invalid, for that matter) JSON settings file ends up popping an alert to the user (“Error trying to parse settings”, “No data…”).

I guess this comes from the JSON parser raising an exception, and what I’d like to do is catching this somehow to just fail silently in my plugin. The use-case is for when a user deletes all content inside the settings file.

I searched the documentation and asked on irc, but was not lucky. Is this possible directly in ST?

Thank you.

0 Likes

#2

FWIW empty settings files won’t raise an error in ST3

0 Likes

#3

The issue occurs while using latest ST3 build at this date: do you mean this will change, or should it already ignore empty files?

Actually I’m developing for both ST2 and ST3. For some reason, it seems worse in ST2, for if you load_settings() such an empty, custom settings file, calling get(“some_key”) obviously doesn’t report a failure, but if the code then rely on this value to exist, you actually manipulate a TypeNone, resulting in unexpected TypeError errors (ST3 seems immune to this). One could check for this all along the code, but it’s just easier catching the inconsistency at (settings’) loading time.

I guess for now I’ll just check for empty settings file before doing anything: I don’t really want to use the common trick of writing bar = bool(sublime.load_settings(‘foo’).get(‘bar’, DEFAULT_VALUE_SAME_AS_JSON)) just because I need to ensure some akward empty settings may mess with the fetching of default values.

0 Likes