Conan server side rest API returns wrong status for missing package reference
Summary
The Conan 2 server side rest API returns wrong status for missing package reference when only one user name or one channel name is provided. It works as expected when a package reference contains both, or none. But it does not work when one of the two is missing.
Here's an example request:
${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/conan/v2/conans/<name>/<version>/<user>/<channel>/revisions/<revision>/packages/<package>/latest
If user=_ and channel=_ the request works (returns {"message":"404 Package Not Found"}).
If user=myname and channel=_ the request returns {"message":"400 Bad request - channel can't be solely blank"}.
If user=_ and channel=stable the request returns {"message":"400 Bad request - username can't be solely blank"}
If user=myname and channel=stable the request works (returns {"message":"404 Package Not Found"}).
Expected result:
For all 4 cases the expected return would be {"message":"404 Package Not Found"} when there's no package with given ID.
Logs
Here's an example log output for the relevant failure (with name and project id redacted):
HttpRequest: get: https://center2.conan.io/v2/conans/mysql-connector-cpp/8.1.0/CUSTOM_NAME/_/revisions/3cb60c9025bf92ca456ea41f4322c3ea/packages/145376572b5cc1f80fa2e94b44914ab906900fcf/latest
HttpRequest: get: https://gitlab.com/api/v4/projects/PROJECT_ID/packages/conan/v2/conans/mysql-connector-cpp/8.1.0/CUSTOM_NAME/_/revisions/3cb60c9025bf92ca456ea41f4322c3ea/packages/145376572b5cc1f80fa2e94b44914ab906900fcf/latest
Traceback (most recent call last):
File "/usr/local/lib/python3.12/dist-packages/conan/internal/rest/remote_manager.py", line 255, in get_latest_package_reference
result = cached_method[pref]
~~~~~~~~~~~~~^^^^^^
KeyError: mysql-connector-cpp/8.1.0@CUSTOM_NAME#3cb60c9025bf92ca456ea41f4322c3ea%1758291153.6246006:145376572b5cc1f80fa2e94b44914ab906900fcf