tarasglek / chatcraft.org

Developer-oriented ChatGPT clone
https://chatcraft.org/
MIT License
147 stars 26 forks source link

Extend ImportCommand to fetch error messages from CloudFlare Worker error pages #618

Closed rjwignar closed 2 months ago

rjwignar commented 2 months ago

This fixes #605

Background

Currently, if you use /import with a Youtube video that either has no captions or English subtitles, ChatCraft will throw this Error Toast:

image

This Error Toast isn't very descriptive.

When /import successfully parses YouTube video captions, res.text() returns the actual content that populates the Message: image.

However, if /import (when using youtube-captions-scraper) fails to do so (either due to no captions in specified language or no captions at all), res.text() returns the HTML of a CloudFlare Worker error page:

image

We only want the actual error message, nested inside the html content: image

Solution

I tried modifying the error handling in youtube-rewriter.ts and in proxy.ts (API handler for /import command) so res.text() would contain just the error message, but it seems no matter what I did, a bad response would always return HTML in res.text().

Instead, I decided to use Cheerio (already installed) to extract the error message from any bad responses that have an HTML type. I kept the old error message in case other error responses don't contain HTML.

The result is that if /import fails on a YouTube video, the user will get a useful Error Toast telling them why: No English captions (https://www.youtube.com/watch?v=y6120QOlsfU): image

No captions at all (https://www.youtube.com/watch?v=TKfS5zVfGBc): image

cloudflare-pages[bot] commented 2 months ago

Deploying chatcraft-org with  Cloudflare Pages  Cloudflare Pages

Latest commit: 46b569e
Status: ✅  Deploy successful!
Preview URL: https://08d7b6bb.console-overthinker-dev.pages.dev
Branch Preview URL: https://issue-605.console-overthinker-dev.pages.dev

View logs

rjwignar commented 2 months ago

Closing as we're exploring #625 instead