razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
306 stars 9 forks source link

Show error or throw error on `emit('event', { some: 'data' })` #52

Closed bluepuma77 closed 3 months ago

bluepuma77 commented 4 months ago

When using emit('event', { some: 'data' }), it seems nothing is sent to the client, and no error message is shown or thrown.

It would be great if a notice would be provided to the developer that they use wrong parameter format.

razshare commented 4 months ago

Hello @bluepuma77 , emit() doesn't throw errors, it returns Unsafe<void>, which is an Either<L,R> in disguise, in this case L being void and R an Error.

import { produce } from 'sveltekit-sse';

export function POST() {
  return produce(async function start({ emit }) {
    while (true) {
      const { error } = await emit('event', { some: 'data' });
      if (error) {
        console.error(`There's been an error.\n${error.message}`);
        return;
      }
    }
  });
}

image

When it comes to static analysis, I'm getting a type error as expected - image

Let me know if this solves your issue.

razshare commented 3 months ago

I'm closing this issue due to inactivity. Feel free to open another issue or follow up here if you think it should be reopened.