Skip to content

Commit

Permalink
Error logging (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored Jun 9, 2021
1 parent cea6a12 commit d680771
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 46 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "notetweet",
"name": "NoteTweet🐦",
"version": "0.4.0",
"version": "0.4.1",
"minAppVersion": "0.9.12",
"description": "This plugin allows you to post tweets directly from Obsidian.",
"author": "Christian B. B. Houmann",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notetweet",
"version": "0.4.0",
"version": "0.4.1",
"description": "Post tweets from Obsidian",
"main": "src/main.js",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions src/ErrorModule/ILogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ILogger {
logError(msg: string): void;

logWarning(msg: string): void;

logMessage(msg: string): void;
}
32 changes: 32 additions & 0 deletions src/ErrorModule/consoleErrorLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {ErrorLevel} from "./errorLevel";
import {NoteTweetError} from "./noteTweetError";
import {NoteTweetLogger} from "./noteTweetLogger";

export class ConsoleErrorLogger extends NoteTweetLogger {
public ErrorLog: NoteTweetError[] = [];

public logError(errorMsg: string) {
const error = this.getNoteTweetError(errorMsg, ErrorLevel.Error);
this.addMessageToErrorLog(error);

console.error(this.formatOutputString(error));
}

public logWarning(warningMsg: string) {
const warning = this.getNoteTweetError(warningMsg, ErrorLevel.Warning);
this.addMessageToErrorLog(warning);

console.warn(this.formatOutputString(warning));
}

public logMessage(logMsg: string) {
const log = this.getNoteTweetError(logMsg, ErrorLevel.Log);
this.addMessageToErrorLog(log);

console.log(this.formatOutputString(log));
}

private addMessageToErrorLog(error: NoteTweetError): void {
this.ErrorLog.push(error);
}
}
1 change: 1 addition & 0 deletions src/ErrorModule/errorLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export enum ErrorLevel { Error = "ERROR", Warning = "WARNING", Log = "LOG"}
20 changes: 20 additions & 0 deletions src/ErrorModule/guiLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {NoteTweetLogger} from "./noteTweetLogger";
import {TweetErrorModal} from "../Modals/TweetErrorModal";
import NoteTweet from "../main";
import {Notice} from "obsidian";

export class GuiLogger extends NoteTweetLogger {
constructor(private plugin: NoteTweet) {
super();
}

logError(msg: string): void {
new TweetErrorModal(this.plugin.app, msg).open();
}

logWarning(msg: string): void {
new Notice(msg);
}

logMessage(msg: string): void {}
}
25 changes: 25 additions & 0 deletions src/ErrorModule/logManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {ILogger} from "./ILogger";

class LogManager {
public static loggers: ILogger[] = [];

public register(logger: ILogger): LogManager {
LogManager.loggers.push(logger);

return this;
}

logError(message: string) {
LogManager.loggers.forEach(logger => logger.logError(message));
}

logWarning(message: string) {
LogManager.loggers.forEach(logger => logger.logError(message));
}

logMessage(message: string) {
LogManager.loggers.forEach(logger => logger.logMessage(message));
}
}

export const log = new LogManager();
7 changes: 7 additions & 0 deletions src/ErrorModule/noteTweetError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ErrorLevel} from "./errorLevel";

export interface NoteTweetError {
message: string,
level: ErrorLevel,
time: number
}
19 changes: 19 additions & 0 deletions src/ErrorModule/noteTweetLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {ILogger} from "./ILogger";
import {NoteTweetError} from "./noteTweetError";
import {ErrorLevel} from "./errorLevel";

export abstract class NoteTweetLogger implements ILogger {
abstract logError(msg: string): void;

abstract logMessage(msg: string): void;

abstract logWarning(msg: string): void;

protected formatOutputString(error: NoteTweetError): string {
return `NoteTweet: (${error.level}) ${error.message}`;
}

protected getNoteTweetError(message: string, level: ErrorLevel): NoteTweetError {
return {message, level, time: Date.now()};
}
}
41 changes: 9 additions & 32 deletions src/Modals/PostTweetModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { App, Modal, Notice } from "obsidian";
import { TwitterHandler } from "../TwitterHandler";
import { TweetsPostedModal } from "./TweetsPostedModal/TweetsPostedModal";
import { TweetErrorModal } from "./TweetErrorModal";
import {log} from "../ErrorModule/logManager";

export class PostTweetModal extends Modal {
private readonly twitterHandler: TwitterHandler;
Expand Down Expand Up @@ -42,7 +43,7 @@ export class PostTweetModal extends Modal {

this.createTweetButton(contentEl);
} catch (e) {
new Notice(e);
log.logWarning(e);
this.close();
return;
}
Expand Down Expand Up @@ -78,7 +79,7 @@ export class PostTweetModal extends Modal {

tempTextarea.style.height = tempTextarea.scrollHeight + "px";
} catch (e) {
new Notice(e);
log.logWarning(e);
return;
}
});
Expand Down Expand Up @@ -192,7 +193,7 @@ export class PostTweetModal extends Modal {
try {
this.createTextarea(textZone);
} catch (e) {
new Notice(e);
log.logWarning(e);
return;
}
}
Expand All @@ -202,7 +203,7 @@ export class PostTweetModal extends Modal {
try {
this.createTextarea(textZone);
} catch (e) {
new Notice(e);
log.logWarning(e);
return;
}
}
Expand Down Expand Up @@ -320,7 +321,7 @@ export class PostTweetModal extends Modal {
(txt) => txt.length > this.MAX_TWEET_LENGTH || txt == ""
) != null
) {
new Notice("At least one of your tweets is too long or empty.");
log.logWarning("At least one of your tweets is too long or empty.");
return;
}

Expand All @@ -333,7 +334,7 @@ export class PostTweetModal extends Modal {
);
postedModal.open();
} catch (e) {
new TweetErrorModal(this.app, e.data || e).open();
log.logError(`unable to post tweet. ${e}`);
}

this.close();
Expand All @@ -354,7 +355,7 @@ export class PostTweetModal extends Modal {

return { tweet: insertedTweet, index: insertAboveIndex };
} catch (e) {
new Notice(e);
log.logWarning(e);
return;
}
}
Expand All @@ -374,7 +375,7 @@ export class PostTweetModal extends Modal {

return insertedTextarea;
} catch (e) {
new Notice(e);
log.logWarning(e);
}
}

Expand All @@ -387,28 +388,4 @@ export class PostTweetModal extends Modal {
this.textAreas[insertedIndex].value = "";
this.textAreas[insertedIndex].focus();
}

