yamlconfig exceptions on single quotes

Discussion in 'Spigot Plugin Development' started by LethalBunny, Jun 7, 2018.

  1. So i'm using YamlConfiguration to manage a "messages.yml" file that is copied from internal resource into the game data folder.

    All works well, reloads great, and everything, EXCEPT when a user misplaces a single quote ' somewhere, eg:
    gameStartMsg: '"oops wrong single quote"
    msg2: "line filler"
    msg3: "line filler"
    msg4: "yo what's up"​

    If you can't tell, there's a single quote before the double quote for gameStartMsg. Doing such a thing causes the entire plugin to basically die causing the error InvalidConfigurationException.

    I've tried all sorts of try/catch ways to try and catch this error to no avail. It doesn't return any defaults or anything I have tried to do with it, it just DIES. Is there a way to deal with the error and be able to tell the user that there's an error on that line without having the plugin crash?

    And yes I have spent several hours googling for a possible pre-existing solution on the web but i've found no such thing :(. If you have links that may help please share, i'll gladly read them and share my findings.
    #1 LethalBunny, Jun 7, 2018
    Last edited: Jun 7, 2018
  2. Unfortunately, no solutions. You have to trust users to know how to use a config file. The best defence would be to add instructions at the top and maybe a link to a YAML parser, tell them to paste it there and that will tell them if it all works.
  3. This fix is does require more work, but if you're desperate, you could use something like this:
    Code (Java):
    public static String getString(ConfigurationSection section,String identifier) {
            try {
                return section.getString(identifier);
            } catch(Exception e) {
                plugin.getServer().getLogger().info(ChatColor.RED+"Error in configuration file. Path: "+section.getName()+"."+identifier);
            return null; //or disable plugin based on problem severity
    Person using would probably check out logs for errors, and gets a yaml path to the line causing the issue.

    I don't think there is any other solution than this.

    PS: when testing this out myself, i got an error saying where my yaml was incorrect.
  4. The problem is an error will occurr when the server loads the configuration file, which you cannot control. That's why i didn't suggest a try catch statement. Well, I thought but perhaps not.
  5. Was not aware of that. Turns out you can't catch those errors. One other alternative is to check yaml in your java. I would not recommend this as you would probably have to shade your project, and it therefore being large in size. Could not find any other alternative.
  6. Hmm yea, once again i reckon you're just going to have to trust your users ;/ Which is never good haha
  7. Well... this is highly disappointing... thank you all for your responses.

    I re-read the API and this finally clicked in.
    Any errors loading the Configuration will be logged and then ignored. If the specified input is not a valid config, a blank config will be returned.

    I just need to check if the loaded Configuration is null, if it's null use a seperate method that uses purely defaults. I guess that's my only failsafe but we'll have to live with the console errors and we wont be able to determine which line the error occurred on.


    It seems that InvalidConfigurationException doesn't cause the loadConfiguration method to return null.... WTF!!!!

    Update 2:

    Alright, i've managed to figure out how to detect if your YamlConfiguration is invalid: by checking if it has no keys:

    YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
    if (config == null)
    else {
    Set<String> keys = config.getConfigurationSection("").getKeys(false);
    if (keys.isEmpty())
    // load from config​
    #7 LethalBunny, Jun 9, 2018
    Last edited: Jun 9, 2018