()
| 15 | sslHint?: string; |
| 16 | } |
| 17 | async function checkEndpoints(): Promise<PreflightCheckResult> { |
| 18 | try { |
| 19 | const oauthConfig = getOauthConfig(); |
| 20 | const tokenUrl = new URL(oauthConfig.TOKEN_URL); |
| 21 | const endpoints = [`${oauthConfig.BASE_API_URL}/api/hello`, `${tokenUrl.origin}/v1/oauth/hello`]; |
| 22 | const checkEndpoint = async (url: string): Promise<PreflightCheckResult> => { |
| 23 | try { |
| 24 | const response = await axios.get(url, { |
| 25 | headers: { |
| 26 | 'User-Agent': getUserAgent() |
| 27 | } |
| 28 | }); |
| 29 | if (response.status !== 200) { |
| 30 | const hostname = new URL(url).hostname; |
| 31 | return { |
| 32 | success: false, |
| 33 | error: `Failed to connect to ${hostname}: Status ${response.status}` |
| 34 | }; |
| 35 | } |
| 36 | return { |
| 37 | success: true |
| 38 | }; |
| 39 | } catch (error) { |
| 40 | const hostname = new URL(url).hostname; |
| 41 | const sslHint = getSSLErrorHint(error); |
| 42 | return { |
| 43 | success: false, |
| 44 | error: `Failed to connect to ${hostname}: ${error instanceof Error ? (error as ErrnoException).code || error.message : String(error)}`, |
| 45 | sslHint: sslHint ?? undefined |
| 46 | }; |
| 47 | } |
| 48 | }; |
| 49 | const results = await Promise.all(endpoints.map(checkEndpoint)); |
| 50 | const failedResult = results.find(result => !result.success); |
| 51 | if (failedResult) { |
| 52 | // Log failure to Statsig |
| 53 | logEvent('tengu_preflight_check_failed', { |
| 54 | isConnectivityError: false, |
| 55 | hasErrorMessage: !!failedResult.error, |
| 56 | isSSLError: !!failedResult.sslHint |
| 57 | }); |
| 58 | } |
| 59 | return failedResult || { |
| 60 | success: true |
| 61 | }; |
| 62 | } catch (error) { |
| 63 | logError(error as Error); |
| 64 | |
| 65 | // Log to Statsig |
| 66 | logEvent('tengu_preflight_check_failed', { |
| 67 | isConnectivityError: true |
| 68 | }); |
| 69 | return { |
| 70 | success: false, |
| 71 | error: `Connectivity check error: ${error instanceof Error ? (error as ErrnoException).code || error.message : String(error)}` |
| 72 | }; |
| 73 | } |
| 74 | } |
no test coverage detected