Skip to content

Correct way of stopping the middleware flow  #129

@matt-dalton

Description

@matt-dalton

We (possibly weirdly) combine this library with React-Relay-offline to persist queries to local/async storage.

I was trying to prevent the middlewares completing in the event of an error, so that the cache isn't updated. My last middleware function ("last" meaning furthest from the fetch result) looks like this

export const blockCacheOnErrorMiddleware = () => (next: MiddlewareNextFn) => async (
    req: RelayRequestAny
) => {
    const res = await next(req)
    
    // simple fn to grab the error from either a batch or single response
    const error = getFirstErrorFromRes(res)

    if (error) {
        return new Error('Error received from Relay response')
    }

    return res
}

This works great for what I need as far as I can tell. However, I am getting (unsurprisingly) an unhandled Promise exception in the console:

backend.js:32 Possible Unhandled Promise Rejection (id: 0):
RelayNetwork: No data returned for operation `ExampleQuery`, got error(s):
(No errors)

Is there a better way of handling this situation? I had a look at your example source code and couldn't see any similar use-cases

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions