mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
fix(apis/websocket): improve logging and error handling
This commit is contained in:
@@ -16,19 +16,18 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
|
||||
|
||||
const nextSeq = client.currentSequence++
|
||||
|
||||
logger.debug(`Client ${client.id} requested to parse image from URL:`, imageUrl)
|
||||
logger.debug(`Queue currently has ${queue.remaining}/${config.ocrConcurrentQueues} items in it`)
|
||||
logger.debug(`Client ${client.id} requested to parse image from URL (${nextSeq})`, imageUrl)
|
||||
|
||||
if (queue.remaining < config.ocrConcurrentQueues) queue.shift()
|
||||
await queue.wait()
|
||||
|
||||
try {
|
||||
logger.debug(`Recognizing image from URL for client ${client.id}`)
|
||||
logger.debug(`Process queued (${nextSeq}), queue has ${queue.remaining} items`)
|
||||
|
||||
try {
|
||||
const { data, jobId } = await tesseract.recognize(imageUrl)
|
||||
|
||||
logger.debug(`Recognized image from URL for client ${client.id} (job ${jobId}):`, data.text)
|
||||
await client.send(
|
||||
logger.debug(`Image parsed (job ${jobId}) (${nextSeq}):`, data.text)
|
||||
client.send(
|
||||
{
|
||||
op: ServerOperation.ParsedImage,
|
||||
d: {
|
||||
@@ -37,20 +36,20 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
} catch {
|
||||
logger.error(`Failed to parse image from URL for client ${client.id}:`, imageUrl)
|
||||
await client.send(
|
||||
{
|
||||
op: ServerOperation.ParseImageFailed,
|
||||
d: null,
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
} catch (e) {
|
||||
if (!client.disconnected)
|
||||
client.send(
|
||||
{
|
||||
op: ServerOperation.ParseImageFailed,
|
||||
d: null,
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
else logger.warn(`Client disconnected before the failed packet could be sent (${nextSeq})`)
|
||||
logger.error(`Failed to parse image (${nextSeq}):`, e)
|
||||
} finally {
|
||||
queue.shift()
|
||||
logger.debug(
|
||||
`Finished processing image from URL for client ${client.id}, queue has ${queue.remaining}/${config.ocrConcurrentQueues} remaining items in it`,
|
||||
)
|
||||
logger.debug(`Finished parsing image (${nextSeq}), queue has ${queue.remaining} items`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { type ClientOperation, ServerOperation } from '@revanced/bot-shared'
|
||||
|
||||
import { inspect as inspectObject } from 'util'
|
||||
|
||||
import type { EventHandler } from '.'
|
||||
|
||||
const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (packet, { wit, logger }) => {
|
||||
@@ -19,7 +17,7 @@ const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (pa
|
||||
const { intents } = await wit.message(actualText)
|
||||
const intentsWithoutIds = intents.map(({ id, ...rest }) => rest)
|
||||
|
||||
await client.send(
|
||||
client.send(
|
||||
{
|
||||
op: ServerOperation.ParsedText,
|
||||
d: {
|
||||
@@ -29,16 +27,16 @@ const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (pa
|
||||
nextSeq,
|
||||
)
|
||||
} catch (e) {
|
||||
await client.send(
|
||||
{
|
||||
op: ServerOperation.ParseTextFailed,
|
||||
d: null,
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
|
||||
if (e instanceof Error) logger.error(e.stack ?? e.message)
|
||||
else logger.error(inspectObject(e))
|
||||
if (!client.disconnected)
|
||||
client.send(
|
||||
{
|
||||
op: ServerOperation.ParseTextFailed,
|
||||
d: null,
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
else logger.warn(`Client disconnected before the failed packet could be sent (${nextSeq})`)
|
||||
logger.error(`Failed to parse text (${nextSeq}):`, e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { type ClientOperation, ServerOperation } from '@revanced/bot-shared'
|
||||
|
||||
import { inspect as inspectObject } from 'util'
|
||||
|
||||
import type { EventHandler } from '.'
|
||||
|
||||
const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = async (packet, { wit, logger }) => {
|
||||
@@ -13,11 +11,11 @@ const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = asy
|
||||
const nextSeq = client.currentSequence++
|
||||
const actualText = text.slice(0, 279)
|
||||
|
||||
logger.debug(`Client ${client.id} requested to train label ${label} with:`, actualText)
|
||||
logger.debug(`${client.id} requested to train label ${label} (${nextSeq}) with:`, actualText)
|
||||
|
||||
try {
|
||||
await wit.train(actualText, label)
|
||||
await client.send(
|
||||
client.send(
|
||||
{
|
||||
op: ServerOperation.TrainedMessage,
|
||||
d: null,
|
||||
@@ -25,18 +23,18 @@ const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = asy
|
||||
nextSeq,
|
||||
)
|
||||
|
||||
logger.debug(`Trained label ${label} with:`, actualText)
|
||||
logger.debug(`Trained label (${nextSeq})`)
|
||||
} catch (e) {
|
||||
await client.send(
|
||||
{
|
||||
op: ServerOperation.TrainMessageFailed,
|
||||
d: null,
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
|
||||
if (e instanceof Error) logger.error(e.stack ?? e.message)
|
||||
else logger.error(inspectObject(e))
|
||||
if (!client.disconnected)
|
||||
client.send(
|
||||
{
|
||||
op: ServerOperation.TrainMessageFailed,
|
||||
d: null,
|
||||
},
|
||||
nextSeq,
|
||||
)
|
||||
else logger.warn(`Client ${client.id} disconnected before the failed packet could be sent`)
|
||||
logger.error(`Failed to train (${nextSeq})`, e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user