axios+telemetry cleanup
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
*/
|
||||
|
||||
import { feature } from 'bun:bundle'
|
||||
import axios from 'axios'
|
||||
import { isHttpError, nativeRequest } from '../../utils/http.js'
|
||||
import { mkdir, readFile, stat, writeFile } from 'fs/promises'
|
||||
import pickBy from 'lodash-es/pickBy.js'
|
||||
import { dirname } from 'path'
|
||||
@@ -27,7 +27,7 @@ import {
|
||||
import { clearMemoryFileCaches } from '../../utils/claudemd.js'
|
||||
import { getMemoryPath } from '../../utils/config.js'
|
||||
import { logForDiagnosticsNoPII } from '../../utils/diagLogs.js'
|
||||
import { classifyAxiosError } from '../../utils/errors.js'
|
||||
import { classifyHttpError } from '../../utils/errors.js'
|
||||
import { getRepoRemoteHash } from '../../utils/git.js'
|
||||
import {
|
||||
getAPIProvider,
|
||||
@@ -257,26 +257,37 @@ async function fetchUserSettingsOnce(): Promise<SettingsSyncFetchResult> {
|
||||
}
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
...authHeaders.headers,
|
||||
'User-Agent': getClaudeCodeUserAgent(),
|
||||
}
|
||||
const headers: Record<string, string> = {
|
||||
...authHeaders.headers,
|
||||
'User-Agent': getClaudeCodeUserAgent(),
|
||||
}
|
||||
|
||||
const endpoint = getSettingsSyncEndpoint()
|
||||
const response = await axios.get(endpoint, {
|
||||
headers,
|
||||
timeout: SETTINGS_SYNC_TIMEOUT_MS,
|
||||
validateStatus: status => status === 200 || status === 404,
|
||||
})
|
||||
const endpoint = getSettingsSyncEndpoint()
|
||||
try {
|
||||
const response = await nativeRequest(endpoint, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
timeout: SETTINGS_SYNC_TIMEOUT_MS,
|
||||
responseType: 'json',
|
||||
})
|
||||
|
||||
// 404 means no settings exist yet
|
||||
if (response.status === 404) {
|
||||
logForDiagnosticsNoPII('info', 'settings_sync_fetch_empty')
|
||||
return {
|
||||
success: true,
|
||||
isEmpty: true,
|
||||
}
|
||||
}
|
||||
logForDiagnosticsNoPII('info', 'settings_sync_fetch_success')
|
||||
return {
|
||||
success: true,
|
||||
data: response.data,
|
||||
isEmpty: false,
|
||||
}
|
||||
} catch (error) {
|
||||
if (isHttpError(error) && error.status === 404) {
|
||||
// 404 means no settings exist yet
|
||||
logForDiagnosticsNoPII('info', 'settings_sync_fetch_empty')
|
||||
return {
|
||||
success: true,
|
||||
isEmpty: true,
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
const parsed = UserSyncDataSchema().safeParse(response.data)
|
||||
if (!parsed.success) {
|
||||
@@ -293,23 +304,23 @@ async function fetchUserSettingsOnce(): Promise<SettingsSyncFetchResult> {
|
||||
data: parsed.data,
|
||||
isEmpty: false,
|
||||
}
|
||||
} catch (error) {
|
||||
const { kind, message } = classifyAxiosError(error)
|
||||
switch (kind) {
|
||||
case 'auth':
|
||||
return {
|
||||
success: false,
|
||||
error: 'Not authorized for settings sync',
|
||||
skipRetry: true,
|
||||
}
|
||||
case 'timeout':
|
||||
return { success: false, error: 'Settings sync request timeout' }
|
||||
case 'network':
|
||||
return { success: false, error: 'Cannot connect to server' }
|
||||
default:
|
||||
return { success: false, error: message }
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const { kind, message } = classifyHttpError(error)
|
||||
switch (kind) {
|
||||
case 'auth':
|
||||
return {
|
||||
success: false,
|
||||
error: 'Not authorized for settings sync',
|
||||
skipRetry: true,
|
||||
}
|
||||
case 'timeout':
|
||||
return { success: false, error: 'Settings sync request timeout' }
|
||||
case 'network':
|
||||
return { success: false, error: 'Cannot connect to server' }
|
||||
default:
|
||||
return { success: false, error: message }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUserSettings(
|
||||
@@ -358,30 +369,29 @@ async function uploadUserSettings(
|
||||
}
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
...authHeaders.headers,
|
||||
'User-Agent': getClaudeCodeUserAgent(),
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
const headers: Record<string, string> = {
|
||||
...authHeaders.headers,
|
||||
'User-Agent': getClaudeCodeUserAgent(),
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
const endpoint = getSettingsSyncEndpoint()
|
||||
const response = await axios.put(
|
||||
endpoint,
|
||||
{ entries },
|
||||
{
|
||||
headers,
|
||||
timeout: SETTINGS_SYNC_TIMEOUT_MS,
|
||||
},
|
||||
)
|
||||
const endpoint = getSettingsSyncEndpoint()
|
||||
const response = await nativeRequest(endpoint, {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: { entries },
|
||||
timeout: SETTINGS_SYNC_TIMEOUT_MS,
|
||||
responseType: 'json',
|
||||
})
|
||||
|
||||
logForDiagnosticsNoPII('info', 'settings_sync_uploaded', {
|
||||
entryCount: Object.keys(entries).length,
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
checksum: response.data?.checksum,
|
||||
lastModified: response.data?.lastModified,
|
||||
}
|
||||
logForDiagnosticsNoPII('info', 'settings_sync_uploaded', {
|
||||
entryCount: Object.keys(entries).length,
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
checksum: response.data?.checksum,
|
||||
lastModified: response.data?.lastModified,
|
||||
}
|
||||
} catch (error) {
|
||||
logForDiagnosticsNoPII('warn', 'settings_sync_upload_error')
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user