createContentfulErrorMessage = e => {
  // Handle axios error messages
  if (e.isAxiosError) {
    const axiosErrorMessage = [e.code, e.status]
    const axiosErrorDetails = []

    if (e.response) {
      axiosErrorMessage.push(e.response.status)

      // Parse Contentful API error data
      if (e.response?.data) {
        axiosErrorMessage.push(e.response.data.sys?.id, e.response.data.message)
      }

      // Get request ID from headers
      const requestId =
        e.response.headers &&
        typeof e.response.headers === `object` &&
        e.response.headers[`x-contentful-request-id`]

      if (requestId) {
        axiosErrorDetails.push(`Request ID: ${requestId}`)
      }
    }

    if (e.attempts) {
      axiosErrorDetails.push(`Attempts: ${e.attempts}`)
    }

    if (axiosErrorDetails.length) {
      axiosErrorMessage.push(
        `\n\n---\n${axiosErrorDetails.join(`\n\n`)}\n\n---\n`
      )
    }

    return axiosErrorMessage.filter(Boolean).join(` `)
  }

  // If it is not an axios error, we assume that we got a Contentful SDK error and try to parse it
  const errorMessage = [e.name]
  const errorDetails = []
  try {
    /**
     * Parse stringified error data from message
     * https://github.com/contentful/contentful-sdk-core/blob/4cfcd452ba0752237a26ce6b79d72a50af84d84e/src/error-handler.ts#L71-L75
     *
     * @todo properly type this with TS
     * type {
     *    status?: number
     *    statusText?: string
     *    requestId?: string
     *    message: string
     *    !details: Record<string, unknown>
     *    !request?: Record<string, unknown>
     *  }
     */
    const errorData = JSON.parse(e.message)
    errorMessage.push(errorData.status && String(errorData.status))
    errorMessage.push(errorData.statusText)
    errorMessage.push(errorData.message)
    if (errorData.requestId) {
      errorDetails.push(`Request ID: ${errorData.requestId}`)
    }
    if (errorData.request) {
      errorDetails.push(
        `Request:\n${JSON.stringify(errorData.request, null, 2)}`
      )
    }
    if (errorData.details && Object.keys(errorData.details).length) {
      errorDetails.push(
        `Details:\n${JSON.stringify(errorData.details, null, 2)}`
      )
    }
  } catch (err) {
    // If we can't parse it, we assume its a human readable string
    errorMessage.push(e.message)
  }

  if (errorDetails.length) {
    errorMessage.push(`\n\n---\n${errorDetails.join(`\n\n`)}\n\n---\n`)
  }

  return errorMessage.filter(Boolean).join(` `)
}
Generate a user friendly error message.

Contentful's API has its own error message structure, which might change depending of internal server or authentification errors.