mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-31 18:18:36 +08:00
feat: enhance logging and error handling in upload commands and base class
This commit is contained in:
@@ -1,12 +1,39 @@
|
||||
import {i18nConfig} from "../lang/I18n";
|
||||
import {App, Notice} from "obsidian";
|
||||
import {Upload2Notion} from "./Upload2Notion";
|
||||
import type {NotionPageResponse} from "./common/UploadBase";
|
||||
import {DatabaseDetails, PluginSettings} from "../ui/settingTabs";
|
||||
import ObsidianSyncNotionPlugin from "../main";
|
||||
import {getNowFileMarkdownContentNext} from "./common/getMarkdownNext";
|
||||
import {getNowFileMarkdownContentGeneral} from "./common/getMarkdownGeneral";
|
||||
import {getNowFileMarkdownContentCustom} from "./common/getMarkdownCustom";
|
||||
|
||||
const SYNC_ERROR_NOTICE_DURATION = 8000;
|
||||
|
||||
function extractErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error && error.message) {
|
||||
return error.message;
|
||||
}
|
||||
return String(error);
|
||||
}
|
||||
|
||||
function notifySyncError(prefix: string, basename: string, error: unknown): void {
|
||||
const errorMessage = extractErrorMessage(error);
|
||||
console.error(`${prefix} Sync failed`, error);
|
||||
new Notice(
|
||||
`${i18nConfig["sync-fail"]} ${basename}. ${errorMessage}\n${i18nConfig["CheckConsole"]}`,
|
||||
SYNC_ERROR_NOTICE_DURATION,
|
||||
);
|
||||
}
|
||||
|
||||
function logCommandDebug(context: string, message: string, payload?: Record<string, unknown>): void {
|
||||
if (payload) {
|
||||
console.log(`[${context}] ${message}`, payload);
|
||||
} else {
|
||||
console.log(`[${context}] ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadCommandNext(
|
||||
plugin: ObsidianSyncNotionPlugin,
|
||||
settings: PluginSettings,
|
||||
@@ -16,6 +43,10 @@ export async function uploadCommandNext(
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
console.log(`[uploadCommandNext] ${new Date().toISOString()} Triggered for file`, app.workspace.getActiveFile()?.path);
|
||||
logCommandDebug("uploadCommandNext", "Command invoked", {
|
||||
databaseId: databaseID,
|
||||
apiTokenPreview: `${notionAPI?.slice(0, 4) ?? ""}***`,
|
||||
});
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
@@ -38,38 +69,76 @@ export async function uploadCommandNext(
|
||||
paword,
|
||||
favicon,
|
||||
datetime
|
||||
} = await getNowFileMarkdownContentNext(app, settings)
|
||||
} = await getNowFileMarkdownContentNext(app, settings);
|
||||
|
||||
logCommandDebug("uploadCommandNext", "Metadata extracted from markdown", {
|
||||
hasMarkdown: !!markDownData,
|
||||
markdownLength: markDownData?.length ?? 0,
|
||||
tagCount: tags?.length ?? 0,
|
||||
coverIncluded: !!cover,
|
||||
hasEmoji: !!emoji,
|
||||
type,
|
||||
slug,
|
||||
hasPassword: !!paword,
|
||||
datetime,
|
||||
});
|
||||
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
logCommandDebug("uploadCommandNext", "Preparing to upload", {
|
||||
filename: basename,
|
||||
filePath: nowFile.path,
|
||||
});
|
||||
|
||||
const upload = new Upload2Notion(plugin, dbDetails);
|
||||
const res = await upload.sync({
|
||||
dataset: "next",
|
||||
title: basename,
|
||||
emoji: emoji || "",
|
||||
cover: cover || "",
|
||||
tags: tags || [],
|
||||
type: type || "",
|
||||
slug: slug || "",
|
||||
stats: stats || "",
|
||||
category: category || "",
|
||||
summary: summary || "",
|
||||
password: paword || "",
|
||||
favicon: favicon || "",
|
||||
datetime: datetime || "",
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
let res: NotionPageResponse;
|
||||
try {
|
||||
res = await upload.sync({
|
||||
dataset: "next",
|
||||
title: basename,
|
||||
emoji: emoji || "",
|
||||
cover: cover || "",
|
||||
tags: tags || [],
|
||||
type: type || "",
|
||||
slug: slug || "",
|
||||
stats: stats || "",
|
||||
category: category || "",
|
||||
summary: summary || "",
|
||||
password: paword || "",
|
||||
favicon: favicon || "",
|
||||
datetime: datetime || "",
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
notifySyncError("[uploadCommandNext]", basename, error);
|
||||
logCommandDebug("uploadCommandNext", "Sync threw error", {
|
||||
filename: basename,
|
||||
error: extractErrorMessage(error),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const {response} = res;
|
||||
if (response.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||
console.log(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`);
|
||||
logCommandDebug("uploadCommandNext", "Sync succeeded", {
|
||||
filename: basename,
|
||||
status: response.status,
|
||||
pageId: res.data?.id ?? null,
|
||||
pageUrl: res.data?.url ?? null,
|
||||
});
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||
console.log(`${i18nConfig["sync-fail"]} ${basename}`);
|
||||
logCommandDebug("uploadCommandNext", "Sync failed with status", {
|
||||
filename: basename,
|
||||
status: response.status,
|
||||
errorCode: res.data?.code ?? null,
|
||||
errorStatus: res.data?.status ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -85,6 +154,10 @@ export async function uploadCommandGeneral(
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
console.log(`[uploadCommandGeneral] ${new Date().toISOString()} Triggered for file`, app.workspace.getActiveFile()?.path);
|
||||
logCommandDebug("uploadCommandGeneral", "Command invoked", {
|
||||
databaseId: databaseID,
|
||||
apiTokenPreview: `${notionAPI?.slice(0, 4) ?? ""}***`,
|
||||
});
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
@@ -100,25 +173,53 @@ export async function uploadCommandGeneral(
|
||||
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
logCommandDebug("uploadCommandGeneral", "Preparing to upload", {
|
||||
filename: basename,
|
||||
markdownLength: markDownData.length,
|
||||
tagCount: tags?.length ?? 0,
|
||||
hasCover: !!cover,
|
||||
});
|
||||
|
||||
const upload = new Upload2Notion(plugin, dbDetails);
|
||||
const res = await upload.sync({
|
||||
dataset: "general",
|
||||
title: basename,
|
||||
cover: cover || "",
|
||||
tags: tags || [],
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
let res: NotionPageResponse;
|
||||
try {
|
||||
res = await upload.sync({
|
||||
dataset: "general",
|
||||
title: basename,
|
||||
cover: cover || "",
|
||||
tags: tags || [],
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
notifySyncError("[uploadCommandGeneral]", basename, error);
|
||||
logCommandDebug("uploadCommandGeneral", "Sync threw error", {
|
||||
filename: basename,
|
||||
error: extractErrorMessage(error),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const {response} = res;
|
||||
if (response.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||
console.log(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`);
|
||||
logCommandDebug("uploadCommandGeneral", "Sync succeeded", {
|
||||
filename: basename,
|
||||
status: response.status,
|
||||
pageId: res.data?.id ?? null,
|
||||
pageUrl: res.data?.url ?? null,
|
||||
});
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||
console.log(`${i18nConfig["sync-fail"]} ${basename}`);
|
||||
logCommandDebug("uploadCommandGeneral", "Sync failed with status", {
|
||||
filename: basename,
|
||||
status: response.status,
|
||||
errorCode: res.data?.code ?? null,
|
||||
errorStatus: res.data?.status ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -134,6 +235,10 @@ export async function uploadCommandCustom(
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
console.log(`[uploadCommandCustom] ${new Date().toISOString()} Triggered for file`, app.workspace.getActiveFile()?.path);
|
||||
logCommandDebug("uploadCommandCustom", "Command invoked", {
|
||||
databaseId: databaseID,
|
||||
apiTokenPreview: `${notionAPI?.slice(0, 4) ?? ""}***`,
|
||||
});
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
@@ -149,25 +254,53 @@ export async function uploadCommandCustom(
|
||||
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
logCommandDebug("uploadCommandCustom", "Preparing to upload", {
|
||||
filename: basename,
|
||||
markdownLength: markDownData.length,
|
||||
hasCover: !!cover,
|
||||
customValueKeys: Object.keys(customValues || {}),
|
||||
});
|
||||
|
||||
const upload = new Upload2Notion(plugin, dbDetails);
|
||||
const res = await upload.sync({
|
||||
dataset: "custom",
|
||||
cover: cover || "",
|
||||
customValues,
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
let res: NotionPageResponse;
|
||||
try {
|
||||
res = await upload.sync({
|
||||
dataset: "custom",
|
||||
cover: cover || "",
|
||||
customValues,
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
notifySyncError("[uploadCommandCustom]", basename, error);
|
||||
logCommandDebug("uploadCommandCustom", "Sync threw error", {
|
||||
filename: basename,
|
||||
error: extractErrorMessage(error),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const {response} = res;
|
||||
|
||||
if (response.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||
console.log(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`);
|
||||
logCommandDebug("uploadCommandCustom", "Sync succeeded", {
|
||||
filename: basename,
|
||||
status: response.status,
|
||||
pageId: res.data?.id ?? null,
|
||||
pageUrl: res.data?.url ?? null,
|
||||
});
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||
console.log(`${i18nConfig["sync-fail"]} ${basename}`);
|
||||
logCommandDebug("uploadCommandCustom", "Sync failed with status", {
|
||||
filename: basename,
|
||||
status: response.status,
|
||||
errorCode: res.data?.code ?? null,
|
||||
errorStatus: res.data?.status ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user