Skip to content

fix(den-api): surface invite email failures instead of silently dropping - #1483

Merged
benjaminshafii merged 3 commits into
devfrom
task/invite-email-surface-failures
Apr 18, 2026
Merged

benjaminshafii merged 3 commits into
devfrom
task/invite-email-surface-failures

Conversation

@benjaminshafii

Copy link
Copy Markdown
Member

Summary

Traces an issue Ben hit this week: he invited Jan and Omar to OpenWork Cloud; Jan received the email, Omar never did. Resending changed nothing and the UI kept showing Omar as pending. The Slack hypothesis was that invites to an existing user silently fail.

An audit (notes in the commit) shows there is no existing-user branch anywhere on the invite path. The actual bug is that sendDenOrganizationInvitationEmail in ee/apps/den-api/src/email.ts caught every Loops failure with console.warn and returned cleanly, so POST /v1/orgs/:orgId/invitations always returned 201 Created. The DB row was persisted, the UI showed it as pending, and nobody noticed the email provider had rejected the send.

This PR stops swallowing the failure.

Changes

  • Introduce a typed DenEmailSendError with a stable reason tagged union (loops_not_configured | loops_rejected | loops_network) plus template, recipient, and optional detail.
  • sendDenOrganizationInvitationEmail and sendDenVerificationEmail now throw DenEmailSendError instead of console-warning and swallowing. The dev-mode short-circuit is preserved — OPENWORK_DEV_MODE=1 still logs the payload and returns cleanly, so local dev and CI are unaffected.
  • POST /v1/orgs/:orgId/invitations catches DenEmailSendError, logs via console.error with a stable [auth][invite_email_failed] organization=… invitation=… email=… reason=… detail=… prefix (greppable across deployments), and returns 502 invitation_email_failed with a human-readable message plus the invitationId so the UI can correlate and retry. The invitation row is intentionally left pending so the next submit becomes a real resend.
  • Documented the 502 response in describeRoute.
  • Added ee/apps/den-api/scripts/smoke-email-failures.mjs so a reviewer can verify both failure paths without a real Loops account.

Operator note: sendDenVerificationEmail now throws on Loops failure too. Previously, a missing LOOPS_TRANSACTIONAL_ID_DEN_VERIFY_EMAIL would silently drop the OTP and leave users stuck at the verification screen forever. It will now return a real error upstream — that's intentional.

Testing

All runs performed against `packaging/docker/den-dev-up.sh` at commit $(git -C _repos/openwork-worktrees/invite-email-surface-failures rev-parse --short HEAD) with the den-api stack rebuilt.

1. Happy path (dev mode on, `OPENWORK_DEV_MODE=1`)

Signed up `[email protected]`, created org "Invite Test Org", invited `[email protected]`. UI flipped the Invitations tab counter to 1. `docker logs` showed:

```
[auth] dev organization invite email payload for [email protected]: {...}
```

No regression from the pre-refactor behaviour.

2. loops_not_configured path (dev mode off, Loops env unset)

```bash
docker cp ee/apps/den-api/scripts/smoke-email-failures.mjs :/app/ee/apps/den-api/scripts/smoke-email-failures.mjs
docker exec -e OPENWORK_DEV_MODE=0 node /app/ee/apps/den-api/scripts/smoke-email-failures.mjs [email protected]
```

Output:

```
[smoke] ok loops_not_configured {
reason: 'loops_not_configured',
template: 'organization_invite',
recipient: '[email protected]',
detail: undefined
}
```

3. loops_rejected path (dev mode off, Loops env set to bogus values)

```bash
docker exec -e OPENWORK_DEV_MODE=0 -e LOOPS_API_KEY=bogus \
-e LOOPS_TRANSACTIONAL_ID_DEN_ORG_INVITE_EMAIL=bogus \
node /app/ee/apps/den-api/scripts/smoke-email-failures.mjs [email protected]
```

Output:

```
[smoke] ok loops_rejected {
reason: 'loops_rejected',
template: 'organization_invite',
recipient: '[email protected]',
detail: 'Invalid API key'
}
```

`detail` is the real message from Loops, so both the server log and the UI `message` field will surface it.

4. What I did not run end-to-end in Chrome MCP

I did not drive the 502 response through the browser. Flipping `OPENWORK_DEV_MODE=0` inside the Den dev stack breaks the Better Auth cookie flow (401 on `/v1/me/orgs` once dev mode is off), which is a pre-existing dev-stack-only issue unrelated to this PR. The end-to-end UI verification for production traffic is:

  1. Deploy this branch to a preview env with real Loops creds.
  2. Invite a recipient whose email is not on the Loops transactional allowlist.
  3. The Members form should surface the returned `message` (e.g. "The invitation email provider rejected the send: ") on the page-error banner, instead of silently succeeding.

How this helps the original Omar/Jan incident

The next time an invite silently fails in prod, operators now get:

  1. A greppable `[auth][invite_email_failed]` line with the org id, invitation id, recipient, and Loops reason in the den-api logs.
  2. A real HTTP 502 at the call site, so the inviter sees an error in the UI instead of the invitation appearing to "just work".

