Skip to content

Commit

Permalink
fix: getPayload node exits on webpack-hmr websocket failure (#9279)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
DanRibbens authored Nov 18, 2024
1 parent 7489c29 commit 488c28c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/payload/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ExecutionResult, GraphQLSchema, ValidationRule } from 'graphql'
import type { Request as graphQLRequest, OperationArgs } from 'graphql-http'
import type { Logger } from 'pino'
import type { NonNever } from 'ts-essentials'

import { spawn } from 'child_process'
import crypto from 'crypto'
Expand All @@ -25,12 +26,6 @@ import type {
SelectFromCollectionSlug,
TypeWithID,
} from './collections/config/types.js'

import { generateImportMap, type ImportMap } from './bin/generateImportMap/index.js'
export type { FieldState } from './admin/forms/Form.js'
export type * from './admin/types.js'
import type { NonNever } from 'ts-essentials'

import type { Options as CountOptions } from './collections/operations/local/count.js'
import type { Options as CreateOptions } from './collections/operations/local/create.js'
import type {
Expand Down Expand Up @@ -72,6 +67,7 @@ import type { TypeWithVersion } from './versions/types.js'
import { decrypt, encrypt } from './auth/crypto.js'
import { APIKeyAuthentication } from './auth/strategies/apiKey.js'
import { JWTAuthentication } from './auth/strategies/jwt.js'
import { generateImportMap, type ImportMap } from './bin/generateImportMap/index.js'
import { checkPayloadDependencies } from './checkPayloadDependencies.js'
import localOperations from './collections/operations/local/index.js'
import { consoleEmailAdapter } from './email/consoleEmailAdapter.js'
Expand All @@ -82,6 +78,9 @@ import { getLogger } from './utilities/logger.js'
import { serverInit as serverInitTelemetry } from './utilities/telemetry/events/serverInit.js'
import { traverseFields } from './utilities/traverseFields.js'

export type { FieldState } from './admin/forms/Form.js'
export type * from './admin/types.js'

export interface GeneratedTypes {
authUntyped: {
[slug: string]: {
Expand Down Expand Up @@ -848,6 +847,10 @@ export const getPayload = async (
}
}
}

cached.ws.onerror = (_) => {
// swallow any websocket connection error
}
} catch (_) {
// swallow e
}
Expand Down

0 comments on commit 488c28c

Please sign in to comment.