Skip to content

Commit

Permalink
Add additional error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Mar 31, 2022
1 parent c1c7cca commit 37e6a96
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-zotero-desktop-connector",
"name": "Zotero Desktop Connector",
"version": "1.1.4",
"version": "1.1.5",
"minAppVersion": "0.12.0",
"description": "Insert citations, bibliographies, and notes directly from Zotero desktop.",
"author": "mgmeyers",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"builtin-modules": "3.2.0",
"cross-env": "7.0.3",
"esbuild": "0.13.12",
"obsidian": "0.13.30",
"obsidian": "0.14.4",
"obsidian-plugin-cli": "0.8.1",
"tslib": "2.3.1",
"typescript": "4.4.4",
Expand Down
22 changes: 11 additions & 11 deletions src/DebugView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ export class DebugView extends ItemView {
{ text: "Prompt For Selection" },
(btn) => {
btn.onClickEvent((e) => {
pdfDebugPrompt(this.plugin.settings).then(
(res) => {
if (!res || res.length === 0) {
this.wrapper.innerText = "No data retrieved";
}

pdfDebugPrompt(this.plugin.settings).then((res) => {
if (!res || res.length === 0) {
this.wrapper.innerText = "No data retrieved";
} else {
this.mountJsonViewer(res[0]);
}
);
});
});
}
);
Expand All @@ -97,12 +95,14 @@ export class DebugView extends ItemView {
<JSONTree
data={json}
sortObjectKeys={(a: string, b: string) => a.localeCompare(b)}
isCustomNode={v => v instanceof moment}
valueRenderer={v => {
isCustomNode={(v) => v instanceof moment}
valueRenderer={(v) => {
if (v instanceof moment) {
return `moment(${(v as moment.Moment).toLocaleString()})`
return `moment(${(
v as moment.Moment
).toLocaleString()})`;
}
return v
return v;
}}
theme={
document.body.hasClass("theme-dark")
Expand Down
32 changes: 17 additions & 15 deletions src/bbt/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ function processNote(note: any) {
note.note = htmlToMarkdown(note.note);
}
if (note.dateAdded) {
note.dateAdded = moment(new Date(note.dateAdded));
note.dateAdded = moment(note.dateAdded);
}

if (note.dateModified) {
note.dateModified = moment(new Date(note.dateModified));
note.dateModified = moment(note.dateModified);
}
}

function processAttachment(attachment: any) {
if (attachment.dateAdded) {
attachment.dateAdded = moment(new Date(attachment.dateAdded));
attachment.dateAdded = moment(attachment.dateAdded);
}

if (attachment.dateModified) {
attachment.dateModified = moment(new Date(attachment.dateModified));
attachment.dateModified = moment(attachment.dateModified);
}

if (attachment.uri) {
Expand All @@ -57,7 +57,7 @@ function processAnnotation(
attachment: any,
imageRelativePath: any
) {
annotation.date = moment(new Date(annotation.date));
annotation.date = moment(annotation.date);
annotation.attachment = attachment;

if (annotation.imagePath) {
Expand All @@ -79,27 +79,25 @@ async function processItem(
item.desktopURI = `zotero://select/library/items/${item.itemKey}`;

if (item.accessDate) {
item.accessDate = moment(new Date(item.accessDate));
item.accessDate = moment(item.accessDate);
}

if (item.dateAdded) {
item.dateAdded = moment(new Date(item.dateAdded));
item.dateAdded = moment(item.dateAdded);
}

if (item.dateModified) {
item.dateModified = moment(new Date(item.dateModified));
item.dateModified = moment(item.dateModified);
}

try {
item.date = await getIssueDateFromCiteKey(item.citekey, database);
} catch (e) {
return false;
}
} catch {}

try {
item.bibliography = await getBibFromCiteKey(item.citekey, database);
} catch (e) {
return false;
} catch {
item.bibliography = "Error generating bibliography";
}

item.notes?.forEach(processNote);
Expand All @@ -121,8 +119,10 @@ export async function exportToMarkdown(
}

const citeKeys: string[] = await getCiteKeys(database);
let itemData: any;

if (!citeKeys.length) return false;

let itemData: any;
try {
itemData = await getItemJSONFromCiteKeys(citeKeys, database);
} catch (e) {
Expand Down Expand Up @@ -301,8 +301,10 @@ export async function exportToMarkdown(

export async function pdfDebugPrompt(settings: ZoteroConnectorSettings) {
const citeKeys: string[] = await getCiteKeys(settings.database);
let itemData: any;

if (!citeKeys.length) return null;

let itemData: any;
try {
itemData = await getItemJSONFromCiteKeys(citeKeys, settings.database);
} catch (e) {
Expand Down
12 changes: 10 additions & 2 deletions src/bbt/exportNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,23 @@ export async function newFile(
const target = !folder.endsWith("/") ? folder + "/" : folder;

if (!app.vault.getAbstractFileByPath(target)) {
await app.vault.createFolder(target);
try {
await app.vault.createFolder(target);
} catch (e) {
console.error(e);
new Notice(
`Error creating folder "${target}": ${e.message}`,
10000
);
}
}

try {
return await app.vault.create(`${target}${citeKey}.md`, content);
} catch (e) {
console.error(e);
new Notice(
`Error creating file ${target}${citeKey}.md: ${e.message}`,
`Error creating file "${target}${citeKey}.md": ${e.message}`,
10000
);
return null;
Expand Down
6 changes: 1 addition & 5 deletions src/bbt/extractAnnotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function extractAnnotations(input: string, params: ExtractParams) {

modal.close();

if (result.stderr) {
if (result.stderr && !result.stderr.includes("warning")) {
new Notice(`Error processing PDF: ${result.stderr}`, 10000);
throw new Error(result.stderr);
}
Expand All @@ -69,7 +69,3 @@ export async function extractAnnotations(input: string, params: ExtractParams) {
throw e;
}
}

function escapePath(input: string): string {
throw new Error("Function not implemented.");
}
1 change: 1 addition & 0 deletions src/bbt/jsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export async function getBibFromCiteKeys(
return htmlToMarkdown(JSON.parse(res).result);
} catch (e) {
console.error(e);
console.error(`Response from BBT: ${res}`);
new Notice(
`Error converting formatted bibliography to markdown: ${e.message}`,
10000
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1981,10 +1981,10 @@ obsidian-utils@^0.10.0:
node-fetch "^2.6.1"
which "^2.0.2"

obsidian@0.13.30:
version "0.13.30"
resolved "https://registry.yarnpkg.com/obsidian/-/obsidian-0.13.30.tgz#215697376c5c0059d7661df400440d97224d3f95"
integrity sha512-uAOrIyeHE9qYzg1Qjfpy/qlyLUFX9oyKWeHYO8NVDoI+pm5VUTMe7XWcsXPwb9iVsVmggVJcdV15Vqm9bljhxQ==
obsidian@^0.14.4:
version "0.14.4"
resolved "https://registry.yarnpkg.com/obsidian/-/obsidian-0.14.4.tgz#c4602d5a48482513a29242576074f82dec510cdc"
integrity sha512-lWU1w/NlXuswAa35ImAbyElLOOno1C13KCZE19kqtcxSPIquvi5Fzy8zBbQ6ZRZZvkvftUiToCp5lcZFRMGxrQ==
dependencies:
"@codemirror/state" "^0.19.6"
"@codemirror/view" "^0.19.31"
Expand Down

0 comments on commit 37e6a96

Please sign in to comment.