Skip to content

Commit 74d1124

Browse files
committed
fix(template-variables): log the necessary info if variables form is unable to load
1 parent 206a429 commit 74d1124

4 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { setDefaultTemplatesView } from "./views/defaultTemplates";
77
import { TemplateAction, performAction } from "./actions";
88
import { loadLegacyTemplates } from "./legacyTemplates";
99
import * as open from "open";
10+
import { Logger } from "./logger";
1011

1112
const DOCUMENTATION_URL = "https://github.com/joplin/plugin-templates#readme";
1213

@@ -35,12 +36,15 @@ joplin.plugins.register({
3536
const userLocale = await joplin.settings.globalValue("locale");
3637
const userDateFormat = await joplin.settings.globalValue("dateFormat");
3738
const userTimeFormat = await joplin.settings.globalValue("timeFormat");
39+
const profileDir = await joplin.settings.globalValue("profileDir");
40+
3841
const dateAndTimeUtils = new DateAndTimeUtils(userLocale, userDateFormat, userTimeFormat);
39-
const parser = new Parser(dateAndTimeUtils, dialogViewHandle);
42+
const logger = new Logger(profileDir);
43+
const parser = new Parser(dateAndTimeUtils, dialogViewHandle, logger);
4044

4145

4246
// Asynchronously load legacy templates
43-
loadLegacyTemplates(dateAndTimeUtils);
47+
loadLegacyTemplates(dateAndTimeUtils, profileDir);
4448

4549

4650
// Utility Functions

src/legacyTemplates.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ const getTemplatesTag = async (): Promise<string> => {
2222
return (await getAnyTagWithTitle("template")).id;
2323
}
2424

25-
export const loadLegacyTemplates = async (dateAndTimeUtils: DateAndTimeUtils): Promise<void> => {
25+
export const loadLegacyTemplates = async (dateAndTimeUtils: DateAndTimeUtils, profileDir: string): Promise<void> => {
2626
const fs = joplin.require("fs-extra");
2727

2828
let folderId = null;
2929
let templatesTagId = null;
3030

31-
const profileDir = await joplin.settings.globalValue("profileDir");
3231
const templatesDir = `${profileDir}/templates`;
3332

3433
if (await fs.pathExists(templatesDir)) {

src/logger.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import joplin from "api";
2+
3+
const fs = joplin.require("fs-extra");
4+
5+
export class Logger {
6+
private logsFile: string;
7+
8+
constructor(profileDir: string) {
9+
this.logsFile = `${profileDir}/templates-logs.txt`;
10+
}
11+
12+
public async log(message: string): Promise<void> {
13+
await fs.appendFile(this.logsFile, `[${new Date().toISOString()}]\n${message}\n\n\n`);
14+
}
15+
}

src/parser.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import joplin from "api";
22
import * as Handlebars from "handlebars/dist/handlebars";
3+
import { Logger } from "./logger";
34
import { DateAndTimeUtils } from "./utils/dateAndTime";
45
import { Note } from "./utils/templates";
56
import { setTemplateVariablesView } from "./views/templateVariables";
@@ -13,10 +14,12 @@ const frontmatter = require("front-matter");
1314
export class Parser {
1415
private utils: DateAndTimeUtils;
1516
private dialog: string;
17+
private logger: Logger;
1618

17-
constructor(dateAndTimeUtils: DateAndTimeUtils, dialogViewHandle: string) {
19+
constructor(dateAndTimeUtils: DateAndTimeUtils, dialogViewHandle: string, logger: Logger) {
1820
this.utils = dateAndTimeUtils;
1921
this.dialog = dialogViewHandle;
22+
this.logger = logger;
2023
}
2124

2225
private getDefaultContext() {
@@ -68,7 +71,24 @@ export class Parser {
6871
return null;
6972
}
7073

71-
const userResponse = dialogResponse.formData.variables;
74+
let userResponse;
75+
76+
// There's a try catch block here because a user experienced an error
77+
// due to the following line. I've added a try catch block to log the
78+
// necessary info to find the root cause of the error in case it happens again.
79+
// Reference -> https://github.com/joplin/plugin-templates/issues/6
80+
try {
81+
userResponse = dialogResponse.formData.variables;
82+
} catch (err) {
83+
console.error("Template variables form was not able to load properly.", err);
84+
console.error("DEBUG INFO", variables, dialogResponse, title);
85+
86+
const message = (`Template variables form was not able to load properly.\n${err}\nDEBUG INFO\nvariables: ${JSON.stringify(variables)}\ndialogResponse: ${JSON.stringify(dialogResponse)}\ntitle: ${JSON.stringify(title)}`);
87+
this.logger.log(message);
88+
89+
throw new Error(err);
90+
}
91+
7292
return this.mapUserResponseToVariables(variables, userResponse);
7393
}
7494

0 commit comments

Comments
 (0)