Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(providers): surface friendly message on transport-level request failures
Akarshan Biswasb0d515678a53
diff --git a/web-app/src/lib/__tests__/model-factory.test.ts b/web-app/src/lib/__tests__/model-factory.test.tsindex 9789757e4..6e5437765 100644--- a/web-app/src/lib/__tests__/model-factory.test.ts+++ b/web-app/src/lib/__tests__/model-factory.test.ts@@ -3,6 +3,7 @@ import {ModelFactory,cleanUpstreamErrorMessage,createCustomFetch,+ describeTransportError,} from '../model-factory'import type { ProviderObject } from '@janhq/core'import { invoke } from '@tauri-apps/api/core'@@ -233,6 +234,54 @@ describe('cleanUpstreamErrorMessage', () => {})})+describe('describeTransportError', () => {+ it('maps reqwest "error sending request" to a connection message', () => {+ const msg = describeTransportError(+ new Error('error sending request for url (https://x/v1/chat/completions)')+ )+ expect(msg).toMatch(/connection failed|couldn't reach/i)+ })++ it('maps DNS failures to an address-resolution message', () => {+ const msg = describeTransportError(new Error('dns error: failed to lookup address'))+ expect(msg).toMatch(/could not be resolved/i)+ })++ it('maps timeouts to a timeout message', () => {+ expect(describeTransportError(new Error('operation timed out'))).toMatch(/timed out/i)+ })++ it('maps TLS errors to a secure-connection message', () => {+ expect(describeTransportError(new Error('invalid certificate'))).toMatch(/secure connection/i)+ })++ it('returns null for non-transport errors (HTTP/app errors)', () => {+ expect(describeTransportError(new Error('400 Bad Request: invalid model'))).toBeNull()+ })+})++describe('createCustomFetch — transport error wrapping', () => {+ it('rethrows transport failures with a friendly message and the url', async () => {+ const baseFetch: typeof globalThis.fetch = (async () => {+ throw new Error('error sending request for url (https://up/v1/chat/completions)')+ }) as typeof globalThis.fetch+ const wrapped = createCustomFetch(baseFetch, {}, true)+ await expect(+ wrapped('https://up/v1/chat/completions', { method: 'POST', body: '{}' })+ ).rejects.toThrow(/couldn't reach the provider.*https:\/\/up/i)+ })++ it('rethrows non-transport errors unchanged', async () => {+ const baseFetch: typeof globalThis.fetch = (async () => {+ throw new Error('boom unexpected')+ }) as typeof globalThis.fetch+ const wrapped = createCustomFetch(baseFetch, {}, true)+ await expect(+ wrapped('https://up/v1/chat/completions', { method: 'POST', body: '{}' })+ ).rejects.toThrow('boom unexpected')+ })+})+describe('createCustomFetch — max_tokens coercion', () => {async function captureSentBody(parameters: Record<string, unknown>,diff --git a/web-app/src/lib/model-factory.ts b/web-app/src/lib/model-factory.tsindex 944927a8a..22081ad5b 100644--- a/web-app/src/lib/model-factory.ts+++ b/web-app/src/lib/model-factory.ts@@ -289,6 +289,47 @@ export function cleanUpstreamErrorMessage(raw: string): string {return msg}+/**+ * Map a transport-level fetch failure (no HTTP response — DNS, connect, TLS,+ * timeout, dropped connection) to an actionable message. `@tauri-apps/plugin-http`+ * rethrows reqwest's raw "error sending request for url …" string, which isn't+ * useful to users. Returns null when `err` is not a recognised transport error.+ */+export function describeTransportError(err: unknown): string | null {+ const raw = err instanceof Error ? err.message : String(err)+ if (typeof raw !== 'string' || !raw) return null+ const m = raw.toLowerCase()++ const isTransport =+ /error sending request/.test(m) ||+ /error trying to connect/.test(m) ||+ /failed to fetch|networkerror|load failed/.test(m) ||+ /connection (refused|reset|closed|aborted)/.test(m) ||+ /connection error|broken pipe/.test(m) ||+ /dns error|failed to lookup|name resolution/.test(m) ||+ /(operation|request|connection) timed out|timeout/.test(m) ||+ /tls|certificate|ssl|handshake/.test(m) ||+ /unreachable/.test(m)+ if (!isTransport) return null++ if (/dns error|failed to lookup|name resolution|unreachable/.test(m)) {+ return "Couldn't reach the provider — the address could not be resolved. Check the provider's Base URL and your internet connection."+ }+ if (/timed out|timeout/.test(m)) {+ return 'The provider took too long to respond and the request timed out. It may be overloaded or slow to start — try again.'+ }+ if (/tls|certificate|ssl|handshake/.test(m)) {+ return "Couldn't establish a secure connection to the provider (TLS/certificate error). Verify the endpoint URL."+ }+ return "Couldn't reach the provider — the connection failed. Check the provider's Base URL and your internet connection, then try again."+}++function requestUrlOf(input: RequestInfo | URL): string {+ if (typeof input === 'string') return input+ if (input instanceof URL) return input.toString()+ return (input as Request).url ?? ''+}+/*** Create a custom fetch function that injects additional parameters into the* request body, normalising key names for OpenAI-compatible APIs:@@ -348,7 +389,14 @@ export function createCustomFetch(init = { ...init, body: JSON.stringify(buildBody(rawBody!, true)) }}- const res = await baseFetch(input, init)+ let res: Response+ try {+ res = await baseFetch(input, init)+ } catch (err) {+ const friendly = describeTransportError(err)+ if (!friendly) throw err+ throw new Error(`${friendly} (${requestUrlOf(input)})`)+ }if (res.ok) return resconst isLlamacpp500 = keepLlamacppOnly && res.status === 500