Skip to content

Commit 8c65cb4

Browse files
committed
feat: add error handling and notifications for plugin settings and registration
1 parent d9c0a4a commit 8c65cb4

File tree

6 files changed

+133
-15
lines changed

6 files changed

+133
-15
lines changed

src/i18n/en.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ export const EN = {
1010
time: "",
1111
},
1212
},
13+
"notice:load-settings-error": "Error loading plugin settings, please check the console logs.",
14+
"notice:register-plugin-error": "Error registering plugin, please check the console logs.",
1315
"command:format-content-name": "Format all content",
1416
"command:format-selection-name": "Format selected content",
17+
"setting:error-boundary-title": "Settings panel failed to load",
18+
"setting:error-boundary-description":
19+
"Error loading settings panel, please check the error message below.",
1520
"setting:format-on-save-name": "Format on save",
1621
"setting:format-on-save-description": "Format the current content when saving the file.",
1722
"setting:format-on-file-change-name": "Format on file change",

src/i18n/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import type { JSX } from "jsx/jsx-runtime";
22

33
export type LangKey =
44
| "notice:format-too-slow"
5+
| "notice:load-settings-error"
6+
| "notice:register-plugin-error"
57
| "command:format-content-name"
68
| "command:format-selection-name"
9+
| "setting:error-boundary-title"
10+
| "setting:error-boundary-description"
711
| "setting:format-on-save-name"
812
| "setting:format-on-save-description"
913
| "setting:format-on-file-change-name"

src/i18n/zh-cn.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ export const ZH_CN = {
99
time: "",
1010
},
1111
},
12+
"notice:load-settings-error": "加载插件设置时出错,请检查控制台日志。",
13+
"notice:register-plugin-error": "注册插件时出错,请检查控制台日志。",
1214
"command:format-content-name": "格式化全部内容",
1315
"command:format-selection-name": "格式化选定内容",
16+
"setting:error-boundary-title": "设置面板加载失败",
17+
"setting:error-boundary-description": "加载设置面板时出错,请检查下方的错误信息。",
1418
"setting:format-on-save-name": "保存时格式化",
1519
"setting:format-on-save-description": "保存文件时是否格式化当前内容。",
1620
"setting:format-on-file-change-name": "文件更改时格式化",

src/main.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Formatter } from "./formatter";
44
import { fmt } from "./i18n";
55
import { getCurrentVersion, getDefaultSettings, migrate } from "./model";
66
import { SettingsTab } from "./setting";
7-
import { withPerfNotice } from "./utils/common";
7+
import { logger, showNotice, withPerfNotice } from "./utils/common";
88

