hcengineering/platform · error

Invalid email address

Error message

Invalid email address

What it means

getDomainFromEmail extracts the domain substring after the last '@' in an email. If the string contains no '@' at all (atIndex === -1), it throws this Error because the input is not a usable email address and no domain can be derived.

Source

Thrown at services/mail/mail-common/src/utils.ts:243

  if (messageId !== undefined) {
    headers[HulyMessageIdHeader] = messageId
  }
  return headers
}

export function isHulyMessage (headers: string[]): boolean {
  return headers.some(
    (header) =>
      header.startsWith(HulyMailHeader) ||
      header.startsWith(HulyMessageIdHeader) ||
      header.startsWith(HulyMessageTypeHeader)
  )
}

export function getDomainFromEmail (email: string): string {
  const atIndex = email.lastIndexOf('@')
  if (atIndex === -1) {
    throw new Error('Invalid email address')
  }
  return email.substring(atIndex + 1)
}

export function getEmailMessageIdFromHulyId (hulyId: string | undefined, email: string): string {
  const domain = getDomainFromEmail(email)
  const id = hulyId ?? generateMessageId()
  return `<${id}@${domain}>`
}

export function getHulyIdFromEmailMessageId (messageId: string, email: string): MessageID | undefined {
  const domain = getDomainFromEmail(email)

  const cleanMessageId = messageId.replace(/^<|>$/g, '')

  const domainSuffix = `@${domain}`
  if (!cleanMessageId.endsWith(domainSuffix)) {
    return undefined

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Validate the email contains '@' before calling, e.g. email.includes('@') or a regex check.
  2. Fix upstream header parsing so the extracted address includes its domain part.
  3. Wrap the call in try/catch and treat the message as unprocessable rather than crashing ingestion.
  4. Trim/clean the address (remove angle brackets like <user@host>) before domain extraction.

Example fix

// before
const domain = getDomainFromEmail(fromHeader)
// after
const addr = fromHeader.match(/<([^>]+)>/)?.[1] ?? fromHeader
if (!addr.includes('@')) throw new Error(`Bad address: ${addr}`)
const domain = getDomainFromEmail(addr)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof email !== 'string' || !email.includes('@')) {
  throw new Error(`Cannot extract domain from invalid email: ${email}`)
}

Type guard

function isEmailAddress(s: unknown): s is string {
  return typeof s === 'string' && s.includes('@') && s.lastIndexOf('@') < s.length - 1
}

Try / catch

try {
  domain = getDomainFromEmail(addr)
} catch (err) {
  if (err.message === 'Invalid email address') {
    console.warn(`Unprocessable address "${addr}", skipping`)
    domain = null
  } else throw err
}

Prevention

When it happens

Trigger: Calling getDomainFromEmail (directly or viagetEmailMessageIdFromHulyId/domain helpers) with a string lacking '@' — e.g. a bare username, empty string, a display name, or an 'undisclosed-recipients' placeholder.

Common situations: Parsing inbound mail with malformed From/To headers; addresses stripped of their domain by upstream parsing; placeholder addresses like 'undisclosed-recipients:;' in mailing-list traffic; user-typed emails with typos ('userexample.com').

Related errors


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