Skip to content

Commit 2007373

Browse files
committed
feat: add script user error display function
1 parent 44b9e0f commit 2007373

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/controllers/FlowViewController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ export class FlowViewController {
3131
const scriptArguments = JSON.parse(parameters);
3232
this.scriptManager.runScriptM(scriptArguments);
3333
}
34+
35+
// ─── METHOD: error ───────────────────────────────────────────────────────────────────
36+
displaySelectedUserError(parameters: string) {
37+
this.scriptManager.displayUserError(parameters);
38+
}
3439
}

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ if (method === 'query') {
1919
if (method === 'run') {
2020
flowViewController.runSelectedScript(parameters[0] as string);
2121
}
22+
23+
if (method === 'error') {
24+
flowViewController.displaySelectedUserError(parameters[0] as string);
25+
}

src/scriptManager.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ export class ScriptManager {
8787
return sortByProperty(results, SORT_PROPERTY);
8888
}
8989

90+
displayUserError(errorObjectString: string) {
91+
// TODO: Better origanisation for all patterns (regex, escpape char)
92+
const ESCAPE_LINE_FEED = /\\n/g;
93+
const LINE_FEED = '\n';
94+
const AT_PATTERN = /\s*\sat\s.[^"]*/g;
95+
const NOTHING = ' ';
96+
97+
let prettyErrorString = errorObjectString.replace(
98+
ESCAPE_LINE_FEED,
99+
LINE_FEED
100+
);
101+
prettyErrorString = prettyErrorString.replace(AT_PATTERN, NOTHING);
102+
this.replaceText(prettyErrorString);
103+
}
104+
90105
runScriptM(scriptArguments: ScriptArguments) {
91106
// Get clipboard content
92107
const clip = Clipboard.get();
@@ -98,15 +113,15 @@ export class ScriptManager {
98113
this.replaceText(result, clip);
99114
}
100115

101-
runScript(script: Script, text: string) {
116+
runScript(script: Script, text: string): string {
102117
let scriptExecution = new ScriptExecution({ text: text });
103118

104119
script.run(scriptExecution);
105120

106121
return scriptExecution.text ?? '';
107122
}
108123

109-
replaceText(newText: string, originText: string) {
124+
replaceText(newText: string, originText: string = '') {
110125
if (newText !== originText) {
111126
// Update clipboard with new value
112127
Clipboard.copy(newText);

src/utils/flowLauncher.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,25 @@ export const FlowLauncher = {
3232
* @param scriptErrors list of scripts errors to format.
3333
*/
3434
searchScriptErrorResultFMT: function (scriptErrors: ScriptError[]): Result[] {
35+
const REPLACER = null;
36+
const SPACE = 4;
3537
return scriptErrors.map(scriptError => ({
3638
Title: `Unable to load ${scriptError.filename ?? 'No filename'}`,
3739
Subtitle: `Error: ${scriptError.err.message ?? 'No error'}`,
3840
JsonRPCAction: {
39-
method: '', // TODO: add script error function
40-
parameters: []
41+
method: 'error',
42+
parameters: [
43+
JSON.stringify(
44+
{
45+
filename: scriptError.filename,
46+
name: scriptError.err.name,
47+
message: scriptError.err.message,
48+
stack: scriptError.err.stack
49+
},
50+
REPLACER,
51+
SPACE
52+
)
53+
]
4154
},
4255
IcoPath: ICON.error,
4356
score: 0

0 commit comments

Comments
 (0)