Skip to content

Commit

Permalink
fix: error handling in content scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Jun 13, 2024
1 parent aee11a2 commit 2206ab0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/chrome/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ chrome.notifications.onClicked.addListener(handleNotificationClick)
chrome.notifications.onButtonClicked.addListener(handleNotificationClick)
chrome.storage.onChanged.addListener(handleStorageChange)
chrome.action.onClicked.addListener(openParingPopup)

chrome.runtime.onInstalled.addListener(handleOnInstallExtension)
chrome.management.onEnabled.addListener(handleOnInstallExtension)

createOffscreen()

Expand Down
38 changes: 18 additions & 20 deletions src/chrome/content-script/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ContentScriptMessageHandler } from './message-handler'
import { createMessage } from '../messages/create-message'
import { MessageClient } from '../messages/message-client'
import { ConfirmationMessageError, Message } from '../messages/_types'
import { errAsync, okAsync, ResultAsync } from 'neverthrow'
import { err, errAsync, okAsync, ResultAsync } from 'neverthrow'
import { logger } from 'utils/logger'
import { MessageLifeCycleEvent } from 'chrome/dapp/_types'
import { sendMessage } from 'chrome/helpers/send-message'
Expand Down Expand Up @@ -56,58 +56,56 @@ const messageHandler = MessageClient(
{ logger: appLogger },
)

const handleWalletInteraction = async (
walletInteraction: WalletInteraction,
) => {
const handleWalletInteraction = (walletInteraction: WalletInteraction) =>
sendMessage(createMessage.dAppRequest('contentScript', walletInteraction))
}

const handleExtensionInteraction = async (
const handleExtensionInteraction = (
extensionInteraction: ExtensionInteraction,
) => {
switch (extensionInteraction.discriminator) {
case 'openPopup':
await sendMessage(createMessage.openParingPopup())
break
return sendMessage(createMessage.openParingPopup())

case 'extensionStatus':
await hasConnections().map((hasConnections) => {
sendMessageToDapp(createMessage.extensionStatus(hasConnections))
})
break
return hasConnections().map((hasConnections) =>
sendMessageToDapp(createMessage.extensionStatus(hasConnections)),
)

case 'cancelWalletInteraction':
sendMessage(
return sendMessage(
createMessage.cancelWalletInteraction(
addOriginToCancelInteraction(extensionInteraction),
),
)
break

case 'walletInteraction':
sendMessage(
return sendMessage(
createMessage.walletInteraction({
...extensionInteraction,
interaction: addOriginToWalletInteraction(
extensionInteraction.interaction,
),
}),
)
break

default:
logger.error({
return err({
reason: 'InvalidExtensionRequest',
interaction: extensionInteraction,
})
break
}
}

// incoming messages from dApps
chromeDAppClient.messageListener(
handleWalletInteraction,
handleExtensionInteraction,
(p) =>
handleWalletInteraction(p).mapErr((e) => {
appLogger.error('handleWalletInteraction', e)
}),
(p) =>
handleExtensionInteraction(p).mapErr((e) => {
appLogger.error('handleExtensionInteraction', e)
}),
)

// incoming messages from extension
Expand Down
4 changes: 3 additions & 1 deletion src/chrome/helpers/send-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ResultAsync } from 'neverthrow'

export const sendMessage = (message: Message) =>
ResultAsync.fromPromise(
chrome.runtime.sendMessage(message),
chrome.runtime.id
? chrome.runtime.sendMessage(message)
: Promise.reject({ reason: 'NoChromeRuntimeId' }),
(error) => error as Error,
)
6 changes: 5 additions & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ export const logger = new Logger({
})

logger.attachTransport((logObj) => {
sendMessage(createMessage.log(logObj))
try {
sendMessage(createMessage.log(logObj))
} catch (error) {
console.error('Error sending log message', error)
}
})
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const manifest = defineManifest(async () => {
'scripting',
'notifications',
'contextMenus',
'management',
'idle',
]
const matches = ['https://*/*', 'http://localhost/*', 'http://127.0.0.1/*']
Expand Down

0 comments on commit 2206ab0

Please sign in to comment.