gitbutlerapp/gitbutler · error · ApiError

HTTP Error

Error message

HTTP Error ${response.statusText}: ${text}

What it means

parseResponseJSON throws ApiError(`HTTP Error ${response.statusText}: ${text}`, response) for any response with status >= 400 that is not 401 and whose body does not mention "401 Unauthorized". It wraps the raw status text and response body so callers can see exactly what the server rejected.

Solutions

  1. Read the interpolated body text in the ApiError message to see the server's actual error explanation
  2. Check response.status on the ApiError: 404 => verify the resource/URL, 422 => fix the payload, 5xx => check backend health/retry
  3. Confirm the API base URL and path are correct for the intended backend version
  4. Add retry/backoff for transient 5xx/429 and surface actionable errors to the UI otherwise

Example fix

// before
const project = await httpClient.get(`projects/${projectId}`);
// after
try {
  const project = await httpClient.get(`projects/${projectId}`);
} catch (e) {
  if (e instanceof ApiError && e.response?.status === 404) {
    showToast({ message: `Project ${projectId} not found`, style: "danger" });
    return;
  }
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

const url = new URL(path, `${apiUrl}/api/`);
if (!resourceExists(url)) throw new Error(`Refusing request to unknown resource: ${url}`);

Type guard

function isHttpApiError(e: unknown): e is ApiError {
  return e instanceof ApiError && typeof e.response?.status === 'number' && e.response.status >= 400;
}

Try / catch

try {
  await httpClient.get(path);
} catch (e) {
  if (e instanceof ApiError) {
    const status = e.response?.status;
    if (status === 404) { /* resource missing */ }
    else if (status && status >= 500) { /* retry with backoff */ }
    else { showToast({ message: e.message, style: 'danger' }); }
  } else { throw e; }
}

Prevention

When it happens

Trigger: Any HttpClient get/post/put/patch/delete receiving 4xx/5xx other than 401 — e.g. 404 for a missing project, 422 for validation failures, 500 for backend errors; the message interpolates statusText and the raw body text.

Common situations: Requesting a non-existent resource (404); submitting invalid payloads (422); backend outage or bug (500); rate limiting (429); wrong API base URL hitting an unexpected service.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/88dc11f53f8edadf. Report an issue: GitHub.

Appendix: source

Thrown at packages/shared/src/lib/network/httpClient.ts:120

		return await this.requestJson<T>(path, { ...opts, method: "DELETE" });
	}

	async postRaw(path: string, opts?: RequestOptions) {
		return await this.request(path, { ...opts, method: "POST" });
	}
}

async function parseResponseJSON(response: Response) {
	if (response.status === 204 || response.status === 205) {
		return null;
	} else if (response.status === 401) {
		throw new ApiError("Login token expired. Please log in to GitButler again.", response);
	} else if (response.status >= 400) {
		const text = await response.text();
		if (text.includes("401 Unauthorized") || text.includes("401 unauthorized")) {
			throw new ApiError("Login token expired. Please log in to GitButler again.", response);
		}
		throw new ApiError(`HTTP Error ${response.statusText}: ${text}`, response);
	} else {
		return await response.json();
	}
}

function formatBody(body?: FormData | object) {
	if (!body) return;
	return body instanceof FormData ? body : JSON.stringify(body);
}

View on GitHub (pinned to 58e5313667)