Skip to content

Commit

Permalink
refactor: improve error logging during onInit and website template se…
Browse files Browse the repository at this point in the history
…ed (#10528)

This PR ensures that onInit and website template seed errors are logged
properly
  • Loading branch information
AlessioGr authored Jan 13, 2025
1 parent 9b6cdd0 commit 142c504
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,6 @@ jobs:
if: steps.cache-playwright-browsers.outputs.cache-hit == 'true'
run: pnpm exec playwright install-deps chromium

- name: E2E Tests
uses: nick-fields/retry@v3
with:
retry_on: any
max_attempts: 5
timeout_minutes: 20
command: PLAYWRIGHT_JSON_OUTPUT_NAME=results_${{ matrix.suite }}.json pnpm test:e2e:prod:ci ${{ matrix.suite }}
on_retry_command: pnpm clean:build:allowtgz && pnpm install --no-frozen-lockfile && pnpm build:all
env:
PLAYWRIGHT_JSON_OUTPUT_NAME: results_${{ matrix.suite }}.json
NEXT_TELEMETRY_DISABLED: 1

- uses: actions/upload-artifact@v4
if: always()
with:
Expand Down
2 changes: 1 addition & 1 deletion examples/localization/src/endpoints/seedHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const seedHandler: PayloadHandler = async (req): Promise<Response> => {
return Response.json({ success: true })
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error'
payload.logger.error(message)
payload.logger.error({ err: error, message: 'Error seeding data' })
return Response.json({ error: message }, { status: 500 })
}
}
24 changes: 15 additions & 9 deletions packages/payload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,9 @@ export class BasePayload {
config: this.config.globals,
}

this.config.collections.forEach((collection) => {
for (const collection of this.config.collections) {
let customIDType = undefined
const findCustomID: TraverseFieldsCallback = ({ field, next }) => {
const findCustomID: TraverseFieldsCallback = ({ field }) => {
if (
['array', 'blocks', 'group'].includes(field.type) ||
(field.type === 'tab' && 'name' in field)
Expand All @@ -607,7 +607,7 @@ export class BasePayload {
config: collection,
customIDType,
}
})
}

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

if (!options.disableOnInit) {
if (typeof options.onInit === 'function') {
await options.onInit(this)
}
if (typeof this.config.onInit === 'function') {
await this.config.onInit(this)
try {
if (!options.disableOnInit) {
if (typeof options.onInit === 'function') {
await options.onInit(this)
}
if (typeof this.config.onInit === 'function') {
await this.config.onInit(this)
}
}
} catch (error) {
this.logger.error({ err: error }, 'Error running onInit function')
throw error
}

if (this.config.jobs.autoRun && !isNextBuild()) {
const DEFAULT_CRON = '* * * * *'
const DEFAULT_LIMIT = 10
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/utilities/routeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const routeError = async ({
err: APIError
req: PayloadRequest | Request
}) => {
let payload = 'payload' in incomingReq && incomingReq?.payload
let payload = incomingReq && 'payload' in incomingReq && incomingReq?.payload

if (!payload) {
try {
Expand Down
3 changes: 2 additions & 1 deletion templates/website/src/app/(frontend)/next/seed/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export async function POST(
await seed({ payload, req: payloadReq })

return Response.json({ success: true })
} catch {
} catch (e) {
payload.logger.error({ err: e, message: 'Error seeding data' })
return new Response('Error seeding data.', { status: 500 })
}
}

0 comments on commit 142c504

Please sign in to comment.