From 4e6969a2549210fe757e8ea0b686ebefa6ae7542 Mon Sep 17 00:00:00 2001 From: kawamataryo Date: Sun, 27 Aug 2023 09:34:51 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fixed=20a=20bug=20that=20results?= =?UTF-8?q?=20were=20not=20returned=20in=20case=20of=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background/messages/block.ts | 16 ++++++++++++---- src/background/messages/follow.ts | 16 ++++++++++++---- src/background/messages/login.ts | 22 +++++++++++++++------- src/background/messages/searchUser.ts | 20 ++++++++++++++------ src/background/messages/unblock.ts | 16 ++++++++++++---- src/background/messages/unfollow.ts | 16 ++++++++++++---- src/lib/bskyServiceWorkerClient.ts | 24 ++++++++++++++++++------ 7 files changed, 95 insertions(+), 35 deletions(-) diff --git a/src/background/messages/block.ts b/src/background/messages/block.ts index 9294b03..05ae698 100644 --- a/src/background/messages/block.ts +++ b/src/background/messages/block.ts @@ -2,12 +2,20 @@ import type { PlasmoMessaging } from "@plasmohq/messaging" import { BskyClient } from "~lib/bskyClient"; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { - const { session, subjectDid } = req.body + const { session, subjectDid } = req.body const client = BskyClient.createAgentFromSession(session) - res.send({ - result: await client.block(subjectDid) - }) + try { + res.send({ + result: await client.block(subjectDid) + }) + } catch (e) { + res.send({ + error: { + message: e.message, + } + }) + } } export default handler diff --git a/src/background/messages/follow.ts b/src/background/messages/follow.ts index cd291e1..17b8644 100644 --- a/src/background/messages/follow.ts +++ b/src/background/messages/follow.ts @@ -2,12 +2,20 @@ import type { PlasmoMessaging } from "@plasmohq/messaging" import { BskyClient } from "~lib/bskyClient"; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { - const { session, subjectDid } = req.body + const { session, subjectDid } = req.body const client = BskyClient.createAgentFromSession(session) - res.send({ - result: await client.follow(subjectDid) - }) + try { + res.send({ + result: await client.follow(subjectDid) + }) + } catch (e) { + res.send({ + error: { + message: e.message, + } + }) + } } export default handler diff --git a/src/background/messages/login.ts b/src/background/messages/login.ts index 3833700..dc5c5d3 100644 --- a/src/background/messages/login.ts +++ b/src/background/messages/login.ts @@ -4,14 +4,22 @@ import { BskyClient } from "../../lib/bskyClient"; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { const { identifier, password } = req.body - const agent = await BskyClient.createAgent({ - identifier, - password, - }) + try { + const agent = await BskyClient.createAgent({ + identifier, + password, + }) - res.send({ - session: agent.session, - }) + res.send({ + session: agent.session, + }) + } catch (e) { + res.send({ + error: { + message: e.message, + } + }) + } } export default handler diff --git a/src/background/messages/searchUser.ts b/src/background/messages/searchUser.ts index 40315b0..358787a 100644 --- a/src/background/messages/searchUser.ts +++ b/src/background/messages/searchUser.ts @@ -2,15 +2,23 @@ import type { PlasmoMessaging } from "@plasmohq/messaging" import { BskyClient } from "~lib/bskyClient"; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { - const { session, term, limit } = req.body + const { session, term, limit } = req.body const client = BskyClient.createAgentFromSession(session) - res.send({ - actors: await client.searchUser({ - term, - limit, + try { + res.send({ + actors: await client.searchUser({ + term, + limit, + }) }) - }) + } catch (e) { + res.send({ + error: { + message: e.message, + } + }) + } } export default handler diff --git a/src/background/messages/unblock.ts b/src/background/messages/unblock.ts index 166176a..ff28723 100644 --- a/src/background/messages/unblock.ts +++ b/src/background/messages/unblock.ts @@ -2,12 +2,20 @@ import type { PlasmoMessaging } from "@plasmohq/messaging" import { BskyClient } from "~lib/bskyClient"; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { - const { session, blockUri } = req.body + const { session, blockUri } = req.body const client = BskyClient.createAgentFromSession(session) - res.send({ - result: await client.unblock(blockUri) - }) + try { + res.send({ + result: await client.unblock(blockUri) + }) + } catch (e) { + res.send({ + error: { + message: e.message, + } + }) + } } export default handler diff --git a/src/background/messages/unfollow.ts b/src/background/messages/unfollow.ts index b876ed9..a0946f6 100644 --- a/src/background/messages/unfollow.ts +++ b/src/background/messages/unfollow.ts @@ -2,12 +2,20 @@ import type { PlasmoMessaging } from "@plasmohq/messaging" import { BskyClient } from "~lib/bskyClient"; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { - const { session, followUri } = req.body + const { session, followUri } = req.body const client = BskyClient.createAgentFromSession(session) - res.send({ - result: await client.unfollow(followUri) - }) + try { + res.send({ + result: await client.unfollow(followUri) + }) + } catch (e) { + res.send({ + error: { + message: e.message, + } + }) + } } export default handler diff --git a/src/lib/bskyServiceWorkerClient.ts b/src/lib/bskyServiceWorkerClient.ts index b825982..8957b0e 100644 --- a/src/lib/bskyServiceWorkerClient.ts +++ b/src/lib/bskyServiceWorkerClient.ts @@ -16,13 +16,15 @@ export class BskyServiceWorkerClient { password, }: BskyLoginParams): Promise { const client = new BskyServiceWorkerClient(); - const { session } = await sendToBackground({ + const { session, error } = await sendToBackground({ name: "login", body: { identifier, password, } }) + if(error) throw new Error(error.message) + client.session = session return client; } @@ -34,7 +36,7 @@ export class BskyServiceWorkerClient { term: string; limit: number; }) => { - const { actors } = await sendToBackground({ + const { actors, error } = await sendToBackground({ name: "searchUser", body: { session: this.session, @@ -42,51 +44,61 @@ export class BskyServiceWorkerClient { limit, } }) + if(error) throw new Error(error.message) + return actors; }; public follow = async (subjectDid: string) => { - const { result } = await sendToBackground({ + const { result, error } = await sendToBackground({ name: "follow", body: { session: this.session, subjectDid } }) + if(error) throw new Error(error.message) + return result; } public unfollow = async (followUri: string) => { - const { result } = await sendToBackground({ + const { result, error } = await sendToBackground({ name: "unfollow", body: { session: this.session, followUri } }) + if(error) throw new Error(error.message) + return result; } public block = async (subjectDid: string) => { - const { result } = await sendToBackground({ + const { result, error } = await sendToBackground({ name: "block", body: { session: this.session, subjectDid } }) + if(error) throw new Error(error.message) + return result; } public unblock = async (blockUri: string) => { // TODO: unblock is not working. Need to fix it. - const { result } = await sendToBackground({ + const { result, error } = await sendToBackground({ name: "unblock", body: { session: this.session, blockUri } }) + if(error) throw new Error(error.message) + return result; } }