Skip to content

[Bug?]: Cache service exception handling #11930

@cdubz

Description

@cdubz

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: https://github.com/redwoodjs/redwood/blob/6bbb92533103425fb9f9e2abcec91defc8b6b56e/packages/api/src/cache/index.ts#L118

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: https://github.com/redwoodjs/redwood/blob/6bbb92533103425fb9f9e2abcec91defc8b6b56e/packages/api/src/cache/index.ts#L131-L134

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/needs-infoMore information is needed for reproduction

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions