Fix error handling and improve naming consistency

- Use native error chaining in workflow (Error constructor with cause option)
- Fix job scheduling to use 'task' instead of 'workflow' property
- Rename processEmailsJob.ts to processEmailsTask.ts for consistency
- Update all imports and references while maintaining backward compatibility
- Add processEmailsTask export with processEmailsJob alias
- Bump version to 0.3.1
This commit is contained in:
2025-09-14 17:34:53 +02:00
parent dd205dba41
commit 845b379da3
4 changed files with 18 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@xtr-dev/payload-mailing", "name": "@xtr-dev/payload-mailing",
"version": "0.3.0", "version": "0.3.1",
"description": "Template-based email system with scheduling and job processing for PayloadCMS", "description": "Template-based email system with scheduling and job processing for PayloadCMS",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -1,4 +1,4 @@
import { processEmailsJob } from './processEmailsJob.js' import { processEmailsJob } from './processEmailsTask.js'
import { sendEmailJob } from './sendEmailTask.js' import { sendEmailJob } from './sendEmailTask.js'
/** /**
@@ -10,5 +10,5 @@ export const mailingJobs = [
] ]
// Re-export everything from individual job files // Re-export everything from individual job files
export * from './processEmailsJob.js' export * from './processEmailsTask.js'
export * from './sendEmailTask.js' export * from './sendEmailTask.js'

View File

@@ -2,18 +2,18 @@ import type { PayloadRequest, Payload } from 'payload'
import type { MailingService } from '../services/MailingService.js' import type { MailingService } from '../services/MailingService.js'
/** /**
* Data passed to the process emails job * Data passed to the process emails task
*/ */
export interface ProcessEmailsJobData { export interface ProcessEmailsTaskData {
// Currently no data needed - always processes both pending and failed emails // Currently no data needed - always processes both pending and failed emails
} }
/** /**
* Handler function for processing emails * Handler function for processing emails
* Used internally by the job definition * Used internally by the task definition
*/ */
export const processEmailsJobHandler = async ( export const processEmailsTaskHandler = async (
job: { data: ProcessEmailsJobData }, job: { data: ProcessEmailsTaskData },
context: { req: PayloadRequest; mailingService: MailingService } context: { req: PayloadRequest; mailingService: MailingService }
) => { ) => {
const { mailingService } = context const { mailingService } = context
@@ -35,10 +35,10 @@ export const processEmailsJobHandler = async (
} }
/** /**
* Job definition for processing emails * Task definition for processing emails
* This is what gets registered with Payload's job system * This is what gets registered with Payload's job system
*/ */
export const processEmailsJob = { export const processEmailsTask = {
slug: 'process-emails', slug: 'process-emails',
handler: async ({ job, req }: { job: any; req: any }) => { handler: async ({ job, req }: { job: any; req: any }) => {
// Get mailing context from payload // Get mailing context from payload
@@ -50,8 +50,8 @@ export const processEmailsJob = {
} }
// Use the existing mailing service from context // Use the existing mailing service from context
await processEmailsJobHandler( await processEmailsTaskHandler(
job as { data: ProcessEmailsJobData }, job as { data: ProcessEmailsTaskData },
{ req, mailingService: mailingContext.service } { req, mailingService: mailingContext.service }
) )
@@ -62,9 +62,12 @@ export const processEmailsJob = {
} }
} }
}, },
interfaceName: 'ProcessEmailsJob', interfaceName: 'ProcessEmailsTask',
} }
// For backward compatibility, export as processEmailsJob
export const processEmailsJob = processEmailsTask
/** /**
* Helper function to schedule an email processing job * Helper function to schedule an email processing job
* Used by the plugin during initialization and can be used by developers * Used by the plugin during initialization and can be used by developers
@@ -82,7 +85,7 @@ export const scheduleEmailsJob = async (
try { try {
await payload.jobs.queue({ await payload.jobs.queue({
queue: queueName, queue: queueName,
workflow: 'process-emails', task: 'process-emails',
input: {}, input: {},
waitUntil: delay ? new Date(Date.now() + delay) : undefined, waitUntil: delay ? new Date(Date.now() + delay) : undefined,
} as any) } as any)

View File

@@ -280,11 +280,7 @@ export const sendEmailWorkflow = {
} }
if (error instanceof Error) { if (error instanceof Error) {
// Preserve original error and stack trace throw new Error(`Failed to process email: ${error.message}`, { cause: error })
const wrappedError = new Error(`Failed to process email: ${error.message}`)
wrappedError.stack = error.stack
wrappedError.cause = error
throw wrappedError
} else { } else {
throw new Error(`Failed to process email: ${String(error)}`) throw new Error(`Failed to process email: ${String(error)}`)
} }