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

Return meta from retry.fail() #3789

Open
denchen opened this issue Oct 10, 2023 · 1 comment · May be fixed by #4721
Open

Return meta from retry.fail() #3789

denchen opened this issue Oct 10, 2023 · 1 comment · May be fixed by #4721
Labels
bug Something isn't working rtk-query

Comments

@denchen
Copy link

denchen commented Oct 10, 2023

Currently, if you use retry with fetchBaseQuery(), and you bail out early (eg, because of a 4xx status), then the result is missing meta data.

The code for fail is:

function fail(e: any): never {
  throw Object.assign(new HandledError({ error: e }), {
    throwImmediately: true,
  })
}

So retry.fail() is only returning / throwing the error, and not returning the meta, although HandledError is capable of handling meta:

export class HandledError {
  constructor(
    public readonly value: any,
    public readonly meta: any = undefined
  ) {}
}
@markerikson markerikson added bug Something isn't working rtk-query labels Feb 5, 2024 — with Volta.net
@LordZardeck
Copy link

I recently had this issue as well. We rely on the meta property in some of our listeners, but this broke when adding the retry functionality and bailing out early. The issue specifically is that in the fail method, instead of passing e into HandledError, it's passed to an object {error: e} which I believe is incorrect. Until this is fixed in the library itself, this was my workaround:

class HandledError {
	constructor(
		public readonly value: any,
		public readonly meta: any = undefined,
	) {}
}

// Replicates the behavior of `retry` from `redux-toolkit/query/react` but passes the full result instead of just the error
// @see https://github.com/reduxjs/redux-toolkit/issues/3789
function fail(e: any): never {
	throw Object.assign(new HandledError(e), {
		throwImmediately: true,
	})
}

// Allows us to bail out of retries by executing {fail} with the entire result
function retryableBaseQuery<T extends any[], R>(baseQueryFn: (...args: T) => R | PromiseLike<R>) {
	return async (...args: T): Promise<R> => {
		try {
			return await baseQueryFn(...args)
		} catch (error) {
			// Used to handle bailing out of retries
			if (error instanceof HandledError) {
				return error.value
			}
			throw error
		}
	}
}

Now if I use my fail method, I get the proper meta and payload for RTK queries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rtk-query
Projects
None yet
3 participants