MetaCubeX/mihomo · error · InvalidParams

Invalid parameters

Error message

Invalid parameters

What it means

InvalidParams is raised by MihomoAPI.request (mihomo/client.py:85) when the remote mihomo API responds with HTTP 400 and the body either is not valid JSON or is JSON without a 'detail' key. It means the request parameters — typically the UID or the lang query parameter — were rejected by api.mihomo.me. This is a parameter-validation error from the upstream service, surfaced as an exception without any detail message.

Source

Thrown at mihomo/client.py:85

        Raises:
            HttpRequestError: If the HTTP request fails.
            InvalidParams: If the API request contains invalid parameters.
            UserNotFound: If the requested user is not found.

        """
        url = self.BASE_URL + "/" + str(uid)
        params.update({"lang": language.value})

        async with aiohttp.ClientSession() as session:
            async with session.get(url, params=params) as response:
                match response.status:
                    case 200:
                        return await response.json(encoding="utf-8")
                    case 400:
                        try:
                            data = await response.json(encoding="utf-8")
                        except:
                            raise InvalidParams()
                        else:
                            if isinstance(data, dict) and (
                                detail := data.get("detail")
                            ):
                                raise InvalidParams(detail)
                            raise InvalidParams()
                    case 404:
                        raise UserNotFound()
                    case _:
                        raise HttpRequestError(response.status, str(response.reason))

    async def fetch_user(
        self,
        uid: int,
        *,
        replace_icon_name_with_url: bool = False,
    ) -> StarrailInfoParsed:
        """

View on GitHub (pinned to 008b91bfe8)

Solutions

  1. Validate the UID before calling fetch_user: it must be a 9-digit numeric Star Rail UID (str(uid).isdigit() and len == 9).
  2. Inspect str(exc) / exc.message — a sibling raise at client.py:90 includes the API's 'detail' text which pinpoints the bad parameter.
  3. Verify the Language passed to MihomoAPI(language=...) is one of the supported enum members and not a raw string.
  4. If the 400 body is HTML (proxy/WAF), retry against the API later or from a different network; check api.mihomo.me status.

Example fix

// before
data = await client.fetch_user(int(user_input))

// after
uid = int(user_input)
if not (100000000 <= uid <= 999999999):
    raise ValueError(f"Invalid Star Rail UID: {uid}")
data = await client.fetch_user(uid)
Defensive patterns

Strategy: validation

Validate before calling

def is_valid_uid(uid: int | str) -> bool:
    s = str(uid)
    return s.isdigit() and len(s) == 9 and 100000000 <= int(s) <= 999999999

if not is_valid_uid(uid):
    raise ValueError(f"Invalid Star Rail UID: {uid!r}")
data = await client.fetch_user(uid)

Try / catch

from mihomo.errors import InvalidParams
try:
    data = await client.fetch_user(uid)
except InvalidParams as e:
    log.warning("Bad parameters for uid=%s: %s", uid, e.message)

Prevention

When it happens

Trigger: Calling fetch_user or fetch_user_v1 with a malformed UID (empty string, non-numeric characters, wrong length) so the API returns 400 with a non-JSON or detail-less body; also when the Language enum value sent as the lang param is not accepted by the endpoint, or the version=v1 param combination is rejected.

Common situations: Passing a UID taken from user input without validating it (e.g. trimming mistakes, embedded spaces); using an outdated library version whose Language values no longer match the API; sending a 9-digit non-StarRail UID; API contract changes where 400 responses switch from JSON to plain text (Cloudflare/proxy error pages), which trips the bare except at client.py:84.

Related errors


AI-assisted analysis of MetaCubeX/mihomo@008b91bfe8 (2026-08-27). Data as JSON: /api/errors/ab66db21eb53f62f. Report an issue: GitHub.