Error when trying to get the result of a background task

I am trying to use background tasks to deal with rate limiting from an external API. I have this function
export async function discordRequest(endpoint, options) {
const task = await api.enqueue(
api.discordRequest,
{ endpoint, method: options.method, body: JSON.stringify(options.body) },
{ retries: { retryCount: 16 } }
)

return await task.result()
}
export async function discordRequest(endpoint, options) {
const task = await api.enqueue(
api.discordRequest,
{ endpoint, method: options.method, body: JSON.stringify(options.body) },
{ retries: { retryCount: 16 } }
)

return await task.result()
}
which enqueues this action
const URL = 'https://discord.com/api/v10/'

export const params = {
endpoint: { type: 'string' },
method: { type: 'string' },
body: { type: 'string' },
}

/** @type { ActionRun } */
export const run = async ({ params, logger }) => {
const { endpoint, method, body } = params

const res = await fetch(
URL + endpoint, {
headers: {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
'Content-Type': 'application/json; charset=UTF-8',
'User-Agent': 'redacted',
},
method,
body
}
)

if (res.status == 429) {
logger.warn({ res }, 'Rate limited')
throw new Error('Rate limited')
}

try {
const data = await res.json()
if (!res.ok) {
throw new Error(JSON.stringify(data))
}
return data
} catch (err) {
if (err instanceof SyntaxError) {
return {}
}
logger.error(err)
}
}
const URL = 'https://discord.com/api/v10/'

export const params = {
endpoint: { type: 'string' },
method: { type: 'string' },
body: { type: 'string' },
}

/** @type { ActionRun } */
export const run = async ({ params, logger }) => {
const { endpoint, method, body } = params

const res = await fetch(
URL + endpoint, {
headers: {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
'Content-Type': 'application/json; charset=UTF-8',
'User-Agent': 'redacted',
},
method,
body
}
)

if (res.status == 429) {
logger.warn({ res }, 'Rate limited')
throw new Error('Rate limited')
}

try {
const data = await res.json()
if (!res.ok) {
throw new Error(JSON.stringify(data))
}
return data
} catch (err) {
if (err instanceof SyntaxError) {
return {}
}
logger.error(err)
}
}
2 Replies
atom
atomOP7mo ago
It mostly works, but occasionally I get errors like
GGT_BACKEND_PROCESS_CRASH: Cannot read properties of undefined (reading 'error')

TypeError: Cannot read properties of undefined (reading 'error')
at assertOperationSuccess (/gadget/app/node_modules/@gadgetinc/api-client-core/src/support.ts:318:16)
at backgroundActionResultRunner (/gadget/app/node_modules/@gadgetinc/api-client-core/src/operationRunners.ts:450:50)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at BackgroundActionHandle.result (/gadget/app/node_modules/@gadgetinc/api-client-core/src/BackgroundActionHandle.ts:40:13)
at discordRequest (/gadget/app/utils/discord.js:10:10)
...
GGT_BACKEND_PROCESS_CRASH: Cannot read properties of undefined (reading 'error')

TypeError: Cannot read properties of undefined (reading 'error')
at assertOperationSuccess (/gadget/app/node_modules/@gadgetinc/api-client-core/src/support.ts:318:16)
at backgroundActionResultRunner (/gadget/app/node_modules/@gadgetinc/api-client-core/src/operationRunners.ts:450:50)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at BackgroundActionHandle.result (/gadget/app/node_modules/@gadgetinc/api-client-core/src/BackgroundActionHandle.ts:40:13)
at discordRequest (/gadget/app/utils/discord.js:10:10)
...
however, I can see in the queue that the task was successful on the first attempt Hi, seems the issue can be fixed by simply calling await task.result() again I haven't ever had it throw the error on the second time but this leads me to believe its a gadget bug, and not a problem with my code unless you can offer any other insight? ie. by doing
try {
return await task.result()
} catch {
return await task.result()
}
try {
return await task.result()
} catch {
return await task.result()
}
but this seems obviously a pretty hacky fix
Chocci_Milk
Chocci_Milk7mo ago
Hello, I think that the issue might be referring to logger.error. I don't know why that would be an issue though. It should always be available in action context

Did you find this page helpful?