Whether the original trigger for Omar was a Loops recipient-allowlist, a domain-block from a prior bounce, or a transactional-id typo, any of those will now be diagnosable immediately.

Related

Out of scope

  • Adding a `lastSendError` / `lastSendAttemptAt` column to `InvitationTable` to persist send state. Worth a follow-up but out of scope here.
  • Retrying failed sends via a background job.
  • Unifying the Better Auth `sendInvitationEmail` callback with the custom POST route (the Better Auth callback is currently wired but unused by the UI).

Benjamin Shafii added 3 commits April 17, 2026 16:31
…owing

Loops failures in sendDenOrganizationInvitationEmail and
sendDenVerificationEmail were being caught and logged at warn level,
so the HTTP handlers still returned 201 'Invitation created' even when
no email ever left the process. Ben observed this with two live
invitations: the DB row was pending, the UI showed it, but one of two
recipients never received the email and clicking resend re-ran the same
silent-failure path.

Root cause (from an explore audit):
- email.ts:129,132 swallowed non-2xx Loops responses and fetch throws.
- invitations.ts awaited the send and unconditionally returned 2xx.
- There is no 'skip email if user already exists' branch anywhere;
  the Slack hypothesis was wrong. The failure mode is provider-side and
  was invisible because of the swallow.

Changes:
- Introduce DenEmailSendError with a stable reason tagged union
  (loops_not_configured | loops_rejected | loops_network).
- sendDenOrganizationInvitationEmail and sendDenVerificationEmail now
  throw DenEmailSendError on failure. Dev-mode short-circuit is
  preserved (still logs the payload and returns cleanly).
- POST /v1/orgs/:orgId/invitations catches DenEmailSendError, logs via
  console.error with a stable [auth][invite_email_failed] prefix
  (greppable across deployments), and returns 502
  invitation_email_failed with a human-readable message and the
  invitationId so the UI can correlate and offer a retry. The row is
  left pending intentionally so the next submit becomes a real resend.
- Document the 502 response in the OpenAPI describeRoute.

Operator note: if LOOPS_TRANSACTIONAL_ID_DEN_VERIFY_EMAIL is unset the
signup OTP endpoint will now return a real error instead of silently
stranding the user at the OTP screen forever. This is intentional; the
previous behavior was a latent signup-breaking bug.
PR #1476 introduced a build step that reads apps/desktop/package.json to
bake in a default latest-app-version, but packaging/docker/Dockerfile.den
does not ship the Tauri desktop sources. As a result, the den-dev Docker
stack fails to build after the PR landed. Gracefully fall back to 0.0.0
(matching the runtime default) when the file is absent, and allow a
DEN_API_LATEST_APP_VERSION env override so deployers can still pin a
real value.
scripts/smoke-email-failures.mjs exercises the DenEmailSendError paths
against the built dist/ of den-api. Ships with instructions so a
reviewer can rerun it inside the docker-compose den-dev container with
a single command.

Also parameterises OPENWORK_DEV_MODE in the den compose service so the
failure paths can be reached from outside the container when needed
(defaults to 1; override with OPENWORK_DEV_MODE=0 at compose time).
@vercel

vercel Bot commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
openwork-app Ready Ready Preview, Comment Apr 17, 2026 11:53pm
openwork-den Ready Ready Preview, Comment Apr 17, 2026 11:53pm
openwork-den-worker-proxy Ready Ready Preview, Comment Apr 17, 2026 11:53pm
openwork-landing Ready Ready Preview, Comment, Open in v0 Apr 17, 2026 11:53pm
openwork-share Ready Ready Preview, Comment Apr 17, 2026 11:53pm

