pluginssublimetext3sublime-text-plugin

Error handling in Sublime Text Plugin API


I'm creating my first sublime plugin to be used internally and was wondering if there's a way to stop the plugin from executing and display an error message on screen like an alert?

I took a look at view#show_popup() but this didn't render for me. Below is how I attempted to show it:

    if "WebContent" in subdirectories:
        directory = ROOT_DIR + "/WebContent"
    elif "Content" in subdirectories:
        directory = ROOT_DIR + "/Content"
    else:
        self.view.show_popup('Directory not found', location=-1)

Working Principle:

The plugin takes some data from one view and then pastes them in another view. The plugin has two TextCommands. One command takes the data from the first view, opens a new view and then executes the 2nd command on the new view. The above snippet is in the 2nd command.

I was unable to find any resources to help with show_popup() or any other error handling.

Does any one have possible ideas?


Solution

  • view.show_popup() is for showing things like hover popups next to the cursor; it's used for things like the built in functionality for going to references/definitions for functions:

    example of hover popup

    While you could in theory use this for error messages, it may not be the sort of user experience that anyone would expect.

    Your code above is technically valid, but since the content is expected to be minihtml it may be hard to see because as just a single string all you're going to see is just the text (i.e. you have no font style, padding, etc):

    Example of popup

    Perhaps more importantly, when location is -1, the popup appears at the point in the buffer closest to the first cursor position, so depending on your circumstance it may be appearing in a place you don't expect, and then vanishing away before you can scroll to see it, etc.

    What you want here is sublime.error_message(); given a string, it will display that string in a dialog for the user to interact with, and it also logs the error into the Sublime console as well so that there's an additional trace.

    Example of error dialog