Home Download Buy Blog Forum Support

Silent fail on load_settings

Silent fail on load_settings

Postby chikamichi on Wed Feb 27, 2013 12:29 am

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.
chikamichi
 
Posts: 14
Joined: Sun Oct 16, 2011 2:26 am

Re: Silent fail on load_settings

Postby jps on Wed Feb 27, 2013 5:48 am

FWIW empty settings files won't raise an error in ST3
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: Silent fail on load_settings

Postby chikamichi on Wed Feb 27, 2013 9:15 pm

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.
chikamichi
 
Posts: 14
Joined: Sun Oct 16, 2011 2:26 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests