🐛 fixed a bug that results were not returned in case of error

This commit is contained in:
kawamataryo 2023-08-27 09:34:51 +09:00
parent f451e67a04
commit 4e6969a254
7 changed files with 95 additions and 35 deletions

View File

@ -5,9 +5,17 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { session, subjectDid } = req.body const { session, subjectDid } = req.body
const client = BskyClient.createAgentFromSession(session) const client = BskyClient.createAgentFromSession(session)
try {
res.send({ res.send({
result: await client.block(subjectDid) result: await client.block(subjectDid)
}) })
} catch (e) {
res.send({
error: {
message: e.message,
}
})
}
} }
export default handler export default handler

View File

@ -5,9 +5,17 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { session, subjectDid } = req.body const { session, subjectDid } = req.body
const client = BskyClient.createAgentFromSession(session) const client = BskyClient.createAgentFromSession(session)
try {
res.send({ res.send({
result: await client.follow(subjectDid) result: await client.follow(subjectDid)
}) })
} catch (e) {
res.send({
error: {
message: e.message,
}
})
}
} }
export default handler export default handler

View File

@ -4,6 +4,7 @@ import { BskyClient } from "../../lib/bskyClient";
const handler: PlasmoMessaging.MessageHandler = async (req, res) => { const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { identifier, password } = req.body const { identifier, password } = req.body
try {
const agent = await BskyClient.createAgent({ const agent = await BskyClient.createAgent({
identifier, identifier,
password, password,
@ -12,6 +13,13 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
res.send({ res.send({
session: agent.session, session: agent.session,
}) })
} catch (e) {
res.send({
error: {
message: e.message,
}
})
}
} }
export default handler export default handler

View File

@ -5,12 +5,20 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { session, term, limit } = req.body const { session, term, limit } = req.body
const client = BskyClient.createAgentFromSession(session) const client = BskyClient.createAgentFromSession(session)
try {
res.send({ res.send({
actors: await client.searchUser({ actors: await client.searchUser({
term, term,
limit, limit,
}) })
}) })
} catch (e) {
res.send({
error: {
message: e.message,
}
})
}
} }
export default handler export default handler

View File

@ -5,9 +5,17 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { session, blockUri } = req.body const { session, blockUri } = req.body
const client = BskyClient.createAgentFromSession(session) const client = BskyClient.createAgentFromSession(session)
try {
res.send({ res.send({
result: await client.unblock(blockUri) result: await client.unblock(blockUri)
}) })
} catch (e) {
res.send({
error: {
message: e.message,
}
})
}
} }
export default handler export default handler

View File

@ -5,9 +5,17 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { session, followUri } = req.body const { session, followUri } = req.body
const client = BskyClient.createAgentFromSession(session) const client = BskyClient.createAgentFromSession(session)
try {
res.send({ res.send({
result: await client.unfollow(followUri) result: await client.unfollow(followUri)
}) })
} catch (e) {
res.send({
error: {
message: e.message,
}
})
}
} }
export default handler export default handler

View File

@ -16,13 +16,15 @@ export class BskyServiceWorkerClient {
password, password,
}: BskyLoginParams): Promise<BskyServiceWorkerClient> { }: BskyLoginParams): Promise<BskyServiceWorkerClient> {
const client = new BskyServiceWorkerClient(); const client = new BskyServiceWorkerClient();
const { session } = await sendToBackground({ const { session, error } = await sendToBackground({
name: "login", name: "login",
body: { body: {
identifier, identifier,
password, password,
} }
}) })
if(error) throw new Error(error.message)
client.session = session client.session = session
return client; return client;
} }
@ -34,7 +36,7 @@ export class BskyServiceWorkerClient {
term: string; term: string;
limit: number; limit: number;
}) => { }) => {
const { actors } = await sendToBackground({ const { actors, error } = await sendToBackground({
name: "searchUser", name: "searchUser",
body: { body: {
session: this.session, session: this.session,
@ -42,51 +44,61 @@ export class BskyServiceWorkerClient {
limit, limit,
} }
}) })
if(error) throw new Error(error.message)
return actors; return actors;
}; };
public follow = async (subjectDid: string) => { public follow = async (subjectDid: string) => {
const { result } = await sendToBackground({ const { result, error } = await sendToBackground({
name: "follow", name: "follow",
body: { body: {
session: this.session, session: this.session,
subjectDid subjectDid
} }
}) })
if(error) throw new Error(error.message)
return result; return result;
} }
public unfollow = async (followUri: string) => { public unfollow = async (followUri: string) => {
const { result } = await sendToBackground({ const { result, error } = await sendToBackground({
name: "unfollow", name: "unfollow",
body: { body: {
session: this.session, session: this.session,
followUri followUri
} }
}) })
if(error) throw new Error(error.message)
return result; return result;
} }
public block = async (subjectDid: string) => { public block = async (subjectDid: string) => {
const { result } = await sendToBackground({ const { result, error } = await sendToBackground({
name: "block", name: "block",
body: { body: {
session: this.session, session: this.session,
subjectDid subjectDid
} }
}) })
if(error) throw new Error(error.message)
return result; return result;
} }
public unblock = async (blockUri: string) => { public unblock = async (blockUri: string) => {
// TODO: unblock is not working. Need to fix it. // TODO: unblock is not working. Need to fix it.
const { result } = await sendToBackground({ const { result, error } = await sendToBackground({
name: "unblock", name: "unblock",
body: { body: {
session: this.session, session: this.session,
blockUri blockUri
} }
}) })
if(error) throw new Error(error.message)
return result; return result;
} }
} }