All files reportCacheErrors.ts

0% Statements 0/29
0% Branches 0/14
0% Functions 0/2
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                               
/**
 * A [`GraphQL`]{@link GraphQL} `cache` event handler that reports
 * [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API), HTTP, parse
 * and GraphQL errors via `console.log()`. In a browser environment the grouped
 * error details are expandable.
 * @kind function
 * @name reportCacheErrors
 * @param {object} data [`GraphQL`]{@link GraphQL} `cache` event data.
 * @param {GraphQLCacheKey} data.cacheKey [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey}.
 * @param {GraphQLCacheKey} data.cacheValue [GraphQL cache]{@link GraphQL#cache} [value]{@link GraphQLCacheValue}.
 * @example <caption>[`GraphQL`]{@link GraphQL} initialized to report cache errors.</caption>
 * ```js
 * import { GraphQL, reportCacheErrors } from 'graphql-react'
 *
 * const graphql = new GraphQL()
 * graphql.on('cache', reportCacheErrors)
 * ```
 */
export function reportCacheErrors({
  cacheKey,
  cacheValue: { fetchError, httpError, parseError, graphQLErrors },
}: any) {
  if (fetchError || httpError || parseError || graphQLErrors) {
    // eslint-disable-next-line no-console
    console.groupCollapsed(`GraphQL cache errors for key “${cacheKey}”:`)
 
    if (fetchError) {
      // eslint-disable-next-line no-console
      console.groupCollapsed('Fetch:')
 
      // eslint-disable-next-line no-console
      console.log(fetchError)
 
      // eslint-disable-next-line no-console
      console.groupEnd()
    }
 
    if (httpError) {
      // eslint-disable-next-line no-console
      console.groupCollapsed('HTTP:')
 
      // eslint-disable-next-line no-console
      console.log(`Status: ${httpError.status}`)
 
      // eslint-disable-next-line no-console
      console.log(`Text: ${httpError.statusText}`)
 
      // eslint-disable-next-line no-console
      console.groupEnd()
    }
 
    if (parseError) {
      // eslint-disable-next-line no-console
      console.groupCollapsed('Parse:')
 
      // eslint-disable-next-line no-console
      console.log(parseError)
 
      // eslint-disable-next-line no-console
      console.groupEnd()
    }
 
    if (graphQLErrors) {
      // eslint-disable-next-line no-console
      console.groupCollapsed('GraphQL:')
 
      graphQLErrors.forEach(({ message }: any) =>
        // eslint-disable-next-line no-console
        console.log(message)
      )
 
      // eslint-disable-next-line no-console
      console.groupEnd()
    }
 
    // eslint-disable-next-line no-console
    console.groupEnd()
  }
}