Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch parsing errors #10

Open
ishandutta2007 opened this issue Feb 13, 2024 · 6 comments
Open

How to catch parsing errors #10

ishandutta2007 opened this issue Feb 13, 2024 · 6 comments

Comments

@ishandutta2007
Copy link

ishandutta2007 commented Feb 13, 2024

How do I catch in such scenarios:
str is rightly printed but when it enters parser.feed, it gets stuck and never returns; nor does it throw any errors.
And hence it never reaches console.log("event", event) even

import { createParser } from 'eventsource-parser'
  const parser = createParser((event) => {
    console.log("event", event)
    if (event.type === 'event') {
      onMessage(event.data)
    }
  })
  for await (const chunk of streamAsyncIterable(resp.body!)) {
    const str = new TextDecoder().decode(chunk)
    console.log("str", str)
    parser.feed(str)
  }

resp is the respose object and onMessage is just a function

@ishandutta2007
Copy link
Author

ishandutta2007 commented Feb 13, 2024

On debugging I found it encounters the break on this line and since it breaks out of while loop it never processes parseEventStreamLine.
What I understand event was supposed to be returned by feed but since it reaches the end of the function the callback passed to createParser never gets called.

@rexxars
Copy link
Owner

rexxars commented Feb 13, 2024

Could you provide some example data?

Using string chunks is usually the easiest in order to debug:

import {createParser} from 'eventsource-parser'

const parser = createParser((event) => {
  console.log('event', event)
})

const chunks = [
  'data: hello\n\n',
  'data: world\n\n',
  'event: custom\n',
  'id: abc123\n',
  'data: event\n\n',
]

for (const str of chunks) {
  console.log('chunk', JSON.stringify(str))
  parser.feed(str)
}

Output:

chunk "data: hello\n\n"
event { type: 'event', id: undefined, event: undefined, data: 'hello' }
chunk "data: world\n\n"
event { type: 'event', id: undefined, event: undefined, data: 'world' }
chunk "event: custom\n"
chunk "id: abc123\n"
chunk "data: event\n\n"
event { type: 'event', id: 'abc123', event: 'custom', data: 'event' }

@ishandutta2007
Copy link
Author

ishandutta2007 commented Feb 13, 2024

@rexxars As I said my example data isn't valid format starting with data: .
It's some json, like something like this {key1:data1,key2:data2} but not what this library parses out of the box.
I didn't expect the API server to return it suddenly, which is why I had to suffer code freeze on production.
Now I handle it like this but Ideally eventsource-parser library should have thrown some error while parsing instead of silently suppressing it.

    if (str.includes('some_keyword')) {
      console.log("non parsable str")
      onMessage(str)
    } else {
      parser.feed(str)
    }

@rexxars
Copy link
Owner

rexxars commented Feb 14, 2024

I don't see you mentioning anything about invalid data, but fair enough - thanks for clarifying.

@ishandutta2007
Copy link
Author

ishandutta2007 commented Feb 14, 2024

I guess I meant it by explaining the debugging steps especially break-ing etc that it was not a format which eventsource-parser expects. Sorry for not being explicit which cased miscommunication.

Anyways lets keep it as a priority issue for next release. Throwing all invalid cases as exception in loud and clear manner is a must have for any library in today's world where projects depend on hundreds of libraries. I kept hunting for hours to figure out which library is making it go in infinite loop .

By the way this is a very nice lib, it eased my parsing headache, we should always thank people of npm community for their free of cost volunteering before any communication❤️

@rexxars rexxars changed the title How do I catch in such scenarios? How to catch parsing errors Feb 14, 2024
@Peek-A-Booo
Copy link

Peek-A-Booo commented Mar 20, 2024

Could you provide some example data?

This is my code, console.log(event) will not run when I receive a value in this format which not starting with data:

const textDecoder = new TextDecoder()
let eventSourceParser: EventSourceParser

return new TransformStream({
  async start(controller) {
    eventSourceParser = createParser(
      (event: ParsedEvent | ReconnectInterval) => {
        console.log(event)
      },
    )
  },
  transform(chunk) {
    const value = textDecoder.decode(chunk)
    // the value is a string like:
    // {
    //   "error": {
    //     "message": "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_FwpczVWzU3oIb0tSLiCHOt2z",
    //     "type": "invalid_request_error",
    //     "param": "messages",
    //     "code": null
    //   }
    // }
    eventSourceParser.feed(value)
  },
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants