fix: replace magic numbers with RetryReason enum constants
Address remaining Copilot review comments on PR #306: - Import RetryReason enum from Utils (already re-exported via Utils/index.ts) - Replace all numeric literals (0/1/2/3/4/7) in retryErrorCode with the corresponding enum members (UnknownError / SignalErrorNoSession / etc.) - Removes inline comments that were only needed to explain magic numbers - If RetryReason values ever change, the compiler will surface the drift Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+12
-11
@@ -54,6 +54,7 @@ import {
|
|||||||
MISSING_KEYS_ERROR_TEXT,
|
MISSING_KEYS_ERROR_TEXT,
|
||||||
NACK_REASONS,
|
NACK_REASONS,
|
||||||
NO_MESSAGE_FOUND_ERROR_TEXT,
|
NO_MESSAGE_FOUND_ERROR_TEXT,
|
||||||
|
RetryReason,
|
||||||
normalizeKeyLidToPn,
|
normalizeKeyLidToPn,
|
||||||
normalizeMessageJids,
|
normalizeMessageJids,
|
||||||
resolveLidToPn,
|
resolveLidToPn,
|
||||||
@@ -1331,20 +1332,20 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
// corrupted session. Deleting prematurely creates a race window where no session
|
// corrupted session. Deleting prematurely creates a race window where no session
|
||||||
// exists, which can cause "No Session" errors on concurrent messages.
|
// exists, which can cause "No Session" errors on concurrent messages.
|
||||||
const retryErrorCode = (() => {
|
const retryErrorCode = (() => {
|
||||||
if (!decryptionError) return 0
|
if (!decryptionError) return RetryReason.UnknownError
|
||||||
// Bad MAC must be checked first — it is also in corruptedSessionErrors
|
// Bad MAC must be checked first — it is also in corruptedSessionErrors
|
||||||
// but warrants the more specific code 7 over the generic code 4.
|
// but warrants the more specific code 7 over the generic code 4.
|
||||||
if (decryptionError.includes(BAD_MAC_ERROR_TEXT)) return 7 // SignalErrorBadMac
|
if (decryptionError.includes(BAD_MAC_ERROR_TEXT)) return RetryReason.SignalErrorBadMac
|
||||||
// MessageCounterError and other corrupted-session variants (code 4)
|
// MessageCounterError and other corrupted-session variants
|
||||||
if (DECRYPTION_RETRY_CONFIG.corruptedSessionErrors.some(e => decryptionError.includes(e))) return 4 // SignalErrorInvalidMessage
|
if (DECRYPTION_RETRY_CONFIG.corruptedSessionErrors.some(e => decryptionError.includes(e))) return RetryReason.SignalErrorInvalidMessage
|
||||||
// Missing / invalid session record (code 1)
|
// Missing / invalid session record
|
||||||
if (DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(e => decryptionError.includes(e)) ||
|
if (DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(e => decryptionError.includes(e)) ||
|
||||||
/no\s+(open\s+)?sessions?/i.test(decryptionError)) return 1 // SignalErrorNoSession
|
/no\s+(open\s+)?sessions?/i.test(decryptionError)) return RetryReason.SignalErrorNoSession
|
||||||
// PreKey / key-id errors (code 3)
|
// PreKey / key-id errors
|
||||||
if (/pre\s*key/i.test(decryptionError)) return 3 // SignalErrorInvalidKeyId
|
if (/pre\s*key/i.test(decryptionError)) return RetryReason.SignalErrorInvalidKeyId
|
||||||
// Identity / key errors (code 2)
|
// Identity / key errors
|
||||||
if (/invalid\s*key|untrusted\s*identity/i.test(decryptionError)) return 2 // SignalErrorInvalidKey
|
if (/invalid\s*key|untrusted\s*identity/i.test(decryptionError)) return RetryReason.SignalErrorInvalidKey
|
||||||
return 0
|
return RetryReason.UnknownError
|
||||||
})()
|
})()
|
||||||
|
|
||||||
if (retryCount <= 2) {
|
if (retryCount <= 2) {
|
||||||
|
|||||||
Reference in New Issue
Block a user