Skip to content

Commit a325634

Browse files
committed
refactor(exception): unify exception hierarchy and introduce specific error types
1 parent 5bc292a commit a325634

File tree

15 files changed

+116
-24
lines changed

15 files changed

+116
-24
lines changed

src/lsp_client/client/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import annotations
2+
3+
from .abc import Client
4+
from .exception import ClientError, ClientRuntimeError
5+
6+
__all__ = [
7+
"Client",
8+
"ClientError",
9+
"ClientRuntimeError",
10+
]

src/lsp_client/client/exception.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from lsp_client.exception import LSPError
6+
7+
if TYPE_CHECKING:
8+
from .abc import Client
9+
10+
11+
class ClientError(LSPError):
12+
"""Base exception for client-related errors."""
13+
14+
15+
class ClientRuntimeError(ClientError):
16+
"""Raised when a client encounters a runtime error."""
17+
18+
def __init__(self, client: Client, *args: object):
19+
super().__init__(*args)
20+
self.client = client

src/lsp_client/clients/deno/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525
from lsp_client.capability.server_notification.log_message import WithReceiveLogMessage
2626
from lsp_client.client.abc import Client
27-
from lsp_client.server import DefaultServers
27+
from lsp_client.server import DefaultServers, ServerInstallationError
2828
from lsp_client.server.container import ContainerServer
2929
from lsp_client.server.local import LocalServer
3030
from lsp_client.utils.types import lsp_type
@@ -62,7 +62,7 @@ async def ensure_deno_installed() -> None:
6262
logger.info("Successfully installed deno via shell script")
6363
return
6464
except CalledProcessError as e:
65-
raise RuntimeError(
65+
raise ServerInstallationError(
6666
"Could not install deno. Please install it manually with:\n"
6767
"curl -fsSL https://deno.land/install.sh | sh\n\n"
6868
"See https://deno.land/ for more information."

src/lsp_client/clients/pyrefly.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
WithRespondWorkspaceFoldersRequest,
3737
)
3838
from lsp_client.client.abc import Client
39-
from lsp_client.server import DefaultServers
39+
from lsp_client.server import DefaultServers, ServerInstallationError
4040
from lsp_client.server.container import ContainerServer
4141
from lsp_client.server.local import LocalServer
4242
from lsp_client.utils.types import lsp_type
@@ -60,7 +60,7 @@ async def ensure_pyrefly_installed() -> None:
6060
await anyio.run_process(["pip", "install", "pyrefly"])
6161
logger.info("Successfully installed pyrefly via uv tool")
6262
except CalledProcessError as e:
63-
raise RuntimeError(
63+
raise ServerInstallationError(
6464
"Could not install pyrefly. Please install it manually with 'pip install pyrefly'. "
6565
"See https://pyrefly.org/ for more information."
6666
) from e

src/lsp_client/clients/pyright.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
WithRespondWorkspaceFoldersRequest,
3636
)
3737
from lsp_client.client.abc import Client
38-
from lsp_client.server import DefaultServers
38+
from lsp_client.server import DefaultServers, ServerInstallationError
3939
from lsp_client.server.container import ContainerServer
4040
from lsp_client.server.local import LocalServer
4141
from lsp_client.utils.types import lsp_type
@@ -56,7 +56,7 @@ async def ensure_pyright_installed() -> None:
5656
logger.info("Successfully installed pyright-langserver via npm")
5757
return
5858
except CalledProcessError as e:
59-
raise RuntimeError(
59+
raise ServerInstallationError(
6060
"Could not install pyright-langserver. Please install it manually with 'npm install -g pyright'. "
6161
"See https://microsoft.github.io/pyright/ for more information."
6262
) from e

src/lsp_client/clients/rust_analyzer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
WithRespondWorkspaceFoldersRequest,
3737
)
3838
from lsp_client.client.abc import Client
39-
from lsp_client.server import DefaultServers
39+
from lsp_client.server import DefaultServers, ServerInstallationError
4040
from lsp_client.server.container import ContainerServer
4141
from lsp_client.server.local import LocalServer
4242
from lsp_client.utils.types import lsp_type
@@ -56,7 +56,7 @@ async def ensure_rust_analyzer_installed() -> None:
5656
await anyio.run_process(["rustup", "component", "add", "rust-analyzer"])
5757
logger.info("Successfully installed rust-analyzer via rustup")
5858
except CalledProcessError as e:
59-
raise RuntimeError(
59+
raise ServerInstallationError(
6060
"Could not install rust-analyzer. Please install it manually with 'rustup component add rust-analyzer'. "
6161
"See https://rust-analyzer.github.io/ for more information."
6262
) from e

src/lsp_client/clients/ty.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
WithRespondWorkspaceFoldersRequest,
3636
)
3737
from lsp_client.client.abc import Client
38-
from lsp_client.server import DefaultServers
38+
from lsp_client.server import DefaultServers, ServerInstallationError
3939
from lsp_client.server.container import ContainerServer
4040
from lsp_client.server.local import LocalServer
4141
from lsp_client.utils.types import lsp_type
@@ -54,7 +54,7 @@ async def ensure_ty_installed() -> None:
5454
logger.info("Successfully installed ty via pip")
5555
return
5656
except CalledProcessError as e:
57-
raise RuntimeError(
57+
raise ServerInstallationError(
5858
"Could not install ty. Please install it manually with 'pip install ty' or 'uv tool install ty'. "
5959
"See https://docs.astral.sh/ty/installation/ for more information."
6060
) from e

src/lsp_client/clients/typescript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
WithRespondWorkspaceFoldersRequest,
3535
)
3636
from lsp_client.client.abc import Client
37-
from lsp_client.server import DefaultServers
37+
from lsp_client.server import DefaultServers, ServerInstallationError
3838
from lsp_client.server.container import ContainerServer
3939
from lsp_client.server.local import LocalServer
4040
from lsp_client.utils.types import lsp_type
@@ -61,7 +61,7 @@ async def ensure_typescript_installed() -> None:
6161
logger.info("Successfully installed typescript-language-server via npm")
6262
return
6363
except CalledProcessError as e:
64-
raise RuntimeError(
64+
raise ServerInstallationError(
6565
"Could not install typescript-language-server and typescript. Please install them manually with 'npm install -g typescript-language-server typescript'. "
6666
"See https://github.com/typescript-language-server/typescript-language-server for more information."
6767
) from e

src/lsp_client/exception.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import annotations
2+
3+
4+
class LSPError(Exception):
5+
"""Base exception for all lsp-client errors."""

src/lsp_client/jsonrpc/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from .exception import (
4+
JsonRpcError,
5+
JsonRpcParseError,
6+
JsonRpcResponseError,
7+
JsonRpcTransportError,
8+
)
9+
10+
__all__ = [
11+
"JsonRpcError",
12+
"JsonRpcParseError",
13+
"JsonRpcResponseError",
14+
"JsonRpcTransportError",
15+
]

0 commit comments

Comments
 (0)