Skip to content

Commit

Permalink
Merge pull request #396 from dreamit-de/395-fetch-recog
Browse files Browse the repository at this point in the history
#395 Add fetch failed as FetchError. Add tests
  • Loading branch information
sgohlke authored Nov 7, 2024
2 parents e271244 + e7113dc commit 2a97e0d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dreamit/graphql-server",
"version": "4.10.2",
"version": "4.11.0",
"description": "A GraphQL server written in NodeJS/Typescript.",
"scripts": {
"build": "tsup-node",
Expand Down
1 change: 1 addition & 0 deletions src/error/DetermineGraphQLOrFetchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function determineGraphQLOrFetchError(error: unknown): string {
return error instanceof Error &&
error.message &&
(error.message.includes(FETCH_ERROR) ||
error.message.includes('fetch failed') ||
error.message.includes('ECONNREFUSED') ||
error.message.includes('ECONNRESET') ||
error.message.includes('ETIMEDOUT') ||
Expand Down
29 changes: 29 additions & 0 deletions tests/error/DetermineGraphQLOrFetchError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable unicorn/error-message */
import { FETCH_ERROR, GRAPHQL_ERROR } from '@dreamit/graphql-server-base'
import { determineGraphQLOrFetchError } from 'src'
import { expect, test } from 'vitest'

const redirectedErrorMessage =
'uri requested responds with a redirect, redirect mode is set to error'

test.each`
errorMessage | expectedErrorName
${'ECONNREFUSED'} | ${FETCH_ERROR}
${'ECONNRESET'} | ${FETCH_ERROR}
${'ETIMEDOUT'} | ${FETCH_ERROR}
${'network timeout'} | ${FETCH_ERROR}
${'invalid redirect URL'} | ${FETCH_ERROR}
${redirectedErrorMessage} | ${FETCH_ERROR}
${'maximum redirect reached'} | ${FETCH_ERROR}
${'Cannot follow redirect'} | ${FETCH_ERROR}
${'socket hang up'} | ${FETCH_ERROR}
${'fetch failed'} | ${FETCH_ERROR}
${'I am a GraphQLError'} | ${GRAPHQL_ERROR}
`(
'Correctly determine if error $errorMessage is a GraphQLError or FetchError $expectedErrorName',
({ errorMessage, expectedErrorName }) => {
expect(determineGraphQLOrFetchError(new Error(errorMessage))).toBe(
expectedErrorName,
)
},
)

0 comments on commit 2a97e0d

Please sign in to comment.