Handle missing keys with a default (#3753)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
@@ -7,9 +7,9 @@ use axum::{
|
|||||||
Json, Router,
|
Json, Router,
|
||||||
};
|
};
|
||||||
use etcetera::{choose_app_strategy, AppStrategy};
|
use etcetera::{choose_app_strategy, AppStrategy};
|
||||||
use goose::config::Config;
|
|
||||||
use goose::config::APP_STRATEGY;
|
use goose::config::APP_STRATEGY;
|
||||||
use goose::config::{extensions::name_to_key, PermissionManager};
|
use goose::config::{extensions::name_to_key, PermissionManager};
|
||||||
|
use goose::config::{Config, ConfigError};
|
||||||
use goose::config::{ExtensionConfigManager, ExtensionEntry};
|
use goose::config::{ExtensionConfigManager, ExtensionEntry};
|
||||||
use goose::model::ModelConfig;
|
use goose::model::ModelConfig;
|
||||||
use goose::providers::base::ProviderMetadata;
|
use goose::providers::base::ProviderMetadata;
|
||||||
@@ -142,7 +142,7 @@ pub async fn remove_config(
|
|||||||
request_body = ConfigKeyQuery,
|
request_body = ConfigKeyQuery,
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "Configuration value retrieved successfully", body = Value),
|
(status = 200, description = "Configuration value retrieved successfully", body = Value),
|
||||||
(status = 404, description = "Configuration key not found")
|
(status = 500, description = "Unable to get the configuration value"),
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
pub async fn read_config(
|
pub async fn read_config(
|
||||||
@@ -160,17 +160,24 @@ pub async fn read_config(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config = Config::global();
|
let config = Config::global();
|
||||||
|
let response_value = match config.get(&query.key, query.is_secret) {
|
||||||
match config.get(&query.key, query.is_secret) {
|
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
if query.is_secret {
|
if query.is_secret {
|
||||||
Ok(Json(Value::Bool(true)))
|
Value::Bool(true)
|
||||||
} else {
|
} else {
|
||||||
Ok(Json(value))
|
value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => Err(StatusCode::NOT_FOUND),
|
Err(ConfigError::NotFound(_)) => {
|
||||||
}
|
if query.is_secret {
|
||||||
|
Value::Bool(false)
|
||||||
|
} else {
|
||||||
|
Value::Null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR),
|
||||||
|
};
|
||||||
|
Ok(Json(response_value))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
|
|||||||
@@ -333,8 +333,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"404": {
|
"500": {
|
||||||
"description": "Configuration key not found"
|
"description": "Unable to get the configuration value"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1015,9 +1015,9 @@ export type ReadConfigData = {
|
|||||||
|
|
||||||
export type ReadConfigErrors = {
|
export type ReadConfigErrors = {
|
||||||
/**
|
/**
|
||||||
* Configuration key not found
|
* Unable to get the configuration value
|
||||||
*/
|
*/
|
||||||
404: unknown;
|
500: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ReadConfigResponses = {
|
export type ReadConfigResponses = {
|
||||||
|
|||||||
Reference in New Issue
Block a user