Skip to content

Commit 142c504

Browse files
authored
refactor: improve error logging during onInit and website template seed (#10528)
This PR ensures that onInit and website template seed errors are logged properly
1 parent 9b6cdd0 commit 142c504

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,6 @@ jobs:
367367
if: steps.cache-playwright-browsers.outputs.cache-hit == 'true'
368368
run: pnpm exec playwright install-deps chromium
369369

370-
- name: E2E Tests
371-
uses: nick-fields/retry@v3
372-
with:
373-
retry_on: any
374-
max_attempts: 5
375-
timeout_minutes: 20
376-
command: PLAYWRIGHT_JSON_OUTPUT_NAME=results_${{ matrix.suite }}.json pnpm test:e2e:prod:ci ${{ matrix.suite }}
377-
on_retry_command: pnpm clean:build:allowtgz && pnpm install --no-frozen-lockfile && pnpm build:all
378-
env:
379-
PLAYWRIGHT_JSON_OUTPUT_NAME: results_${{ matrix.suite }}.json
380-
NEXT_TELEMETRY_DISABLED: 1
381-
382370
- uses: actions/upload-artifact@v4
383371
if: always()
384372
with:

examples/localization/src/endpoints/seedHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const seedHandler: PayloadHandler = async (req): Promise<Response> => {
2121
return Response.json({ success: true })
2222
} catch (error: unknown) {
2323
const message = error instanceof Error ? error.message : 'Unknown error'
24-
payload.logger.error(message)
24+
payload.logger.error({ err: error, message: 'Error seeding data' })
2525
return Response.json({ error: message }, { status: 500 })
2626
}
2727
}

packages/payload/src/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ export class BasePayload {
581581
config: this.config.globals,
582582
}
583583

584-
this.config.collections.forEach((collection) => {
584+
for (const collection of this.config.collections) {
585585
let customIDType = undefined
586-
const findCustomID: TraverseFieldsCallback = ({ field, next }) => {
586+
const findCustomID: TraverseFieldsCallback = ({ field }) => {
587587
if (
588588
['array', 'blocks', 'group'].includes(field.type) ||
589589
(field.type === 'tab' && 'name' in field)
@@ -607,7 +607,7 @@ export class BasePayload {
607607
config: collection,
608608
customIDType,
609609
}
610-
})
610+
}
611611

612612
// Generate types on startup
613613
if (process.env.NODE_ENV !== 'production' && this.config.typescript.autoGenerate !== false) {
@@ -707,14 +707,20 @@ export class BasePayload {
707707
})
708708
}
709709

710-
if (!options.disableOnInit) {
711-
if (typeof options.onInit === 'function') {
712-
await options.onInit(this)
713-
}
714-
if (typeof this.config.onInit === 'function') {
715-
await this.config.onInit(this)
710+
try {
711+
if (!options.disableOnInit) {
712+
if (typeof options.onInit === 'function') {
713+
await options.onInit(this)
714+
}
715+
if (typeof this.config.onInit === 'function') {
716+
await this.config.onInit(this)
717+
}
716718
}
719+
} catch (error) {
720+
this.logger.error({ err: error }, 'Error running onInit function')
721+
throw error
717722
}
723+
718724
if (this.config.jobs.autoRun && !isNextBuild()) {
719725
const DEFAULT_CRON = '* * * * *'
720726
const DEFAULT_LIMIT = 10

packages/payload/src/utilities/routeError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const routeError = async ({
2222
err: APIError
2323
req: PayloadRequest | Request
2424
}) => {
25-
let payload = 'payload' in incomingReq && incomingReq?.payload
25+
let payload = incomingReq && 'payload' in incomingReq && incomingReq?.payload
2626

2727
if (!payload) {
2828
try {

templates/website/src/app/(frontend)/next/seed/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export async function POST(
3232
await seed({ payload, req: payloadReq })
3333

3434
return Response.json({ success: true })
35-
} catch {
35+
} catch (e) {
36+
payload.logger.error({ err: e, message: 'Error seeding data' })
3637
return new Response('Error seeding data.', { status: 500 })
3738
}
3839
}

0 commit comments

Comments
 (0)