chore: handle positions with market data errors - fix use of error policy

This commit is contained in:
maciek
2023-02-08 17:46:23 +01:00
parent 37fedbe3a8
commit 5f9eb261d5
3 changed files with 39 additions and 8 deletions
@@ -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()),
+16 -3
View File
@@ -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;