Merge pull request #5486 from CodeMan62/fix-network-url-validation-error-msg

Update network URL validation error message format to match expected
This commit is contained in:
Louis Dureuil 2025-04-03 10:42:33 +00:00 committed by GitHub
commit c3c5a928e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 5 deletions

View File

@ -119,10 +119,22 @@ pub struct Network {
impl Remote {
pub fn try_into_db_node(self, name: &str) -> Result<DbRemote, ResponseError> {
Ok(DbRemote {
url: self.url.set().ok_or(ResponseError::from_msg(
url: self
.url
.set()
.ok_or(ResponseError::from_msg(
format!("Missing field `.remotes.{name}.url`"),
meilisearch_types::error::Code::MissingNetworkUrl,
))?,
))
.and_then(|url| {
if let Err(error) = url::Url::parse(&url) {
return Err(ResponseError::from_msg(
format!("Invalid `.remotes.{name}.url` (`{url}`): {error}"),
meilisearch_types::error::Code::InvalidNetworkUrl,
));
}
Ok(url)
})?,
search_api_key: self.search_api_key.set(),
})
}
@ -211,7 +223,15 @@ async fn patch_network(
let merged = DbRemote {
url: match new_url {
Setting::Set(new_url) => new_url,
Setting::Set(new_url) => {
if let Err(error) = url::Url::parse(&new_url) {
return Err(ResponseError::from_msg(
format!("Invalid `.remotes.{key}.url` (`{new_url}`): {error}"),
meilisearch_types::error::Code::InvalidNetworkUrl,
));
}
new_url
}
Setting::Reset => {
return Err(ResponseError::from_msg(
format!(

View File

@ -117,6 +117,25 @@ async fn errors_on_param() {
}
"###);
// remote with url not valid
let (response, code) = server
.set_network(json!({"remotes": {
"new": {
"url": "no-http-scheme"
}
}}))
.await;
meili_snap::snapshot!(code, @"400 Bad Request");
meili_snap::snapshot!(meili_snap::json_string!(response), @r###"
{
"message": "Invalid `.remotes.new.url` (`no-http-scheme`): relative URL without a base",
"code": "invalid_network_url",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_network_url"
}
"###);
// remote with non-existing param
let (response, code) = server
.set_network(json!({"remotes": {