hcengineering/platform · error · Error

Notification context id is required

Error message

Notification context id is required

What it means

NotificationContextProcessor.create resolves the context id from event.contextId or the optional id parameter and throws this Error if both are null/undefined. A NotificationContext cannot be constructed without a stable identity.

Source

Thrown at foundations/communication/packages/shared/src/processor.ts:82

      language: event.language,
      reactions: {},
      attachments: [],
      threads: []
    }
  }

  static applyPatch (message: Message, patchEvent: PatchEvent): Message | undefined {
    return applyPatchEvent(message, patchEvent)
  }
}

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class NotificationContextProcessor {
  static create (event: CreateNotificationContextEvent, id?: ContextID): NotificationContext {
    const contextId: ContextID | undefined = event.contextId ?? id

    if (contextId == null) {
      throw new Error('Notification context id is required')
    }
    return {
      id: contextId,
      cardId: event.cardId,
      account: event.account,
      lastView: event.lastView,
      lastUpdate: event.lastUpdate,
      lastNotify: event.lastNotify,
      notifications: withTotal([] as Notification[])
    }
  }

  static update (context: NotificationContext, event: UpdateNotificationContextEvent): NotificationContext {
    if (context.account !== event.account || context.id !== event.contextId) return context
    return {
      ...context,
      lastView: event.updates.lastView ?? context.lastView,
      lastUpdate: event.updates.lastUpdate ?? context.lastUpdate,

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Provide event.contextId when constructing the event.
  2. Pass the id explicitly as the second argument: NotificationContextProcessor.create(event, contextId).
  3. Generate a context id (UUID) at creation time if the event legitimately lacks one.
  4. Add ingestion validation so events missing contextId are rejected/reported at the boundary instead of deep in the processor.

Example fix

// before
const ctx = NotificationContextProcessor.create({ cardId, account } as CreateNotificationContextEvent)
// Error: Notification context id is required
// after
const ctx = NotificationContextProcessor.create({ cardId, account, contextId: crypto.randomUUID() } as CreateNotificationContextEvent)
Defensive patterns

Strategy: type-guard

Validate before calling

function assertContextId(event: CreateNotificationContextEvent, id?: ContextID): ContextID {
  const cid = event.contextId ?? id
  if (cid == null) throw new Error('Provide event.contextId or an explicit id to NotificationContextProcessor.create')
  return cid
}

Type guard

function hasContextId(event: CreateNotificationContextEvent, id?: ContextID): event is CreateNotificationContextEvent & { contextId: ContextID } {
  return (event.contextId ?? id) != null
}

Try / catch

try {
  const ctx = NotificationContextProcessor.create(event, id)
} catch (e) {
  if (e.message === 'Notification context id is required') {
    return NotificationContextProcessor.create({ ...event, contextId: crypto.randomUUID() }, id)
  }
  throw e
}

Prevention

When it happens

Trigger: Calling NotificationContextProcessor.create(event) where event.contextId is null/undefined and no `id` argument is provided.

Common situations: Creating notification contexts from client events that omit contextId; passing an undefined id variable at runtime despite TypeScript typing; deserialization dropping undefined fields; fixtures for tests missing contextId.

Understand the failure class

Background: "Missing required field" and "field is required" errors: why libraries reject payloads that omit mandatory fields — this error's family across 20 libraries.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/763adefc711f66f3. Report an issue: GitHub.