Skip to content

Commit 97d1ee8

Browse files
committed
improve error messages
1 parent 36d362d commit 97d1ee8

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/main.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
resetDomToDefault,
2525
} from "./settings_frontend.ts";
2626
import { translate } from "./translator/translator.ts";
27+
import { closestString } from "@std/text/closest-string";
2728

2829
const DICTIONARY_AUTO_PARSE_THRESHOLD = 5000;
2930
const INITIAL_PAGE_SIZE = 100;
@@ -61,8 +62,10 @@ const DICTIONARY_LOADING_FAILED_MESSAGE =
6162
"syntax has been updated and your custom dictionary still uses the old " +
6263
"syntax. Please fix it. Apologies for the inconvenience.";
6364
const NO_WORD_MESSAGE = "Please provide a word";
64-
const WORD_NOT_FOUND_MESSAGE = "Word not found";
65-
const WORD_ALREADY_IMPORTED_MESSAGE = "The word is already imported";
65+
const WORD_NOT_FOUND_MESSAGE = (word: string, suggestion: string) =>
66+
`"${word}" doesn't exist in the dictionary. Maybe you mean "${suggestion}".`;
67+
const WORD_ALREADY_IMPORTED_MESSAGE = (word: string) =>
68+
`"${word}" is already imported`;
6669
const DICTIONARY_ERROR_MESSAGE = "Please fix the errors before saving";
6770

6871
function main() {
@@ -337,13 +340,11 @@ function main() {
337340
});
338341
function importWord() {
339342
const word = importWordTextBox.value.trim();
340-
if (word === "") {
341-
showMessage(NO_WORD_MESSAGE);
342-
} else if (
343+
if (
343344
autoParse() && !currentDictionary.isError() &&
344345
currentDictionary.unwrap()[0].has(word)
345346
) {
346-
showMessage(WORD_ALREADY_IMPORTED_MESSAGE);
347+
showMessage(WORD_ALREADY_IMPORTED_MESSAGE(word));
347348
} else {
348349
const definitions = dictionary.get(word)?.source;
349350
if (definitions != null) {
@@ -356,8 +357,19 @@ function main() {
356357
customDictionaryTextBox.scrollHeight,
357358
);
358359
updateIfCanAutoParse();
360+
} else if (word === "") {
361+
showMessage(NO_WORD_MESSAGE);
359362
} else {
360-
showMessage(WORD_NOT_FOUND_MESSAGE);
363+
showMessage(
364+
WORD_NOT_FOUND_MESSAGE(
365+
word,
366+
closestString(
367+
word,
368+
[...dictionary.keys()],
369+
{ caseSensitive: true },
370+
),
371+
),
372+
);
361373
}
362374
}
363375
}

0 commit comments

Comments
 (0)