Skip to content

Commit c78e35d

Browse files
committed
feat: display scripts errors in FL results
1 parent 9cf7c22 commit c78e35d

7 files changed

Lines changed: 80 additions & 7 deletions

File tree

icon/error.png

1.42 KB
Loading

src/const.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ const __filename = fileURLToPath(import.meta.url);
99
const __dirname = path.dirname(__filename);
1010
const __rootpath = path.resolve(__dirname, '..');
1111

12+
// ─── Scripts ─────────────────────────────────────────────────────────────────────────
13+
1214
export const defaultScriptPath = path.join(__rootpath, 'dist', 'scripts/');
1315

16+
// ─── Icon ────────────────────────────────────────────────────────────────────────────
17+
18+
export const ICON = {
19+
app: path.join(__rootpath, 'icon', 'app.png'),
20+
error: path.join(__rootpath, 'icon', 'error.png')
21+
};
22+
1423
// ┌ ┐
1524
// │ Metas │
1625
// └ ┘

src/controllers/FlowViewController.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ export class FlowViewController {
1414

1515
// ─── METHOD: query ───────────────────────────────────────────────────────────────────
1616
queryScript(parameter: string = '') {
17-
const results = this.scriptManager.search(parameter ?? '');
18-
const resultsFmt = FlowLauncher.searchResultFormat(results);
17+
const normalizeQuery = parameter.toLowerCase();
18+
19+
const errorResults = this.scriptManager.scriptsError;
20+
const errorsFMT = FlowLauncher.searchScriptErrorResultFMT(errorResults);
21+
22+
const scriptResults = this.scriptManager.search(normalizeQuery);
23+
const scriptsFMT = FlowLauncher.searchScriptResultFMT(scriptResults);
1924

2025
// Send result to Flow laucher via JsonRPC
21-
sendJsonRpcRequest({ result: resultsFmt });
26+
sendJsonRpcRequest({ result: [...errorsFMT, ...scriptsFMT] });
2227
}
2328

2429
// ─── METHOD: run ─────────────────────────────────────────────────────────────────────

src/models/ScriptError.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type ScriptErrorArguments = {
2+
file: string;
3+
error: Error;
4+
};
5+
6+
export class ScriptError {
7+
filename: string;
8+
err: Error;
9+
constructor({ file, error }: ScriptErrorArguments) {
10+
this.filename = file;
11+
this.err = error;
12+
}
13+
}

src/scriptManager.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import type { Metas } from './types.js';
44
import { Script, type ScriptArguments } from './models/Script.js';
55
import { ScriptExecution } from './models/ScriptExecution.js';
6+
import { ScriptError } from './models/ScriptError.js';
67
import {
78
defaultScriptPath,
89
metaEndTerm,
@@ -13,12 +14,15 @@ import {
1314
import { matchesObject, sortByProperty, stringRange } from './utils/utils.js';
1415
import { Clipboard } from './utils/clipboard.js';
1516
import { FlowLauncher } from './utils/flowLauncher.js';
17+
import { ensureError } from './utils/error.js';
1618

1719
export class ScriptManager {
1820
scripts: Script[];
21+
scriptsError: ScriptError[];
1922

2023
constructor() {
2124
this.scripts = [];
25+
this.scriptsError = [];
2226
}
2327

2428
init() {
@@ -50,8 +54,13 @@ export class ScriptManager {
5054
});
5155

5256
this.scripts.push(scriptObject);
53-
} catch (e) {
54-
console.error(`Unable to load ${path}, Error: ${e}`);
57+
} catch (err) {
58+
const error = ensureError(err);
59+
const scriptErrorObject = new ScriptError({
60+
file: path,
61+
error: error
62+
});
63+
this.scriptsError.push(scriptErrorObject);
5564
}
5665
}
5766

src/utils/error.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Return Error based on unknown potential error
3+
* @param value the unknown error to convert to true Error
4+
*/
5+
export function ensureError(value: unknown): Error {
6+
if (value instanceof Error) return value;
7+
8+
let stringified = '[Unable to stringify the thrown value]';
9+
try {
10+
stringified = JSON.stringify(value);
11+
} catch {}
12+
13+
const error = new Error(
14+
`This value was thrown as is, not through an Error: ${stringified}`
15+
);
16+
return error;
17+
}

src/utils/flowLauncher.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { ICON } from '../const.js';
12
import type { Script } from '../models/Script.js';
3+
import type { ScriptError } from '../models/ScriptError.js';
24
import type { Result } from '../types.js';
35

46
export const FlowLauncher = {
57
/**
68
* Return reformatted list of scripts for Flow Launcher
79
* @param scripts list of scripts to format.
810
*/
9-
searchResultFormat: function (scripts: Script[]): Result[] {
11+
searchScriptResultFMT: function (scripts: Script[]): Result[] {
1012
return scripts.map(script => ({
1113
Title: script.name ?? 'No Name',
1214
Subtitle: script.desc ?? 'No description',
@@ -20,10 +22,28 @@ export const FlowLauncher = {
2022
})
2123
]
2224
},
23-
IcoPath: 'icon\\app.png',
25+
IcoPath: ICON.app,
2426
score: 0
2527
}));
2628
},
29+
30+
/**
31+
* Return reformatted list of scripts Errors for Flow Launcher
32+
* @param scriptErrors list of scripts errors to format.
33+
*/
34+
searchScriptErrorResultFMT: function (scriptErrors: ScriptError[]): Result[] {
35+
return scriptErrors.map(scriptError => ({
36+
Title: `Unable to load ${scriptError.filename ?? 'No filename'}`,
37+
Subtitle: `Error: ${scriptError.err.message ?? 'No error'}`,
38+
JsonRPCAction: {
39+
method: '', // TODO: add script error function
40+
parameters: []
41+
},
42+
IcoPath: ICON.error,
43+
score: 0
44+
}));
45+
},
46+
2747
/**
2848
* Shows a desktop notification.
2949
* @param title The notification title.

0 commit comments

Comments
 (0)