## Turns the two things that can go wrong with an HTTPRequest into messages ## worth showing a user: Godot's transport-level Result code (DNS, TLS, ## connection, timeout — these arrive with an empty body and a 0 status, so ## parsing the body first just yields a confusing empty-response error), and ## the provider's own HTTP/JSON error payload. class_name ClaudeGodotHttpError extends RefCounted const MAX_BODY_EXCERPT := 400 ## Human-readable reason for a non-success HTTPRequest.Result, or "" if the ## transport succeeded and the failure (if any) is in the response itself. static func transport_error(result: int, url: String, timeout: float) -> String: if result == HTTPRequest.RESULT_SUCCESS: return "" var host := _host_of(url) match result: HTTPRequest.RESULT_CANT_RESOLVE: return "Could not resolve %s — check the URL and this device's DNS/network." % host HTTPRequest.RESULT_CANT_CONNECT: return "Could not connect to %s — the host refused the connection or is unreachable." % host HTTPRequest.RESULT_CONNECTION_ERROR: return "Connection to %s dropped mid-request." % host HTTPRequest.RESULT_TLS_HANDSHAKE_ERROR: return "TLS handshake with %s failed — its certificate could not be verified." % host HTTPRequest.RESULT_TIMEOUT: return "Request to %s timed out after %ds." % [host, int(timeout)] HTTPRequest.RESULT_NO_RESPONSE: return "%s accepted the connection but sent no response." % host HTTPRequest.RESULT_BODY_SIZE_LIMIT_EXCEEDED: return "Response from %s exceeded the body size limit." % host HTTPRequest.RESULT_REQUEST_FAILED: return "Request to %s failed at the transport layer." % host HTTPRequest.RESULT_REDIRECT_LIMIT_REACHED: return "Too many redirects from %s." % host HTTPRequest.RESULT_DOWNLOAD_FILE_CANT_OPEN, HTTPRequest.RESULT_DOWNLOAD_FILE_WRITE_ERROR: return "Could not write the downloaded file." HTTPRequest.RESULT_CHUNKED_BODY_SIZE_MISMATCH: return "Malformed chunked response from %s." % host _: return "Network request to %s failed (Godot result code %d)." % [host, result] ## Message for a non-2xx response, pulling out whatever shape the provider ## used for its error object and naming who rejected the call. static func api_error(provider_label: String, model: String, response_code: int, body: String, data) -> String: var detail := "" if data is Dictionary and data.has("error"): var err = data["error"] if err is Dictionary: detail = str(err.get("message", "")) var kind := str(err.get("type", err.get("code", ""))) if kind != "" and kind != detail: detail = "%s (%s)" % [detail, kind] else: detail = str(err) if detail == "": detail = _excerpt(body) var hint := "" match response_code: 400: hint = " The request was rejected as malformed — often an unsupported parameter or a model that doesn't accept this endpoint's format." 401: hint = " The API key was rejected — check the key for this provider." 402: hint = " Payment/credit required on this account." 403: hint = " This key isn't allowed to use this model." 404: hint = " Endpoint or model not found — check the model id." 413: hint = " The request was too large — try a shorter conversation or fewer attachments." 429: hint = " Rate limited — wait a moment and retry." 500, 502, 503, 504: hint = " The provider is having trouble on their end; retrying later usually works." return "%s returned HTTP %d for %s: %s%s" % [provider_label, response_code, model, detail, hint] ## Message for a 2xx whose body isn't the JSON we expected. static func unparseable(provider_label: String, response_code: int, body: String) -> String: if body.strip_edges() == "": return "%s returned HTTP %d with an empty body." % [provider_label, response_code] return "%s returned HTTP %d with a body that isn't valid JSON: %s" % [provider_label, response_code, _excerpt(body)] static func _excerpt(body: String) -> String: var trimmed := body.strip_edges() if trimmed.length() <= MAX_BODY_EXCERPT: return trimmed return trimmed.substr(0, MAX_BODY_EXCERPT) + "… (%d chars total)" % trimmed.length() static func _host_of(url: String) -> String: var without_scheme := url.trim_prefix("https://").trim_prefix("http://") var slash := without_scheme.find("/") return without_scheme.substr(0, slash) if slash != -1 else without_scheme