Skip to content

Commit a40e8b5

Browse files
committed
feat: allow providing custom error, close, exit handling
Override onSpawnError, onSpawnClose, onSpawnExit
1 parent a5c03a5 commit a40e8b5

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

lib/auto-languageclient.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,17 @@ export default class AutoLanguageClient {
381381
}
382382

383383
private captureServerErrors(lsProcess: LanguageServerProcess, projectPath: string): void {
384-
lsProcess.on('error', (err) => this.handleSpawnFailure(err));
385-
lsProcess.on("close", (...args) => this.handleCloseFailure(...args));
386-
lsProcess.on('exit', (code, signal) => this.logger.debug(`exit: code ${code} signal ${signal}`));
384+
lsProcess.on('error', (err) => this.onSpawnError(err));
385+
lsProcess.on("close", (code, signal) => this.onSpawnClose(code, signal));
386+
lsProcess.on('exit', (code, signal) => this.onSpawnExit(code, signal));
387387
lsProcess.stderr?.setEncoding('utf8');
388-
lsProcess.stderr?.on('data', (chunk: Buffer) => {
389-
const errorString = chunk.toString();
390-
this.handleServerStderr(errorString, projectPath);
391-
// Keep the last 5 lines for packages to use in messages
392-
this.processStdErr = (this.processStdErr + errorString)
393-
.split('\n')
394-
.slice(-5)
395-
.join('\n');
396-
});
388+
lsProcess.stderr?.on('data', (chunk: Buffer) => this.onSpawnStdErrData(chunk, projectPath));
397389
}
398390

399-
private handleSpawnFailure(err: any): void {
391+
/** The function called whenever the spawned server `error`s.
392+
* Extend (call super.onSpawnError) or override this if you need custom error handling
393+
*/
394+
protected onSpawnError(err: Error): void {
400395
atom.notifications.addError(
401396
`${this.getServerName()} language server for ${this.getLanguageName()} unable to start`,
402397
{
@@ -406,14 +401,38 @@ export default class AutoLanguageClient {
406401
);
407402
}
408403

409-
private handleCloseFailure(code: number | null, signal: NodeJS.Signals | null): void {
404+
/** The function called whenever the spawned server `close`s.
405+
* Extend (call super.onSpawnClose) or override this if you need custom close handling
406+
*/
407+
protected onSpawnClose(code: number | null, signal: NodeJS.Signals | null): void {
410408
if (code !== 0 && signal === null) {
411409
atom.notifications.addError(
412410
`${this.getServerName()} language server for ${this.getLanguageName()} was closed with code: ${code}.`
413411
);
414412
}
415413
}
416414

415+
416+
/** The function called whenever the spawned server `exit`s.
417+
* Extend (call super.onSpawnExit) or override this if you need custom exit handling
418+
*/
419+
protected onSpawnExit(code: number | null, signal: NodeJS.Signals | null): void {
420+
this.logger.debug(`exit: code ${code} signal ${signal}`);
421+
}
422+
423+
/** The function called whenever the spawned server returns `data` in `stderr`
424+
* Extend (call super.onSpawnStdErrData) or override this if you need custom stderr data handling
425+
*/
426+
protected onSpawnStdErrData(chunk: Buffer, projectPath: string): void {
427+
const errorString = chunk.toString();
428+
this.handleServerStderr(errorString, projectPath);
429+
// Keep the last 5 lines for packages to use in messages
430+
this.processStdErr = (this.processStdErr + errorString)
431+
.split('\n')
432+
.slice(-5)
433+
.join('\n');
434+
}
435+
417436
/** Creates the RPC connection which can be ipc, socket or stdio */
418437
private createRpcConnection(lsProcess: LanguageServerProcess): rpc.MessageConnection {
419438
let reader: rpc.MessageReader;

0 commit comments

Comments
 (0)