• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alkem-io / client-web / #8427

22 Jul 2024 10:11AM UTC coverage: 5.739%. First build
#8427

Pull #6612

travis-ci

Pull Request #6612: Package updates to reduce vulnerabilities

188 of 9993 branches covered (1.88%)

Branch coverage included in aggregate %.

0 of 25 new or added lines in 13 files covered. (0.0%)

1406 of 17783 relevant lines covered (7.91%)

0.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.25
/src/core/apollo/hooks/useApolloErrorHandler.ts
1
import { Severity } from '@/core/state/global/notifications/notificationMachine';
2
import { useNotification } from '@/core/ui/notifications/useNotification';
3
import { ApolloError } from '@apollo/client';
4
import { GraphQLError, GraphQLFormattedError } from 'graphql';
5
import type { TFunction, i18n } from 'i18next';
6
import { useTranslation } from 'react-i18next';
7
import { AlkemioGraphqlErrorCode } from '@/main/constants/errors';
8
import TranslationKey from '@/core/i18n/utils/TranslationKey';
1✔
9

×
10
const getTranslationForCode = (error: GraphQLFormattedError, t: TFunction, i18n: i18n) => {
×
11
  const { message } = error;
×
12
  const code = error.extensions?.code as string;
13
  const meta = { code, message };
×
14

15
  if (!code) {
×
16
    // if code missing send a generic error text
17
    return t('apollo.errors.generic', meta);
18
  }
×
19

20
  const key = `apollo.errors.${code}` as TranslationKey;
×
21

22
  if (!i18n.exists(key)) {
23
    // if the error text is missing for that code
×
24
    // send a generic error text with code
25
    return t('apollo.errors.generic-with-code', meta);
26
  }
×
27
  // send the error text
28
  return t(key, meta);
29
};
1!
30

×
31
export const useApolloErrorHandler = (severity: Severity = 'error') => {
×
32
  const { t, i18n } = useTranslation();
33
  const notify = useNotification();
×
34

×
NEW
35
  const handleNetworkErrors = (error: ApolloError) => {
×
36
    const networkError = error.networkError;
×
37
    if (
38
      networkError &&
39
      'result' in networkError &&
40
      typeof networkError.result === 'object' &&
41
      'errors' in networkError.result &&
42
      networkError.result.errors
×
43
    ) {
×
44
      const error = networkError.result.errors[0] as GraphQLError;
45
      notify(error.message, severity);
46
    }
47
  };
×
48

×
49
  const handleGraphQLErrors = (error: ApolloError) => {
50
    const graphqlErrors = error.graphQLErrors;
×
51

×
52
    graphqlErrors.forEach((error: GraphQLFormattedError) => {
×
53
      const translation = getTranslationForCode(error, t, i18n);
54
      notify(translation, severity);
55
    });
56
  };
×
57

×
58
  const handleClientErrors = (error: ApolloError) => {
×
59
    if (error.clientErrors && error.clientErrors.length > 0) {
60
      notify(error.message, severity);
61
    }
62
  };
×
63

×
64
  return (error: ApolloError) => {
×
65
    handleNetworkErrors(error);
×
66
    handleGraphQLErrors(error);
67
    handleClientErrors(error);
68
  };
69
};
1✔
70

×
71
export const isApolloNotFoundError = (error: ApolloError | undefined) => {
×
72
  if (error && error.graphQLErrors) {
×
73
    const extensions = error.graphQLErrors.map(graphQLError => graphQLError.extensions);
74
    return extensions.some(extension => extension?.code === AlkemioGraphqlErrorCode.ENTITY_NOT_FOUND);
×
75
  }
76
  return false;
77
};
1✔
78

×
79
export const isApolloForbiddenError = (error: ApolloError | undefined) => {
×
80
  if (error && error.graphQLErrors) {
×
81
    const extensions = error.graphQLErrors.map(graphQLError => graphQLError.extensions);
82
    return extensions.some(extension => extension?.code === AlkemioGraphqlErrorCode.FORBIDDEN);
×
83
  }
84
  return false;
85
};
86

87
export const isApolloForbiddenPolicyError = (error: ApolloError | undefined) => {
88
  if (error && error.graphQLErrors) {
89
    const extensions = error.graphQLErrors.map(graphQLError => graphQLError.extensions);
90
    return extensions.some(extension => extension?.code === AlkemioGraphqlErrorCode.FORBIDDEN_POLICY);
91
  }
92
  return false;
93
};
94

95
export const isApolloAuthorizationError = (error: ApolloError | undefined) => {
96
  return error && (isApolloForbiddenError(error) || isApolloForbiddenPolicyError(error));
97
};
98

99
export const isUrlResolverError = (error: ApolloError | undefined) => {
100
  if (error && error.graphQLErrors) {
101
    const extensions = error.graphQLErrors.map(graphQLError => graphQLError.extensions);
102
    return extensions.some(extension => extension?.code === AlkemioGraphqlErrorCode.URL_RESOLVER_ERROR);
103
  }
104
  return false;
105
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc