Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: drop redundant try/catch from contacts import route (#5676)

Adam Eivy8ee2f775ecdb
diff --git a/server/routes/contacts.js b/server/routes/contacts.js
index 03068195a6..549eacd74a 100644
--- a/server/routes/contacts.js
+++ b/server/routes/contacts.js
@@ -1,7 +1,7 @@
import { Router } from 'express';
import { z } from 'zod';
-import { asyncHandler, ServerError } from '../lib/errorHandler.js';
+import { asyncHandler } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import * as contactsSync from '../services/contactsSync.js';
import * as identityResolve from '../services/identityResolve.js';
@@ -84,15 +84,8 @@ const importBodySchema = z.object({
router.post('/import-to-tribe', asyncHandler(async (req, res) => {
const body = validateRequest(importBodySchema, req.body || {});
- try {
- const result = await tribeContacts.importContactToTribe(body);
- res.status(result.created ? 201 : 200).json(result);
- } catch (err) {
- if (err?.status === 400) {
- throw new ServerError(err.message, { status: 400, code: err.code || 'BAD_REQUEST' });
- }
- throw err;
- }
+ const result = await tribeContacts.importContactToTribe(body);
+ res.status(result.created ? 201 : 200).json(result);
}));
export default router;
diff --git a/server/routes/contacts.test.js b/server/routes/contacts.test.js
index c0fcbd93d2..670bcd419a 100644
--- a/server/routes/contacts.test.js
+++ b/server/routes/contacts.test.js
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import express from 'express';
import { request } from '../lib/testHelper.js';
-import { errorMiddleware } from '../lib/errorHandler.js';
+import { errorMiddleware, ServerError } from '../lib/errorHandler.js';
vi.mock('../services/contactsSync.js', () => ({
checkSetup: vi.fn(async () => ({ ok: true, sourceCount: 2, rawContactRows: 100 })),
@@ -22,6 +22,7 @@ vi.mock('../services/tribeContacts.js', () => ({
}));
const { default: router } = await import('./contacts.js');
+const tribeContacts = await import('../services/tribeContacts.js');
function makeApp() {
const app = express();
@@ -70,4 +71,24 @@ describe('contacts routes (#2415)', () => {
expect(res.status).toBe(201);
expect(res.body.created).toBe(true);
});
+
+ // #5676: the route no longer re-wraps service errors — the shared middleware
+ // normalizes them. Pin the envelope so dropping that catch stays invisible
+ // to callers.
+ it('POST /import-to-tribe surfaces a service ServerError as its own 400 envelope', async () => {
+ tribeContacts.importContactToTribe.mockRejectedValueOnce(
+ new ServerError('name is required', { status: 400, code: 'BAD_REQUEST' }),
+ );
+ const res = await request(makeApp()).post('/api/contacts/import-to-tribe').send({ contactId: 'c1' });
+ expect(res.status).toBe(400);
+ expect(res.body.error).toBe('name is required');
+ expect(res.body.code).toBe('BAD_REQUEST');
+ });
+
+ it('POST /import-to-tribe maps an unexpected service throw to a 500 envelope', async () => {
+ tribeContacts.importContactToTribe.mockRejectedValueOnce(new Error('address book exploded'));
+ const res = await request(makeApp()).post('/api/contacts/import-to-tribe').send({ contactId: 'c1' });
+ expect(res.status).toBe(500);
+ expect(res.body.code).toBe('INTERNAL_ERROR');
+ });
});
diff --git a/server/services/tribeContacts.js b/server/services/tribeContacts.js
index 85f056ca56..d134f649e6 100644
--- a/server/services/tribeContacts.js
+++ b/server/services/tribeContacts.js
@@ -15,6 +15,7 @@ import {
normalizePhone,
identityFromHandle,
} from '../lib/tribeMatch.js';
+import { ServerError } from '../lib/errorHandler.js';
import { loadContactIndex } from './contactsSync.js';
import * as tribe from './tribe.js';
import * as humanActivity from './humanActivity.js';
@@ -198,10 +199,7 @@ export async function importContactToTribe({
}
const personName = String(name || contact?.displayName || organization || contact?.organization || '').trim();
if (!personName) {
- const err = new Error('name is required');
- err.status = 400;
- err.code = 'BAD_REQUEST';
- throw err;
+ throw new ServerError('name is required', { status: 400, code: 'BAD_REQUEST' });
}
const personPhones = phones || contact?.phones || [];
const personEmails = emails || contact?.emails || [];