calcom/cal.diy · error · BadRequestException

Could not add this apple calendar account: ${reason}

Error message

Could not add this apple calendar account: ${reason}

What it means

Thrown by AppleCalendarService.saveCalendarCredentials (apple-calendar.service.ts:134) as BadRequestException (HTTP 400) from a catch-all around BuildCalendarService + dav.listCalendars() + upsert. The caught error is stringified into the message via template literal (`${reason}`), which discards the original stack/cause and can leak internal detail. The '${reason}' segment usually contains the real underlying error text.

Source

Thrown at apps/api/v2/src/platform/calendars/services/apple-calendar.service.ts:134

          process.env.CALENDSO_ENCRYPTION_KEY || ""
        ),
        userId: userId,
        teamId: null,
        appId: APPLE_CALENDAR_ID,
        invalid: false,
        delegationCredentialId: null,
        encryptedKey: null,
      };

      const dav = BuildCalendarService({
        id: 0,
        ...data,
        user: { email: userEmail },
      });
      await dav?.listCalendars();
      await this.credentialRepository.upsertUserAppCredential(APPLE_CALENDAR_TYPE, data.key, userId);
    } catch (reason) {
      throw new BadRequestException(`Could not add this apple calendar account: ${reason}`);
    }

    return {
      status: SUCCESS_STATUS,
    };
  }
}

View on GitHub (pinned to 176037d0af)

Solutions

  1. Read the text after 'Could not add this apple calendar account: ' in the 400 body — it carries the underlying error message.
  2. Verify process.env.CALENDSO_ENCRYPTION_KEY is set and identical across all instances (encryption key rotation breaks old credentials).
  3. Test the supplied credentials directly against Apple's CalDAV endpoint to isolate provider vs. app error.
  4. If reason mentions 'cannot read property' or null service, confirm the apple_calendar app is installed/registered in BuildCalendarService's registry.

Example fix

// before (service): swallows original error
} catch (reason) {
  throw new BadRequestException(`Could not add this apple calendar account: ${reason}`);
}

// after: log stack and preserve cause for diagnosability
} catch (reason) {
  this.logger?.error?.('apple save failed', reason);
  throw new BadRequestException(`Could not add this apple calendar account: ${(reason as Error).message}`, { cause: reason });
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-flight: ensure encryption key is set and credentials look valid
if (!process.env.CALENDSO_ENCRYPTION_KEY) {
  throw new Error('CALENDSO_ENCRYPTION_KEY is not set — Apple save will fail.');
}
// Validate input before calling save (see error 364 validation)

Type guard

function isEncryptionKeyConfigured() {
  return typeof process.env.CALENDSO_ENCRYPTION_KEY === 'string'
    && process.env.CALENDSO_ENCRYPTION_KEY.length > 0;
}

Try / catch

try {
  await api.post('/v2/calendars/apple_calendar/save', { username, password });
} catch (e) {
  const reason = e.response?.data?.message?.replace(/.*:\s*/, ''); // text after the colon
  if (/encrypt|key/i.test(reason)) {
    throw new ConfigError('CALENDSO_ENCRYPTION_KEY issue — contact admin');
  }
  if (/auth|credential|unauthorized/i.test(reason)) {
    throw new UserError('Apple credentials are incorrect — re-enter them.');
  }
  throw e;
}

Prevention

When it happens

Trigger: Wrong Apple credentials causing DAV auth failure; CALENDSO_ENCRYPTION_KEY unset/mismatched so symmetricEncrypt/symmetricDecrypt misbehave; Apple CalDAV endpoint unreachable; BuildCalendarService returns a service whose listCalendars() rejects.

Common situations: Missing or rotated CALENDSO_ENCRYPTION_KEY env var across deploys; user supplied wrong app-specific password; network egress blocked to caldav.apple.com; apple_calendar app not registered so BuildCalendarService yields a failing service.

Related errors


AI-assisted analysis of calcom/cal.diy@176037d0af (2026-08-12). Data as JSON: /api/errors/faadafb4c608344e. Report an issue: GitHub.