/* private insertTweetBelowWithText(textarea: HTMLTextAreaElement, textZone: HTMLDivElement, insertText: string){
// Insert tweet, assign to var. Pass that var in again.
// It'll be reverse order if I don't insert below each one. For inserting above, you can just insert as you normally would.
if (insertText.length > this.MAX_TWEET_LENGTH) {
let sliced = this.textInputHandler(insertText); // First, make sure the text is sized correctly.
let tweet: HTMLTextAreaElement = textarea;
let tweetIndex = this.insertTweetBelow(tweet, textZone);
tweet = this.textAreas[tweetIndex];
this.insertTweetBelowWithText(tweet, textZone, sliced.slice(1).join());
// sliced.forEach(chunk => {
// console.log("!!!!")
// let x = this.insertTweetBelow(tweet, textZone);
// tweet = x.insertedTweet;
// tweet.value = chunk;
// });
}
else {
let {insertedTweet, insertedIndex} = this.insertTweetBelow(textarea, textZone);
this.textAreas[insertedIndex].value = insertText;
}
}*/
}
2 changes: 1 addition & 1 deletion src/Modals/TweetErrorModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class TweetErrorModal extends Modal {
onOpen() {
let { contentEl } = this;

contentEl.setText(`Post failed: ${this.errorMessage}`);
contentEl.setText(`NoteTweet: ${this.errorMessage}`);
}

onClose() {
Expand Down
6 changes: 5 additions & 1 deletion src/TwitterHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StatusesUpdate, TwitterClient } from "twitter-api-client";
import NoteTweet from "./main";
import {log} from "./ErrorModule/logManager";

export class TwitterHandler {
private twitterClient: TwitterClient;
Expand Down Expand Up @@ -76,6 +77,8 @@ export class TwitterHandler {
if (media_id) {
media_ids.push(media_id.media_id_string);
processedTweet = processedTweet.replace(this.IMAGE_REGEX, "");
} else {
log.logWarning(`image '${fileName}' found but could not upload it to Twitter. Data is null/undefined: ${!!media_data}.`);
}
}

Expand All @@ -90,7 +93,8 @@ export class TwitterHandler {
});

return true;
} catch {
} catch(e) {
log.logError(`error in deleting tweets. ${e}`);
return false;
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {TweetsPostedModal} from "./Modals/TweetsPostedModal/TweetsPostedModal";
import {TweetErrorModal} from "./Modals/TweetErrorModal";
import {SecureModeGetPasswordModal} from "./Modals/SecureModeGetPasswordModal/SecureModeGetPasswordModal";
import {PostTweetModal} from "./Modals/PostTweetModal";
import {log} from "./ErrorModule/logManager";
import {ConsoleErrorLogger} from "./ErrorModule/consoleErrorLogger";
import {GuiLogger} from "./ErrorModule/guiLogger";

const WELCOME_MESSAGE: string = "Loading NoteTweet🐦. Thanks for installing.";
const UNLOAD_MESSAGE: string = "Unloaded NoteTweet.";
Expand Down Expand Up @@ -87,9 +90,8 @@ export default class NoteTweet extends Plugin {
});
/*END.DEVCMD*/

this.registerObsidianProtocolHandler("notetweet", params => {
console.log(params);
})
log.register(new ConsoleErrorLogger())
.register(new GuiLogger(this));

this.addSettingTab(new NoteTweetSettingsTab(this.app, this));
}
Expand Down Expand Up @@ -145,7 +147,7 @@ export default class NoteTweet extends Plugin {
try {
threadContent = this.parseThreadFromText(content);
} catch (e) {
new TweetErrorModal(this.app, e).open();
log.logError(`error in parsing thread in file ${file?.name}. ${e}`);
return;
}

Expand All @@ -162,7 +164,7 @@ export default class NoteTweet extends Plugin {
postedTweets.forEach((tweet) => this.appendPostTweetTag(tweet.text));
}
} catch (e) {
new TweetErrorModal(this.app, e.data || e).open();
log.logError(`failed attempted to post tweets. ${e}`);
}
}

Expand Down Expand Up @@ -192,10 +194,10 @@ export default class NoteTweet extends Plugin {
await this.appendPostTweetTag(tweet.text);
}
} catch (e) {
new TweetErrorModal(this.app, e.data || e).open();
log.logError(`failed attempt to post selected. ${e}`);
}
} else {
new TweetErrorModal(this.app, "nothing selected.").open();
log.logWarning(`tried to post selected but nothing was selected.`)
}
}

Expand All @@ -210,11 +212,11 @@ export default class NoteTweet extends Plugin {
modal.waitForClose
.then(async () => {
if (this.twitterHandler.isConnectedToTwitter) await callback();
else new Notice("Could not connect to Twitter");
else log.logWarning("could not connect to Twitter");
})
.catch(() => {
modal.close();
new Notice("Could not connect to Twitter.");
log.logWarning("could not connect to Twitter.");
});
}

Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"0.3.3": "0.9.12",
"0.3.4": "0.12.0",
"0.4.0": "0.12.3"
"0.4.1": "0.12.3"
}

0 comments on commit d680771

Please sign in to comment.