Skip to content

Commit 0e70e2f

Browse files
fix(solid): use status-only error checks
1 parent 8ee678e commit 0e70e2f

1 file changed

Lines changed: 23 additions & 32 deletions

File tree

api/server/services/SolidStorage.js

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -474,27 +474,21 @@ async function ensureContainerExists(containerUrl, fetch) {
474474

475475
// If we get here, container might not exist
476476
if (response.status === 404) {
477-
throw new Error('Container not found');
477+
const err = new Error('Container not found');
478+
err.status = 404;
479+
err.response = { status: 404 };
480+
throw err;
478481
}
479482
} catch (error) {
480-
// Container doesn't exist, create it
481-
if (
482-
error.status === 404 ||
483-
error.message?.includes('404') ||
484-
error.message?.includes('not found') ||
485-
error.message?.includes('Container not found')
486-
) {
483+
// Container doesn't exist, create it (use status only)
484+
if (error?.status === 404 || error?.response?.status === 404) {
487485
logger.info('[SolidStorage] Container does not exist, creating', { containerUrl });
488486
try {
489487
await createContainerAt(containerUrl, { fetch });
490488
logger.info('[SolidStorage] Container created successfully', { containerUrl });
491489
} catch (createError) {
492490
// If creation fails with 409, container already exists (race condition)
493-
if (
494-
createError.status === 409 ||
495-
createError.message?.includes('409') ||
496-
createError.message?.includes('already exists')
497-
) {
491+
if (createError?.status === 409 || createError?.response?.status === 409) {
498492
logger.debug('[SolidStorage] Container already exists (race condition)', {
499493
containerUrl,
500494
});
@@ -678,7 +672,7 @@ async function saveMessageToSolid(req, messageDocument, metadata) {
678672
await getFile(messagePath, { fetch: authenticatedFetch });
679673
messageExists = true;
680674
} catch (error) {
681-
if (error.status === 404 || error.message?.includes('404')) {
675+
if (error?.status === 404 || error?.response?.status === 404) {
682676
messageExists = false;
683677
} else {
684678
logger.warn('[SolidStorage] Error checking if message exists, will try to save anyway', {
@@ -1037,7 +1031,7 @@ async function updateMessageInSolid(req, messageData, metadata) {
10371031
conversationId,
10381032
});
10391033
} catch (error) {
1040-
if (error.status === 404 || error.message?.includes('404')) {
1034+
if (error?.status === 404 || error?.response?.status === 404) {
10411035
throw new Error(`Message with ID ${messageData.messageId} not found`);
10421036
}
10431037
throw error;
@@ -1211,7 +1205,7 @@ async function deleteMessagesFromSolid(req, params) {
12111205
messageId: message.messageId,
12121206
});
12131207
} catch (error) {
1214-
if (error.status === 404 || error.message?.includes('404')) {
1208+
if (error?.status === 404 || error?.response?.status === 404) {
12151209
// File already doesn't exist, count it as deleted
12161210
logger.debug('[SolidStorage] Message file already deleted', {
12171211
messageId: message.messageId,
@@ -1286,7 +1280,7 @@ async function saveConvoToSolid(req, convoDocument, metadata) {
12861280
hasTitle: !!existingConversation.title,
12871281
});
12881282
} catch (error) {
1289-
if (error.status === 404 || error.message?.includes('404')) {
1283+
if (error?.status === 404 || error?.response?.status === 404) {
12901284
logger.debug('[SolidStorage] Conversation does not exist yet, will create new', {
12911285
conversationId: finalConversationId,
12921286
});
@@ -1569,7 +1563,7 @@ async function getConvoFromSolid(req, conversationId) {
15691563

15701564
return conversationData;
15711565
} catch (error) {
1572-
if (error.status === 404 || error.message?.includes('404')) {
1566+
if (error?.status === 404 || error?.response?.status === 404) {
15731567
logger.info('[SolidStorage] Conversation not found', {
15741568
conversationId,
15751569
});
@@ -1710,14 +1704,11 @@ async function getConvosByCursorFromSolid(req, options = {}) {
17101704
responseStatusText: error?.response?.statusText,
17111705
});
17121706

1713-
// Check if error is a 404 (container doesn't exist)
1707+
// Check if error is a 404 (container doesn't exist) - use status only
17141708
const isNotFound =
17151709
errorStatus === 404 ||
17161710
errorStatus === '404' ||
1717-
errorMessage?.includes('404') ||
1718-
errorMessage?.includes('Not Found') ||
1719-
errorMessage?.toLowerCase().includes('not found') ||
1720-
errorMessage?.toLowerCase().includes('404');
1711+
error?.response?.status === 404;
17211712

17221713
if (isNotFound) {
17231714
// Container doesn't exist, return empty result (this is expected for new users)
@@ -2066,7 +2057,7 @@ async function deleteConvosFromSolid(req, conversationIds) {
20662057
conversationPath,
20672058
});
20682059
} catch (error) {
2069-
if (error.status === 404 || error.message?.includes('404')) {
2060+
if (error?.status === 404 || error?.response?.status === 404) {
20702061
// File already doesn't exist, count it as deleted
20712062
deletedCount++;
20722063
logger.debug('[SolidStorage] Conversation file already deleted', {
@@ -2166,12 +2157,12 @@ async function fetchAcl(aclUrl, fetchFn) {
21662157
}
21672158
return await response.text();
21682159
} catch (error) {
2169-
// Treat 404 and 403 as "no ACL exists"
2160+
// Treat 404 and 403 as "no ACL exists" (use status only)
21702161
if (
2171-
error.status === 404 ||
2172-
error.status === 403 ||
2173-
error.message?.includes('404') ||
2174-
error.message?.includes('403')
2162+
error?.status === 404 ||
2163+
error?.status === 403 ||
2164+
error?.response?.status === 404 ||
2165+
error?.response?.status === 403
21752166
) {
21762167
return null;
21772168
}
@@ -2738,7 +2729,7 @@ async function removePublicAccessForShare(req, conversationId) {
27382729
conversationId,
27392730
});
27402731
} catch (error) {
2741-
if (error.status === 404 || error.message?.includes('404')) {
2732+
if (error?.status === 404 || error?.response?.status === 404) {
27422733
logger.debug('[SolidStorage] Conversation file not found when removing public access', {
27432734
conversationPath,
27442735
conversationId,
@@ -2767,7 +2758,7 @@ async function removePublicAccessForShare(req, conversationId) {
27672758
conversationId,
27682759
});
27692760
} catch (error) {
2770-
if (error.status === 404 || error.message?.includes('404')) {
2761+
if (error?.status === 404 || error?.response?.status === 404) {
27712762
logger.debug('[SolidStorage] Messages container not found when removing public access', {
27722763
messagesContainerPath,
27732764
conversationId,
@@ -2808,7 +2799,7 @@ async function removePublicAccessForShare(req, conversationId) {
28082799
await removePublicReadAccess(messagePath, authenticatedFetch);
28092800
messagesUnshared++;
28102801
} catch (error) {
2811-
if (error.status === 404 || error.message?.includes('404')) {
2802+
if (error?.status === 404 || error?.response?.status === 404) {
28122803
logger.debug('[SolidStorage] Message file not found when removing public access', {
28132804
messageId: message.messageId,
28142805
conversationId,

0 commit comments

Comments
 (0)