chore: handle positions with market data errors - fix use of error policy
This commit is contained in:
@@ -150,7 +150,7 @@ export const getMetrics = (
|
||||
) && generalAccountBalance.isLessThan(marginInitial.minus(marginSearch));
|
||||
|
||||
metrics.push({
|
||||
marketName: market.tradableInstrument.instrument.name + ' ' + market.id,
|
||||
marketName: market.tradableInstrument.instrument.name,
|
||||
averageEntryPrice: position.averageEntryPrice,
|
||||
marginAccountBalance: marginAccount.balance,
|
||||
capitalUtilisation: Math.round(capitalUtilisation.toNumber()),
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { ApolloError } from '@apollo/client';
|
||||
import type { GraphQLErrors } from '@apollo/client/errors';
|
||||
|
||||
const NOT_FOUND = 'NotFound';
|
||||
const INTERNAL = 'Internal';
|
||||
|
||||
const isApolloGraphQLError = (
|
||||
error: ApolloError | Error | undefined
|
||||
@@ -9,21 +10,33 @@ const isApolloGraphQLError = (
|
||||
return !!error && !!(error as ApolloError).graphQLErrors;
|
||||
};
|
||||
|
||||
const hasNotFoundGraphQLErrors = (errors: GraphQLErrors, path?: string) => {
|
||||
const hasNotFoundGraphQLErrors = (errors: GraphQLErrors, path?: string[]) => {
|
||||
return errors.some(
|
||||
(e) =>
|
||||
e.extensions &&
|
||||
e.extensions['type'] === NOT_FOUND &&
|
||||
(!path || e.path?.[0] === path)
|
||||
(!path || path.every((item, i) => item === e?.path?.[i]))
|
||||
);
|
||||
};
|
||||
|
||||
export const isNotFoundGraphQLError = (
|
||||
error: Error | ApolloError | undefined,
|
||||
path?: string
|
||||
path?: string[]
|
||||
) => {
|
||||
return (
|
||||
isApolloGraphQLError(error) &&
|
||||
hasNotFoundGraphQLErrors(error.graphQLErrors, path)
|
||||
);
|
||||
};
|
||||
|
||||
export const isOnlyMarketDataNotFoundErrors = (
|
||||
errors: GraphQLErrors,
|
||||
path?: string[]
|
||||
) => {
|
||||
return errors.every(
|
||||
(e) =>
|
||||
e.extensions &&
|
||||
e.extensions['type'] === INTERNAL &&
|
||||
(!path || path.every((item, i) => item === e?.path?.[i]))
|
||||
);
|
||||
};
|
||||
|
||||
@@ -5,10 +5,14 @@ import type {
|
||||
OperationVariables,
|
||||
TypedDocumentNode,
|
||||
FetchResult,
|
||||
ErrorPolicy,
|
||||
} from '@apollo/client';
|
||||
import type { Subscription } from 'zen-observable-ts';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import { isNotFoundGraphQLError } from './apollo-client';
|
||||
import {
|
||||
isNotFoundGraphQLError,
|
||||
isOnlyMarketDataNotFoundErrors,
|
||||
} from './apollo-client';
|
||||
import type * as Schema from '@vegaprotocol/types';
|
||||
interface UpdateData<Data, Delta> {
|
||||
delta?: Delta;
|
||||
@@ -313,15 +317,29 @@ function makeDataProviderInternal<
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await client.query<QueryData>({
|
||||
|
||||
const call = (policy?: ErrorPolicy) =>
|
||||
client.query<QueryData>({
|
||||
query,
|
||||
variables: pagination
|
||||
? { ...variables, pagination: { first: pagination.first } }
|
||||
: variables,
|
||||
fetchPolicy: fetchPolicy || 'no-cache',
|
||||
context: additionalContext,
|
||||
errorPolicy: policy || 'none',
|
||||
});
|
||||
|
||||
try {
|
||||
const res = (await call().catch((err) => {
|
||||
if (
|
||||
err.graphQLErrors &&
|
||||
isOnlyMarketDataNotFoundErrors(err.graphQLErrors, ['market', 'data'])
|
||||
) {
|
||||
return call('ignore');
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
data = getData(res.data, variables);
|
||||
if (data && pagination) {
|
||||
if (!(data instanceof Array)) {
|
||||
@@ -355,7 +373,7 @@ function makeDataProviderInternal<
|
||||
}
|
||||
loaded = true;
|
||||
} catch (e) {
|
||||
if (isNotFoundGraphQLError(e as Error, 'party')) {
|
||||
if (isNotFoundGraphQLError(e as Error, ['party'])) {
|
||||
data = getData(null, variables);
|
||||
loaded = true;
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user