Skip to content

Commit b741eb4

Browse files
committed
refactor: move common error messages to lang
1 parent ae59f9c commit b741eb4

12 files changed

Lines changed: 31 additions & 22 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "lineage",
33
"name": "Lineage",
4-
"version": "0.5.0-dev",
4+
"version": "0.5.1",
55
"minAppVersion": "0.15.0",
66
"description": "A writing interface that combines structure and content. Inspired by Gingko Writer.",
77
"author": "ycnmhd",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lineage",
3-
"version": "0.5.0-dev",
3+
"version": "0.5.1",
44
"description": "",
55
"scripts": {
66
"dev": "node esbuild.config.mjs",

src/helpers/store/on-plugin-error.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SilentError } from 'src/stores/view/helpers/errors';
22
import { Notice } from 'obsidian';
3+
import { lang } from 'src/lang/lang';
34

45
export const onPluginError = (
56
error: Error,
@@ -11,6 +12,8 @@ export const onPluginError = (
1112
console.error(`[${location}] action: `, action);
1213
// eslint-disable-next-line no-console
1314
console.error(`[${location}]`, error);
14-
new Notice('Lineage plugin: ' + error.message);
15+
const message = error.message.replace(/Invariant failed(: )?/, '');
16+
if (message) new Notice(message);
17+
else new Notice(lang.error_generic);
1518
}
1619
};

src/lang/lang.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ export const lang = {
77
new_file: 'New lineage document',
88
format_headings: 'Format headings',
99
extract_branch: 'Extract branch to a new document',
10+
error_apply_snapshot_while_editing: 'Cannot apply a snapshot while editing',
11+
error_delete_last_node: 'Cannot delete the last card',
12+
error_generic:
13+
'Something went wrong\nFurther details may be available in the developer console',
14+
error_parent_not_found: (full: string) =>
15+
`Could not find the parent section of ${full}`,
1016
};

src/stores/document/reducers/delete-node/delete-node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isLastRootNode } from 'src/stores/document/reducers/delete-node/helpers
55
import invariant from 'tiny-invariant';
66
import { findNextActiveNode } from 'src/stores/view/reducers/document/helpers/find-next-node/find-next-active-node';
77
import { deleteNodeById } from 'src/stores/document/reducers/delete-node/helpers/delete-node-by-id';
8+
import { lang } from 'src/lang/lang';
89

910
export type DeleteNodeAction = {
1011
type: 'DOCUMENT/DELETE_NODE';
@@ -17,15 +18,15 @@ export const deleteNode = (document: LineageDocument, nodeId: string) => {
1718
invariant(nodeId);
1819

1920
const lastNode = isLastRootNode(document.columns, nodeId);
20-
if (lastNode) throw new Error('cannot delete last root node');
21+
if (lastNode) throw new Error(lang.error_delete_last_node);
2122

2223
const nextNode = findNextActiveNode(document.columns, nodeId, {
2324
type: 'DOCUMENT/DELETE_NODE',
2425
payload: {
2526
activeNodeId: nodeId,
2627
},
2728
});
28-
if (!nextNode) throw new Error('could not find next node');
29+
invariant(nextNode);
2930
deleteChildNodes(document, nodeId);
3031
deleteNodeById(document.columns, document.content, nodeId);
3132
cleanAndSortColumns(document);

src/stores/document/reducers/insert-node/insert-node.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ export const insertNode = (
3030
payload.activeNodeId,
3131
);
3232
const column = document.columns[columnIndex];
33+
invariant(column);
3334
const group = findGroupByNodeId([column], payload.activeNodeId);
3435
invariant(group, 'could not find group of ' + payload.activeNodeId);
3536

3637
const groupIndex = group.nodes.findIndex(
3738
(c) => c === payload.activeNodeId,
3839
);
39-
if (columnIndex === -1 || groupIndex === -1)
40-
throw new Error('could not find node index');
4140

4241
const insertionIndex =
4342
action.payload.position === 'up' ? groupIndex : groupIndex + 1;

src/stores/hotkeys/reducers/load-custom-hotkeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { updateHotkey } from 'src/stores/hotkeys/reducers/update-hotkey';
55
import { Hotkey } from 'obsidian';
66
import { CustomHotkeys } from 'src/stores/settings/settings-type';
77
import { commandToHotkeys } from 'src/stores/hotkeys/reducers/helpers/command-to-hotkeys';
8+
import invariant from 'tiny-invariant';
89

910
export type LoadCustomHotkeysAction = {
1011
type: 'SETTINGS/LOAD_CUSTOM_HOTKEYS';
@@ -24,8 +25,7 @@ export const loadCustomHotkeys = (
2425
state.customHotkeys[name as CommandName] = customHotkey;
2526
}
2627
}
27-
if (!pluginCommands.current)
28-
throw new Error('plugin commands are undefined');
28+
invariant(pluginCommands.current);
2929
state.hotkeys = [];
3030
for (const pluginCommand of pluginCommands.current) {
3131
const hotkey: CommandHotkeys = commandToHotkeys(pluginCommand);

src/stores/view/helpers/json-to-md/markdown-to-json/markdown-to-json.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { parseDelimiter } from 'src/stores/view/helpers/json-to-md/markdown-to-json/helpers/delimiter';
22
import { TreeNode } from 'src/stores/view/helpers/json-to-md/columns-to-json/columns-to-json-tree';
3+
import { lang } from 'src/lang/lang';
34

45
const depthLevel = (number: string) => {
56
if (number.includes('.')) {
@@ -46,7 +47,7 @@ export const markdownToJson = (text: string) => {
4647
depthLevel(parent) > depthLevel(currentParentNumber);
4748
if (isChild) {
4849
if (!currentNode) {
49-
throw new Error(`could not find parent of ${full}`);
50+
throw new Error(lang.error_parent_not_found(full));
5051
}
5152
trimCurrentNode(currentNode);
5253
currentNode.children.push(newNode);
@@ -59,7 +60,7 @@ export const markdownToJson = (text: string) => {
5960
} else {
6061
const parentNode = map[parent];
6162
if (!parentNode) {
62-
throw new Error(`could not find parent of ${full}`);
63+
throw new Error(lang.error_parent_not_found(full));
6364
}
6465
if (currentNode) trimCurrentNode(currentNode);
6566
parentNode.children.push(newNode);

src/view/components/container/controls-bar/controls-container.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
const history = historyStore(view);
3232
const handleNextClick = () => {
3333
if (viewStore.getValue().document.editing.activeNodeId)
34-
new Notice('cannot apply snapshot while editing');
34+
new Notice(lang.error_apply_snapshot_while_editing );
3535
else
3636
documentStore.dispatch({
3737
type: 'HISTORY/APPLY_NEXT_SNAPSHOT',
@@ -40,7 +40,7 @@
4040
4141
const handlePreviousClick = () => {
4242
if (viewStore.getValue().document.editing.activeNodeId)
43-
new Notice('cannot apply snapshot while editing');
43+
new Notice(lang.error_apply_snapshot_while_editing);
4444
else
4545
documentStore.dispatch({
4646
type: 'HISTORY/APPLY_PREVIOUS_SNAPSHOT',

src/view/components/container/controls-bar/modals/snapshots-list/components/snapshot-button.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { getView } from '../../../../context';
88
import { Notice } from 'obsidian';
99
import invariant from 'tiny-invariant';
10+
import { lang } from 'src/lang/lang';
1011
1112
export let snapshot: Snapshot;
1213
export let active: boolean;
@@ -32,7 +33,7 @@
3233
class:selected={active}
3334
on:click={() => {
3435
if (viewStore.getValue().document.editing.activeNodeId)
35-
new Notice('cannot apply snapshot while editing');
36+
new Notice(lang.error_apply_snapshot_while_editing);
3637
else
3738
documentStore.dispatch({
3839
type: 'HISTORY/SELECT_SNAPSHOT',

0 commit comments

Comments
 (0)