-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherror.ts
More file actions
50 lines (47 loc) · 1.34 KB
/
error.ts
File metadata and controls
50 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { Denops } from "@denops/std";
import { AssertError } from "@core/unknownutil/assert";
/**
* Application error that is used to represent an expected error.
*/
export class ExpectedError extends Error {
constructor(message: string, public source?: unknown) {
super(message);
}
}
export async function handleError(
denops: Denops,
error: unknown,
): Promise<void> {
if (error instanceof ExpectedError) {
await denops.cmd(
`redraw | echohl Error | echomsg '[fall] ${error.message}' | echohl None`,
);
return;
}
if (error instanceof AssertError) {
await denops.cmd(
`redraw | echohl Error | echomsg '[fall] ${error.message}' | echohl None`,
);
return;
}
console.error(error);
}
export function withHandleError<T, A extends unknown[]>(
denops: Denops,
fn: (...args: A) => T | Promise<T>,
): (...args: A) => Promise<T | undefined> {
return async (...args: A): Promise<T | undefined> => {
try {
return await fn(...args);
} catch (e) {
// NOTE:
// This setTimeout is required in Neovim to show the error message
// properly. Otherwise, the error message is not shown maybe because
// the msgarea is hidden during the picker execution.
setTimeout(
() => handleError(denops, e).catch((err) => console.error(err)),
0,
);
}
};
}