OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to catch an error on type ReadableStreamDefaultController in Javascript

  • Thread starter Thread starter Oyedeji
  • Start date Start date
O

Oyedeji

Guest
I'm learning about streams and have been understanding it so far. Until recently when I had to face error handling in a custom readable stream, the code is below:

Code:
export default defineEventHandler(event => {
    // ... Some code for validation

    return new ReadableStream({
        start: async (controller) => {
          await scraper(controller, body.url);
          controller.close();
        }
    })
}

async function scraper(controller: ReadableStreamDefaultController) {
   // ... Steps before error
   if (valueDoesntMatch) controller.error({message: 'Error message', name: 'ERR_MESSAGE_TYPE'})
}

This code lives inside a server route /api/proxy. The server is Nitro (since I'm using Nuxt). Whenever an error is thrown from controller I get the following:

Code:
 ERROR  [nuxt] [request error] [unhandled] [500] Website couldn't be reached
  at createError (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:78:15)  
  at Server.toNodeHandle (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2268:21)  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

So my question is how would I catch this error or how can I handle it?

<p>I'm learning about streams and have been understanding it so far. Until recently when I had to face error handling in a <strong>custom</strong> readable stream, the code is below:</p>
<pre class="lang-js prettyprint-override"><code>export default defineEventHandler(event => {
// ... Some code for validation

return new ReadableStream({
start: async (controller) => {
await scraper(controller, body.url);
controller.close();
}
})
}

async function scraper(controller: ReadableStreamDefaultController) {
// ... Steps before error
if (valueDoesntMatch) controller.error({message: 'Error message', name: 'ERR_MESSAGE_TYPE'})
}

</code></pre>
<p>This code lives inside a server route <code>/api/proxy</code>. The server is Nitro (since I'm using Nuxt). Whenever an error is thrown from <code>controller</code> I get the following:</p>
<pre><code> ERROR [nuxt] [request error] [unhandled] [500] Website couldn't be reached
at createError (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:78:15)
at Server.toNodeHandle (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2268:21)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
</code></pre>
<p>So my question is how would I catch this error or how can I handle it?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top