question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve handling of unexpected errors

See original GitHub issue

This jumps off from an issue identified by @alcarney in the v1 breaking changes PR https://github.com/openlawlibrary/pygls/pull/273#issuecomment-1288146841

Pygls does already catch some errors, but not all. Nor does it offer a way for server authors to handle those unexpected errors.

@alcarney Is that a fair summarisation of the problem? Obviously the underlying message parsing problem needs to be fixed, but is it not a reflection of a deeper issue, that Pygls couldn’t recover from that error?

Recently I’ve been attempting to build a Pygls template project, that offers a reasonable and informative starting point for new custom servers. Maybe it’s my relative inexperience with Pygls, but I noticed that there doesn’t seem to be a centralised point for all error handling? My naive approach is to decorate the feature decorator like so:

def add_feature(self, feature: str):
    """
    This is a wrapper just to catch and handle all unexpected errors in one place.
    TODO:
    Is this even useful? I wonder if this should be formally supported in Pygls itself?
    """

    def wrapper(func):
        @self.feature(feature)
        async def inner(*args, **kwargs):
            try:
                await func(*args, **kwargs)
            except Exception as e:
                self.logger.error(e)
                # TODO: should this have the name of the custom LSP server?
                self.show_message(f"Unexpected error in LSP server: {e}")

        return inner

    return wrapper

Does that seem relevant to the deeper issue at hand?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tombhcommented, Oct 23, 2022

Ok great, thanks for the insights 🤓 So just to sketch things out:

  • data_received, like _send_data, should have a try/catch block.
  • Concerning notification errors, both should have an easily configurable (a feature-like decorator or something) shared custom function in their except: blocks.
  • By default that shared function should send a showMessage.type = MessageType.Error message to the client on errors triggered by notification responses.

Not saying you should do all that if you’re the first in there. Just making a note for the future.

0reactions
tombhcommented, Oct 23, 2022

Oh, wait! Hold the press! I’ve figured something out: errors in LSP requests and notifications are handled differently.

So, the actual formal way to handle a LSP client request is with a LSP ResponseError in ResponseMessage.error. Which we do already do in JsonRPCProtocol._handle_request()

Now, here’s the thing. LSP requests and notifications are 2 distinct things. You probably already knew that, but I’ve only just realised that now 😬 For anybody else that isn’t aware of the difference, textDocument/didOpen is an example of a notification, whereas textDocument/completion is a request. And in Pygls’ case, JsonRPCProtocol._handle_notification() does not attempt to return any errors to the client. It does however at least catch and locally log errors.

So, this changes gears a little. Unchanged is that we’re still wanting to improve error handling for data_received. But more specifically we want to default to sending showMessage.type = MessageType.Error messages to the client for notifications only. With a convenient way to override that behaviour.

I’ve updated my previous comment to better reflect this new insight.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dealing with Unexpected Errors in Processes - Keep Kalm
In this way, the business analyst can capture business exceptions thrown in specific tasks and provide proper handling for each scenario. But ...
Read more >
When 'unexpected errors' arise, the solution is to keep moving ...
In the software development world, the approach to handling unplanned errors is well-defined. The first step is to tell your program not to ......
Read more >
Improper Error Handling - OWASP Foundation
Improper handling of errors can introduce a variety of security problems for a web site. The most common problem is when detailed internal...
Read more >
Error monitoring and exception handling in large-scale ...
Learn how to handle errors and exceptions on large scale software projects, with best practices, tools and tips.
Read more >
Expected and Unexpected Errors - bouwe.io
Because of these differences, handling expected and unexpected errors will happen in different places in the app. Expected errors are handled ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found