Skip to content

Commit 488c28c

Browse files
authored
fix: getPayload node exits on webpack-hmr websocket failure (#9279)
### What? When a script attempts to load payload using `getPayload()`, it will end with: `Error: connect ECONNREFUSED 127.0.0.1:3000` etc... ### Why? Even though there is a try/catch, it still errors because WebSocket connection failures happen asynchronously after the ws object is instantiated. ### How? Added the error handling function cached.ws.onerror to prevent exit.
1 parent 7489c29 commit 488c28c

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

packages/payload/src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ExecutionResult, GraphQLSchema, ValidationRule } from 'graphql'
22
import type { Request as graphQLRequest, OperationArgs } from 'graphql-http'
33
import type { Logger } from 'pino'
4+
import type { NonNever } from 'ts-essentials'
45

56
import { spawn } from 'child_process'
67
import crypto from 'crypto'
@@ -25,12 +26,6 @@ import type {
2526
SelectFromCollectionSlug,
2627
TypeWithID,
2728
} from './collections/config/types.js'
28-
29-
import { generateImportMap, type ImportMap } from './bin/generateImportMap/index.js'
30-
export type { FieldState } from './admin/forms/Form.js'
31-
export type * from './admin/types.js'
32-
import type { NonNever } from 'ts-essentials'
33-
3429
import type { Options as CountOptions } from './collections/operations/local/count.js'
3530
import type { Options as CreateOptions } from './collections/operations/local/create.js'
3631
import type {
@@ -72,6 +67,7 @@ import type { TypeWithVersion } from './versions/types.js'
7267
import { decrypt, encrypt } from './auth/crypto.js'
7368
import { APIKeyAuthentication } from './auth/strategies/apiKey.js'
7469
import { JWTAuthentication } from './auth/strategies/jwt.js'
70+
import { generateImportMap, type ImportMap } from './bin/generateImportMap/index.js'
7571
import { checkPayloadDependencies } from './checkPayloadDependencies.js'
7672
import localOperations from './collections/operations/local/index.js'
7773
import { consoleEmailAdapter } from './email/consoleEmailAdapter.js'
@@ -82,6 +78,9 @@ import { getLogger } from './utilities/logger.js'
8278
import { serverInit as serverInitTelemetry } from './utilities/telemetry/events/serverInit.js'
8379
import { traverseFields } from './utilities/traverseFields.js'
8480

81+
export type { FieldState } from './admin/forms/Form.js'
82+
export type * from './admin/types.js'
83+
8584
export interface GeneratedTypes {
8685
authUntyped: {
8786
[slug: string]: {
@@ -848,6 +847,10 @@ export const getPayload = async (
848847
}
849848
}
850849
}
850+
851+
cached.ws.onerror = (_) => {
852+
// swallow any websocket connection error
853+
}
851854
} catch (_) {
852855
// swallow e
853856
}

0 commit comments

Comments
 (0)