Skip to content

Commit 42646a6

Browse files
authored
Merge pull request #7 from supermemoryai/01-01-fix_consistent_error_handling_across_all_client_methods
fix: consistent error handling across all client methods
2 parents 177ba8b + 74e1cca commit 42646a6

3 files changed

Lines changed: 82 additions & 43 deletions

File tree

src/index.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,25 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
9898
if (isFirstMessage) {
9999
injectedSessions.add(input.sessionID);
100100

101-
const [profile, userMemories, projectMemoriesList] = await Promise.all([
101+
const [profileResult, userMemoriesResult, projectMemoriesListResult] = await Promise.all([
102102
supermemoryClient.getProfile(tags.user, userMessage),
103103
supermemoryClient.searchMemories(userMessage, tags.user),
104104
supermemoryClient.listMemories(tags.project, CONFIG.maxProjectMemories),
105105
]);
106106

107+
const profile = profileResult.success ? profileResult : null;
108+
const userMemories = userMemoriesResult.success ? userMemoriesResult : { results: [] };
109+
const projectMemoriesList = projectMemoriesListResult.success ? projectMemoriesListResult : { memories: [] };
110+
107111
const projectMemories = {
108-
results: (projectMemoriesList?.memories || []).map((m: any) => ({
112+
results: (projectMemoriesList.memories || []).map((m: any) => ({
109113
id: m.id,
110114
memory: m.summary,
111115
similarity: 1,
112116
title: m.title,
113117
metadata: m.metadata,
114118
})),
115-
total: projectMemoriesList?.memories?.length || 0,
119+
total: projectMemoriesList.memories?.length || 0,
116120
timing: 0,
117121
};
118122

@@ -264,10 +268,10 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
264268
{ type: args.type }
265269
);
266270

267-
if (!result) {
271+
if (!result.success) {
268272
return JSON.stringify({
269273
success: false,
270-
error: "Failed to add memory",
274+
error: result.error || "Failed to add memory",
271275
});
272276
}
273277

@@ -291,32 +295,51 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
291295
const scope = args.scope;
292296

293297
if (scope === "user") {
294-
const results = await supermemoryClient.searchMemories(
298+
const result = await supermemoryClient.searchMemories(
295299
args.query,
296300
tags.user
297301
);
298-
return formatSearchResults(args.query, scope, results, args.limit);
302+
if (!result.success) {
303+
return JSON.stringify({
304+
success: false,
305+
error: result.error || "Failed to search memories",
306+
});
307+
}
308+
return formatSearchResults(args.query, scope, result, args.limit);
299309
}
300310

301311
if (scope === "project") {
302-
const results = await supermemoryClient.searchMemories(
312+
const result = await supermemoryClient.searchMemories(
303313
args.query,
304314
tags.project
305315
);
306-
return formatSearchResults(args.query, scope, results, args.limit);
316+
if (!result.success) {
317+
return JSON.stringify({
318+
success: false,
319+
error: result.error || "Failed to search memories",
320+
});
321+
}
322+
return formatSearchResults(args.query, scope, result, args.limit);
307323
}
308324

309-
const [userResults, projectResults] = await Promise.all([
325+
const [userResult, projectResult] = await Promise.all([
310326
supermemoryClient.searchMemories(args.query, tags.user),
311327
supermemoryClient.searchMemories(args.query, tags.project),
312328
]);
313329

330+
if (!userResult.success || !projectResult.success) {
331+
return JSON.stringify({
332+
success: false,
333+
error: userResult.error || projectResult.error || "Failed to search memories",
334+
});
335+
}
336+
314337
const combined = [
315-
...(userResults.results || []).map((r) => ({
338+
...(userResult.results || []).map((r) => ({
316339
...r,
317340
scope: "user" as const,
318341
})),
319-
...(projectResults.results || []).map((r) => ({
342+
...(projectResult.results || []).map((r) => ({
320343
...r,
321344
scope: "project" as const,
322345
})),
@@ -336,23 +359,23 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
336359
}
337360

338361
case "profile": {
339-
const profile = await supermemoryClient.getProfile(
362+
const result = await supermemoryClient.getProfile(
340363
tags.user,
341364
args.query
342365
);
343366

344-
if (!profile) {
367+
if (!result.success) {
345368
return JSON.stringify({
346369
success: false,
347-
error: "Failed to fetch profile",
370+
error: result.error || "Failed to fetch profile",
348371
});
349372
}
350373

351374
return JSON.stringify({
352375
success: true,
353376
profile: {
354-
static: profile.profile?.static || [],
355-
dynamic: profile.profile?.dynamic || [],
377+
static: result.profile?.static || [],
378+
dynamic: result.profile?.dynamic || [],
356379
},
357380
});
358381
}
@@ -368,6 +391,13 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
368391
limit
369392
);
370393

394+
if (!result.success) {
395+
return JSON.stringify({
396+
success: false,
397+
error: result.error || "Failed to list memories",
398+
});
399+
}
400+
371401
const memories = result.memories || [];
372402
return JSON.stringify({
373403
success: true,
@@ -396,10 +426,10 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
396426
args.memoryId
397427
);
398428

399-
if (!result) {
429+
if (!result.success) {
400430
return JSON.stringify({
401431
success: false,
402-
error: "Failed to delete memory",
432+
error: result.error || "Failed to delete memory",
403433
});
404434
}
405435

src/services/client.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ export class SupermemoryClient {
5151
TIMEOUT_MS
5252
);
5353
log("searchMemories: success", { count: result.results?.length || 0 });
54-
return result;
54+
return { success: true as const, ...result };
5555
} catch (error) {
56-
log("searchMemories: error", { error: String(error) });
57-
console.error("Supermemory: search failed", error);
58-
return { results: [], total: 0, timing: 0 };
56+
const errorMessage = error instanceof Error ? error.message : String(error);
57+
log("searchMemories: error", { error: errorMessage });
58+
return { success: false as const, error: errorMessage, results: [], total: 0, timing: 0 };
5959
}
6060
}
6161

@@ -70,11 +70,11 @@ export class SupermemoryClient {
7070
TIMEOUT_MS
7171
);
7272
log("getProfile: success", { hasProfile: !!result?.profile });
73-
return result;
73+
return { success: true as const, ...result };
7474
} catch (error) {
75-
log("getProfile: error", { error: String(error) });
76-
console.error("Supermemory: profile fetch failed", error);
77-
return null;
75+
const errorMessage = error instanceof Error ? error.message : String(error);
76+
log("getProfile: error", { error: errorMessage });
77+
return { success: false as const, error: errorMessage, profile: null };
7878
}
7979
}
8080

@@ -83,31 +83,38 @@ export class SupermemoryClient {
8383
containerTag: string,
8484
metadata?: { type?: MemoryType; tool?: string; [key: string]: unknown }
8585
) {
86+
log("addMemory: start", { containerTag, contentLength: content.length });
8687
try {
87-
return await withTimeout(
88+
const result = await withTimeout(
8889
this.getClient().memories.add({
8990
content,
9091
containerTag,
9192
metadata: metadata as Record<string, string | number | boolean | string[]>,
9293
}),
9394
TIMEOUT_MS
9495
);
96+
log("addMemory: success", { id: result.id });
97+
return { success: true as const, ...result };
9598
} catch (error) {
96-
console.error("Supermemory: add memory failed", error);
97-
return null;
99+
const errorMessage = error instanceof Error ? error.message : String(error);
100+
log("addMemory: error", { error: errorMessage });
101+
return { success: false as const, error: errorMessage };
98102
}
99103
}
100104

101105
async deleteMemory(memoryId: string) {
106+
log("deleteMemory: start", { memoryId });
102107
try {
103108
await withTimeout(
104109
this.getClient().memories.delete(memoryId),
105110
TIMEOUT_MS
106111
);
112+
log("deleteMemory: success", { memoryId });
107113
return { success: true };
108114
} catch (error) {
109-
console.error("Supermemory: delete memory failed", error);
110-
return null;
115+
const errorMessage = error instanceof Error ? error.message : String(error);
116+
log("deleteMemory: error", { memoryId, error: errorMessage });
117+
return { success: false, error: errorMessage };
111118
}
112119
}
113120

@@ -124,11 +131,11 @@ export class SupermemoryClient {
124131
TIMEOUT_MS
125132
);
126133
log("listMemories: success", { count: result.memories?.length || 0 });
127-
return result;
134+
return { success: true as const, ...result };
128135
} catch (error) {
129-
log("listMemories: error", { error: String(error) });
130-
console.error("Supermemory: list memories failed", error);
131-
return { memories: [], pagination: { currentPage: 1, totalItems: 0, totalPages: 0 } };
136+
const errorMessage = error instanceof Error ? error.message : String(error);
137+
log("listMemories: error", { error: errorMessage });
138+
return { success: false as const, error: errorMessage, memories: [], pagination: { currentPage: 1, totalItems: 0, totalPages: 0 } };
132139
}
133140
}
134141

@@ -137,7 +144,7 @@ export class SupermemoryClient {
137144
messages: ConversationMessage[],
138145
containerTags: string[],
139146
metadata?: Record<string, string | number | boolean>
140-
): Promise<ConversationIngestResponse | null> {
147+
) {
141148
log("ingestConversation: start", { conversationId, messageCount: messages.length });
142149
try {
143150
const response = await withTimeout(
@@ -160,16 +167,16 @@ export class SupermemoryClient {
160167
if (!response.ok) {
161168
const errorText = await response.text();
162169
log("ingestConversation: error response", { status: response.status, error: errorText });
163-
return null;
170+
return { success: false as const, error: `HTTP ${response.status}: ${errorText}` };
164171
}
165172

166173
const result = await response.json() as ConversationIngestResponse;
167174
log("ingestConversation: success", { conversationId, status: result.status });
168-
return result;
175+
return { success: true as const, ...result };
169176
} catch (error) {
170-
log("ingestConversation: error", { error: String(error) });
171-
console.error("Supermemory: ingest conversation failed", error);
172-
return null;
177+
const errorMessage = error instanceof Error ? error.message : String(error);
178+
log("ingestConversation: error", { error: errorMessage });
179+
return { success: false as const, error: errorMessage };
173180
}
174181
}
175182
}

src/services/compaction.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ export function createCompactionHook(
308308
{ type: "conversation" }
309309
);
310310

311-
if (result) {
311+
if (result.success) {
312312
log("[compaction] summary saved as memory", { sessionID, memoryId: result.id });
313+
} else {
314+
log("[compaction] failed to save summary", { error: result.error });
313315
}
314316
} catch (err) {
315317
log("[compaction] failed to save summary", { error: String(err) });

0 commit comments

Comments
 (0)