Skip to content

Commit

Permalink
Merge pull request #19 from ubq-testing/gh-storage
Browse files Browse the repository at this point in the history
additional error handling
  • Loading branch information
Keyrxng authored Oct 28, 2024
2 parents 19ddc4d + 8468ef1 commit 224d9d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
21 changes: 18 additions & 3 deletions src/bot/helpers/grammy-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,39 @@ interface Dependencies {
}

interface ExtendedContextFlavor extends Dependencies {
adapters: ReturnType<typeof createAdapters>;
adapters?: ReturnType<typeof createAdapters>;
}

export type GrammyContext = ParseModeFlavor<HydrateFlavor<DefaultContext & ExtendedContextFlavor & SessionFlavor<SessionData> & AutoChatActionFlavor>>;

export async function createContextConstructor({ logger, config, octokit }: Dependencies) {
const adapters = createAdapters(await PluginContext.getInstance().getContext());
let adapters: ReturnType<typeof createAdapters> | undefined;

try {
adapters = createAdapters(await PluginContext.getInstance().getContext());
} catch (er) {
logger.error("createAdapters in Grammy Context failed", { er });
}

if (!adapters) {
throw new Error("Adapters not initialized");
}

return class extends DefaultContext implements ExtendedContextFlavor {
logger: Logger;
adapters = adapters;
octokit: RestOctokitFromApp = octokit;
config: UbiquityOsContext["env"];
adapters: ReturnType<typeof createAdapters> | undefined = adapters;

constructor(update: GrammyTelegramUpdate, api: Api, me: UserFromGetMe) {
super(update, api, me);
this.logger = logger;
this.config = config;

if (!this.adapters) {
throw new Error("Adapters not initialized");
}

/**
* We'll need to add handling to detect forks and in such cases
* we'll need to handle the storage differently.
Expand Down
1 change: 0 additions & 1 deletion src/handlers/workflow-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function proxyWorkflowCallbacks(context: Context): ProxyCallbacks {
for (const r of res) {
if (r.status !== 200) {
await bubbleUpErrorComment(context, new Error(r.reason));
await exit(1);
}
}
await exit(0);
Expand Down
21 changes: 12 additions & 9 deletions src/types/plugin-context-single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export class PluginContext {
public readonly inputs: PluginInputs,
public _env: Env
) {
// this will fallback to defaults if it's a telegram bot command
this._config = this.inputs.settings;
}

get env() {
return Value.Decode(envValidator.schema, Value.Default(envValidator.schema, this._env));
}

set env(env: Env) {
this._env = env;
}
Expand Down Expand Up @@ -82,18 +84,19 @@ export class PluginContext {
* This can be used with events from both Telegram and GitHub, this token comes from
* the worker's environment variables i.e the Storage App.
*/
async getTelegramEventOctokit(): Promise<RestOctokitFromApp> {
async getTelegramEventOctokit(): Promise<RestOctokitFromApp | null> {
let octokit: RestOctokitFromApp | null = null;

await this.getApp().eachInstallation(async (installation) => {
if (installation.installation.account?.login === this.config.storageOwner) {
octokit = installation.octokit;
}
});

if (!octokit) {
throw new Error("Octokit could not be initialized");
try {
await this.getApp().eachInstallation((installation) => {
if (installation.installation.account?.login.toLowerCase() === this.config.storageOwner.toLowerCase()) {
octokit = installation.octokit;
}
});
} catch (er) {
logger.error("Error initializing octokit in getTelegramEventOctokit", { er });
}

return octokit;
}

Expand Down
13 changes: 12 additions & 1 deletion src/types/telegram-bot-single.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Octokit } from "octokit";
import { Env } from ".";
import { Bot, createBot } from "../bot";
import { createServer } from "../server";
Expand All @@ -21,7 +22,17 @@ export class TelegramBotSingleton {
},
} = env;

const octokit = await PluginContext.getInstance().getTelegramEventOctokit();
let octokit: Octokit | null = null;

try {
octokit = await PluginContext.getInstance().getTelegramEventOctokit();
} catch (er) {
logger.error("Error initializing octokit in TelegramBotSingleton", { er });
}

if (!octokit) {
throw new Error("Octokit not initialized");
}

if (!TelegramBotSingleton._instance) {
TelegramBotSingleton._instance = new TelegramBotSingleton();
Expand Down

0 comments on commit 224d9d0

Please sign in to comment.