replace templater error with my own

This commit is contained in:
ransurf 2023-12-12 18:02:03 -08:00
parent 6531be9aa8
commit fdacd20f38
3 changed files with 14 additions and 64 deletions

View File

@ -1,37 +0,0 @@
import { log_error } from './log';
export class TemplaterError extends Error {
constructor(
msg: string,
public console_msg?: string,
) {
super(msg);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
export async function errorWrapper<T>(
fn: () => Promise<T>,
msg: string,
): Promise<T> {
try {
return await fn();
} catch (e) {
if (!(e instanceof TemplaterError)) {
log_error(new TemplaterError(msg, e.message));
} else {
log_error(e);
}
return null as unknown as T;
}
}
export function errorWrapperSync<T>(fn: () => T, msg: string): T {
try {
return fn();
} catch (e) {
log_error(new TemplaterError(msg, e.message));
return null as unknown as T;
}
}

View File

@ -1,22 +0,0 @@
import { Notice } from 'obsidian';
import { TemplaterError } from './error';
export function log_update(msg: string): void {
const notice = new Notice('', 15000);
// TODO: Find better way for this
// @ts-ignore
notice.noticeEl.innerHTML = `<b>obsidian-agents update</b>:<br/>${msg}`;
}
export function log_error(e: Error | TemplaterError): void {
const notice = new Notice('', 8000);
if (e instanceof TemplaterError && e.console_msg) {
// TODO: Find a better way for this
// @ts-ignore
notice.noticeEl.innerHTML = `<b>obsidian-agents Error</b>:<br/>${e.message}<br/>Check console for more information`;
console.error(`obsidian-agents Error:`, e.message, '\n', e.console_msg);
} else {
// @ts-ignore
notice.noticeEl.innerHTML = `<b>obsidian-agents Error</b>:<br/>${e.message}`;
}
}

View File

@ -1,4 +1,3 @@
import { TemplaterError } from './error';
import { import {
App, App,
normalizePath, normalizePath,
@ -7,6 +6,7 @@ import {
TFolder, TFolder,
Vault, Vault,
} from 'obsidian'; } from 'obsidian';
import { createNotice } from './Logs';
export function escape_RegExp(str: string): string { export function escape_RegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
@ -25,10 +25,14 @@ export function resolve_tfolder(folder_str: string): TFolder {
const folder = app.vault.getAbstractFileByPath(folder_str); const folder = app.vault.getAbstractFileByPath(folder_str);
if (!folder) { if (!folder) {
throw new TemplaterError(`Folder "${folder_str}" doesn't exist`); const message = `Folder "${folder_str}" doesn't exist`;
createNotice(message);
throw new Error(message);
} }
if (!(folder instanceof TFolder)) { if (!(folder instanceof TFolder)) {
throw new TemplaterError(`${folder_str} is a file, not a folder`); const message = `${folder_str} is a file, not a folder`;
createNotice(message);
throw new Error(message);
} }
return folder; return folder;
@ -39,10 +43,14 @@ export function resolve_tfile(file_str: string): TFile {
const file = app.vault.getAbstractFileByPath(file_str); const file = app.vault.getAbstractFileByPath(file_str);
if (!file) { if (!file) {
throw new TemplaterError(`File "${file_str}" doesn't exist`); const message = `File "${file_str}" doesn't exist`;
createNotice(message);
throw new Error(message);
} }
if (!(file instanceof TFile)) { if (!(file instanceof TFile)) {
throw new TemplaterError(`${file_str} is a folder, not a file`); const message = `${file_str} is a folder, not a file`;
createNotice(message);
throw new Error(message);
} }
return file; return file;
@ -88,3 +96,4 @@ export function arraymove<T>(
export function get_active_file(app: App) { export function get_active_file(app: App) {
return app.workspace.activeEditor?.file ?? app.workspace.getActiveFile(); return app.workspace.activeEditor?.file ?? app.workspace.getActiveFile();
} }