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

[Bug?]: Cache service exception handling #11930

Open
1 task done
cdubz opened this issue Jan 25, 2025 · 0 comments
Open
1 task done

[Bug?]: Cache service exception handling #11930

cdubz opened this issue Jan 25, 2025 · 0 comments
Labels
bug/needs-info More information is needed for reproduction

Comments

@cdubz
Copy link

cdubz commented Jan 25, 2025

What's not working?

I'm looking to establish if this qualifies as a bug or is intentional -- when I am using the cache service and some condition in my input makes me want to "abort" caching the thing (e.g., a network request failure) I'm not sure how to do that.

The cache service first tries to set data from the input:

data = await input()

If input() throws an exception data does not get set. When the cache service catches this exception, it reports and the error and then tries to run input() again, causing the same exception to be thrown:

} catch (e: any) {
logger?.error(`[Cache] Error SET '${cacheKey}': ${e.message}`)
return serialize(data || (await input()))
}

If I put the cache call in my own try ... catch, I can catch and handle the exception and that is kind of helpful. But since input is run twice, there is also a chance that the first run causes an exception and the second run does not so I may get confusing error reporting or miss intermittent issues entirely.

A pattern I have used and liked in other cache interfaces is the ability to throw some specific exception type that tells the cache system to not cache a result (e.g. throw new UncacheableError()) without throwing it's own exception.

Alternatively, I think it would be possible to just return undefined from the catch statement in cache.

If this really is the desired behavior, I think it would be helpful to add some documentation around cache service error handling.

How do we reproduce the bug?

await cache(['key'], () => { throw new Error('Something went wrong!') })

What's your environment? (If it applies)

Are you interested in working on this?

  • I'm interested in working on this
@cdubz cdubz added the bug/needs-info More information is needed for reproduction label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/needs-info More information is needed for reproduction
Projects
None yet
Development

No branches or pull requests

1 participant