99
import type { Data } from "./model";
1010
import type { Command, EventRef, TFile } from "obsidian";
@@ -19,16 +19,45 @@ export default class PrettierPlugin extends Plugin {
1919
private originalSaveCallback: Command["checkCallback"];
2020

2121
override async onload() {
22-
await this.loadSettings();
22+
logger("Loading plugin...");
23+
24+
try {
25+
await this.loadSettings();
26+
} catch (error) {
27+
logger("Error loading plugin settings:", error);
28+
showNotice(fmt("notice:load-settings-error"));
29+
}
2330

2431
this.formatter = new Formatter(this);
2532

26-
this.registerCommands();
27-
this.registerEvents();
28-
this.hookSaveCommands();
33+
try {
34+
try {
35+
const { formatContentCommand, formatSelectionCommand } = this.registerCommands();
36+
if (!formatContentCommand || !formatSelectionCommand) {
37+
const missing = [];
38+
if (!formatContentCommand) missing.push("format-content");
39+
if (!formatSelectionCommand) missing.push("format-selection");
40+
41+
throw new Error(`Failed to register commands: ${missing.join(", ")}`);
42+
}
43+
} catch (error) {
44+
logger("Error registering commands:", error);
45+
}
2946

47+
try {
48+
this.hookSaveCommands();
49+
} catch (error) {
50+
logger("Error hooking save commands:", error);
51+
}
52+
} catch {
53+
showNotice(fmt("notice:register-plugin-error"));
54+
}
55+
56+
this.registerEvents();
3057
this.registerMenu();
3158

59+
logger("Plugin loaded.");
60+
3261
this.addSettingTab(new SettingsTab(this));
3362
}
3463

@@ -54,15 +83,15 @@ export default class PrettierPlugin extends Plugin {
5483
}
5584

5685
private registerCommands() {
57-
this.addCommand({
86+
const formatContentCommand = this.addCommand({
5887
id: "format-content",
5988
name: fmt("command:format-content-name"),
6089
editorCallback: async (editor, view) => {
6190
await withPerfNotice(() => this.formatter.formatContent(editor, view.file));
6291
},
6392
});
6493

65-
this.addCommand({
94+
const formatSelectionCommand = this.addCommand({
6695
id: "format-selection",
6796
name: fmt("command:format-selection-name"),
6897
editorCheckCallback: (checking, editor, view) => {
@@ -73,6 +102,8 @@ export default class PrettierPlugin extends Plugin {
73102
return editor.somethingSelected();
74103
},
75104
});
105+
106+
return { formatContentCommand, formatSelectionCommand };
76107
}
77108

78109
private registerEvents() {

src/setting.ts

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import { fmt } from "./i18n";
1111
import { getDefaultFormatOptions, getDefaultIgnorePatterns } from "./model";
12+
import { logger } from "./utils/common";
1213

1314
import type PrettierPlugin from "./main";
1415
import type { Settings } from "./model";
@@ -39,14 +40,55 @@ export class SettingsTab extends PluginSettingTab {
3940
display() {
4041
this.containerEl.empty();
4142

42-
this.addFormatOnSave();
43-
this.addFormatOnFileChange();
44-
this.addFormatCodeBlock();
45-
// this.addRemoveExtraSpaces();
46-
this.addAddTrailingSpaces();
47-
this.addLanguageMappings();
48-
this.addFormatOptions();
49-
this.addIgnorePatterns();
43+
try {
44+
this.addFormatOnSave();
45+
this.addFormatOnFileChange();
46+
this.addFormatCodeBlock();
47+
// this.addRemoveExtraSpaces();
48+
this.addAddTrailingSpaces();
49+
this.addLanguageMappings();
50+
this.addFormatOptions();
51+
this.addIgnorePatterns();
52+
} catch (error) {
53+
logger("Error displaying settings tab:", error);
54+
this.containerEl.empty();
55+
this.showErrorBoundary(error);
56+
}
57+
}
58+
59+
private showErrorBoundary(error: unknown) {
60+
const errorBoundary = this.containerEl.createDiv("prettier-settings__error-boundary");
61+
errorBoundary.createSpan({
62+
text: fmt("setting:error-boundary-title"),
63+
cls: "prettier-settings__error-boundary-title",
64+
});
65+
errorBoundary.createSpan({
66+
text: fmt("setting:error-boundary-description"),
67+
cls: "prettier-settings__error-boundary-description",
68+
});
69+
70+
let message = "";
71+
if (error instanceof Error) {
72+
message = error.message;
73+
74+
if (error.stack) {
75+
message += `\n\n${error.stack}`;
76+
}
77+
} else if (typeof error === "string") {
78+
message = error;
79+
} else {
80+
try {
81+
message = JSON.stringify(error, null, 2);
82+
} catch {
83+
message = String(error);
84+
}
85+
}
86+
87+
errorBoundary
88+
.createEl("pre", "prettier-settings__error-boundary-message")
89+
.createEl("code", {
90+
text: message,
91+
});
5092
}
5193

5294
private addFormatOnSave() {

src/styles.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
.prettier-settings__error-boundary-title {
2+
display: block;
3+
font-size: 1.4em;
4+
}
5+
6+
.prettier-settings__error-boundary-description {
7+
display: block;
8+
font-size: 1em;
9+
margin-top: 1.2em;
10+
}
11+
12+
.prettier-settings__error-boundary-message {
13+
display: block;
14+
overflow: auto;
15+
margin-top: 2.4em;
16+
padding: 1em;
17+
max-height: 20em;
18+
color: var(--text-muted);
19+
font-family: var(--font-monospace);
20+
font-size: 0.85em;
21+
line-height: 1.4em;
22+
white-space: pre-wrap;
23+
word-break: break-all;
24+
border: 1px solid var(--background-modifier-border);
25+
border-radius: 0.25em;
26+
background: var(--background-secondary);
27+
}
28+
29+
.prettier-settings__error-boundary-message > code {
30+
display: block;
31+
}
32+
133
.setting-item-extra {
234
display: flex;
335
flex-direction: column;

0 commit comments

Comments
 (0)