refactor: replace string-matching error checks with custom error classes
This commit is contained in:
@@ -12,6 +12,7 @@ import type {
|
||||
} from "vscode-languageserver-protocol";
|
||||
import * as path from "node:path";
|
||||
import type { ServerConfig } from "./types.ts";
|
||||
import { ServerNotFoundError } from "./types.ts";
|
||||
import { findRoot, pathToUri, uriToPath } from "./root.ts";
|
||||
|
||||
// Is On PATH - Returns true if `cmd` resolves to an executable via the
|
||||
@@ -73,10 +74,7 @@ export class LspClient {
|
||||
// letting spawn ENOENT surface as a generic error. It's the user's
|
||||
// responsibility to have the server installed & on PATH.
|
||||
if (!isOnPath(this.server.command)) {
|
||||
throw new Error(
|
||||
`LSP server binary "${this.server.command}" not found on PATH. ` +
|
||||
`Install it and ensure it's on your PATH (required by server "${this.server.id}").`,
|
||||
);
|
||||
throw new ServerNotFoundError(this.server.command);
|
||||
}
|
||||
this.proc = spawn(this.server.command, this.server.args, {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
|
||||
@@ -3,6 +3,7 @@ import * as path from "node:path";
|
||||
import { pathToFileURL, fileURLToPath } from "node:url";
|
||||
import { servers } from "../server.ts";
|
||||
import type { ServerConfig } from "./types.ts";
|
||||
import { UnsupportedExtensionError } from "./types.ts";
|
||||
|
||||
// Resolve File URI To Local Path
|
||||
export function uriToPath(uri: string): string {
|
||||
@@ -20,7 +21,7 @@ export function pickServer(filePath: string): ServerConfig {
|
||||
const ext = path.extname(filePath).replace(/^\./, "");
|
||||
const hit = servers.find((s) => s.match.includes(ext));
|
||||
if (!hit) {
|
||||
throw new Error(`No LSP server registered for extension ".${ext}"`);
|
||||
throw new UnsupportedExtensionError(`.${ext}`);
|
||||
}
|
||||
return hit;
|
||||
}
|
||||
|
||||
19
src/types.ts
19
src/types.ts
@@ -1,3 +1,22 @@
|
||||
// LSP Errors - Custom error classes so callers can distinguish expected
|
||||
// conditions (unsupported file type, missing binary) from unexpected ones.
|
||||
export class UnsupportedExtensionError extends Error {
|
||||
constructor(ext: string) {
|
||||
super(`No LSP server registered for extension "${ext}"`);
|
||||
this.name = "UnsupportedExtensionError";
|
||||
}
|
||||
}
|
||||
|
||||
export class ServerNotFoundError extends Error {
|
||||
constructor(command: string) {
|
||||
super(
|
||||
`LSP server binary "${command}" not found on PATH. ` +
|
||||
`Install it and ensure it's on your PATH.`,
|
||||
);
|
||||
this.name = "ServerNotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
export interface ServerConfig {
|
||||
// Stable identifier (useful for logs and future daemon cache keys).
|
||||
id: string;
|
||||
|
||||
Reference in New Issue
Block a user