@benjaminshafii
benjaminshafii merged commit 0002a8e into dev Apr 18, 2026
8 checks passed
benjaminshafii pushed a commit that referenced this pull request May 15, 2026
…ing (#1483)

* fix(den-api): surface invitation email send failures instead of swallowing

Loops failures in sendDenOrganizationInvitationEmail and
sendDenVerificationEmail were being caught and logged at warn level,
so the HTTP handlers still returned 201 'Invitation created' even when
no email ever left the process. Ben observed this with two live
invitations: the DB row was pending, the UI showed it, but one of two
recipients never received the email and clicking resend re-ran the same
silent-failure path.

Root cause (from an explore audit):
- email.ts:129,132 swallowed non-2xx Loops responses and fetch throws.
- invitations.ts awaited the send and unconditionally returned 2xx.
- There is no 'skip email if user already exists' branch anywhere;
  the Slack hypothesis was wrong. The failure mode is provider-side and
  was invisible because of the swallow.

Changes:
- Introduce DenEmailSendError with a stable reason tagged union
  (loops_not_configured | loops_rejected | loops_network).
- sendDenOrganizationInvitationEmail and sendDenVerificationEmail now
  throw DenEmailSendError on failure. Dev-mode short-circuit is
  preserved (still logs the payload and returns cleanly).
- POST /v1/orgs/:orgId/invitations catches DenEmailSendError, logs via
  console.error with a stable [auth][invite_email_failed] prefix
  (greppable across deployments), and returns 502
  invitation_email_failed with a human-readable message and the
  invitationId so the UI can correlate and offer a retry. The row is
  left pending intentionally so the next submit becomes a real resend.
- Document the 502 response in the OpenAPI describeRoute.

Operator note: if LOOPS_TRANSACTIONAL_ID_DEN_VERIFY_EMAIL is unset the
signup OTP endpoint will now return a real error instead of silently
stranding the user at the OTP screen forever. This is intentional; the
previous behavior was a latent signup-breaking bug.

* fix(den-api): tolerate missing apps/desktop/package.json in Docker build

PR #1476 introduced a build step that reads apps/desktop/package.json to
bake in a default latest-app-version, but packaging/docker/Dockerfile.den
does not ship the Tauri desktop sources. As a result, the den-dev Docker
stack fails to build after the PR landed. Gracefully fall back to 0.0.0
(matching the runtime default) when the file is absent, and allow a
DEN_API_LATEST_APP_VERSION env override so deployers can still pin a
real value.

* test(den-api): add smoke script for invite email failure paths

scripts/smoke-email-failures.mjs exercises the DenEmailSendError paths
against the built dist/ of den-api. Ships with instructions so a
reviewer can rerun it inside the docker-compose den-dev container with
a single command.

Also parameterises OPENWORK_DEV_MODE in the den compose service so the
failure paths can be reached from outside the container when needed
(defaults to 1; override with OPENWORK_DEV_MODE=0 at compose time).
benjaminshafii pushed a commit that referenced this pull request May 15, 2026
…ing (#1483)

* fix(den-api): surface invitation email send failures instead of swallowing

Loops failures in sendDenOrganizationInvitationEmail and
sendDenVerificationEmail were being caught and logged at warn level,
so the HTTP handlers still returned 201 'Invitation created' even when
no email ever left the process. Ben observed this with two live
invitations: the DB row was pending, the UI showed it, but one of two
recipients never received the email and clicking resend re-ran the same
silent-failure path.

Root cause (from an explore audit):
- email.ts:129,132 swallowed non-2xx Loops responses and fetch throws.
- invitations.ts awaited the send and unconditionally returned 2xx.
- There is no 'skip email if user already exists' branch anywhere;
  the Slack hypothesis was wrong. The failure mode is provider-side and
  was invisible because of the swallow.

Changes:
- Introduce DenEmailSendError with a stable reason tagged union
  (loops_not_configured | loops_rejected | loops_network).
- sendDenOrganizationInvitationEmail and sendDenVerificationEmail now
  throw DenEmailSendError on failure. Dev-mode short-circuit is
  preserved (still logs the payload and returns cleanly).
- POST /v1/orgs/:orgId/invitations catches DenEmailSendError, logs via
  console.error with a stable [auth][invite_email_failed] prefix
  (greppable across deployments), and returns 502
  invitation_email_failed with a human-readable message and the
  invitationId so the UI can correlate and offer a retry. The row is
  left pending intentionally so the next submit becomes a real resend.
- Document the 502 response in the OpenAPI describeRoute.

Operator note: if LOOPS_TRANSACTIONAL_ID_DEN_VERIFY_EMAIL is unset the
signup OTP endpoint will now return a real error instead of silently
stranding the user at the OTP screen forever. This is intentional; the
previous behavior was a latent signup-breaking bug.

* fix(den-api): tolerate missing apps/desktop/package.json in Docker build

PR #1476 introduced a build step that reads apps/desktop/package.json to
bake in a default latest-app-version, but packaging/docker/Dockerfile.den
does not ship the Tauri desktop sources. As a result, the den-dev Docker
stack fails to build after the PR landed. Gracefully fall back to 0.0.0
(matching the runtime default) when the file is absent, and allow a
DEN_API_LATEST_APP_VERSION env override so deployers can still pin a
real value.

* test(den-api): add smoke script for invite email failure paths

scripts/smoke-email-failures.mjs exercises the DenEmailSendError paths
against the built dist/ of den-api. Ships with instructions so a
reviewer can rerun it inside the docker-compose den-dev container with
a single command.

Also parameterises OPENWORK_DEV_MODE in the den compose service so the
failure paths can be reached from outside the container when needed
(defaults to 1; override with OPENWORK_DEV_MODE=0 at compose time).
@benjaminshafii
benjaminshafii deleted the task/invite-email-surface-failures branch May 15, 2026 17:38

This branch was successfully deployed

5 active deployments
Preview – openwork-den-worker-proxy — ba3a5045 Deployed Apr 17, 2026 by vercel[bot]
Preview – openwork-den — ba3a5045 Deployed Apr 17, 2026 by vercel[bot]
Preview – openwork-landing — ba3a5045 Deployed Apr 17, 2026 by vercel[bot]
Preview – openwork-app — ba3a5045 Deployed Apr 17, 2026 by vercel[bot]
Preview – openwork-share — ba3a5045 Deployed Apr 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant