mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Clean up err returns
This commit is contained in:
parent
06339a2699
commit
09a7619995
3 changed files with 47 additions and 50 deletions
|
@ -34,18 +34,16 @@ async fn net_request<'lua>(lua: &'lua Lua, config: Value<'lua>) -> Result<Table<
|
|||
Value::String(s) => {
|
||||
let url = s.to_string_lossy().to_string();
|
||||
let method = "GET".to_string();
|
||||
(url, method, HashMap::new(), None)
|
||||
Ok((url, method, HashMap::new(), None))
|
||||
}
|
||||
Value::Table(tab) => {
|
||||
// Extract url
|
||||
let url = match tab.raw_get::<&str, mlua::String>("url") {
|
||||
Ok(config_url) => config_url.to_string_lossy().to_string(),
|
||||
Err(_) => {
|
||||
return Err(Error::RuntimeError(
|
||||
"Missing 'url' in request config".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
Ok(config_url) => Ok(config_url.to_string_lossy().to_string()),
|
||||
Err(_) => Err(Error::RuntimeError(
|
||||
"Missing 'url' in request config".to_string(),
|
||||
)),
|
||||
}?;
|
||||
// Extract method
|
||||
let method = match tab.raw_get::<&str, mlua::String>("method") {
|
||||
Ok(config_method) => config_method.to_string_lossy().trim().to_ascii_uppercase(),
|
||||
|
@ -68,26 +66,22 @@ async fn net_request<'lua>(lua: &'lua Lua, config: Value<'lua>) -> Result<Table<
|
|||
Ok(config_body) => Some(config_body.as_bytes().to_owned()),
|
||||
Err(_) => None,
|
||||
};
|
||||
(url, method, headers, body)
|
||||
Ok((url, method, headers, body))
|
||||
}
|
||||
value => {
|
||||
return Err(Error::RuntimeError(format!(
|
||||
"Invalid request config - expected string or table, got {}",
|
||||
value.type_name()
|
||||
)))
|
||||
}
|
||||
};
|
||||
value => Err(Error::RuntimeError(format!(
|
||||
"Invalid request config - expected string or table, got {}",
|
||||
value.type_name()
|
||||
))),
|
||||
}?;
|
||||
// Convert method string into proper enum
|
||||
let method = method.trim().to_ascii_uppercase();
|
||||
let method = match method.as_ref() {
|
||||
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => &method,
|
||||
_ => {
|
||||
return Err(Error::RuntimeError(format!(
|
||||
"Invalid request config method '{}'",
|
||||
&method
|
||||
)))
|
||||
}
|
||||
};
|
||||
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => Ok(&method),
|
||||
_ => Err(Error::RuntimeError(format!(
|
||||
"Invalid request config method '{}'",
|
||||
&method
|
||||
))),
|
||||
}?;
|
||||
// Create and send the request
|
||||
let mut request = ureq::request(method, &url);
|
||||
for (header, value) in headers {
|
||||
|
|
|
@ -49,29 +49,34 @@ fn process_env_get<'lua>(lua: &'lua Lua, (_, key): (Value<'lua>, String)) -> Res
|
|||
fn process_env_set(_: &Lua, (_, key, value): (Value, String, Option<String>)) -> Result<()> {
|
||||
// Make sure key is valid, otherwise set_var will panic
|
||||
if key.is_empty() {
|
||||
return Err(Error::RuntimeError("Key must not be empty".to_string()));
|
||||
Err(Error::RuntimeError("Key must not be empty".to_string()))
|
||||
} else if key.contains('=') {
|
||||
return Err(Error::RuntimeError(
|
||||
Err(Error::RuntimeError(
|
||||
"Key must not contain the equals character '='".to_string(),
|
||||
));
|
||||
))
|
||||
} else if key.contains('\0') {
|
||||
return Err(Error::RuntimeError(
|
||||
Err(Error::RuntimeError(
|
||||
"Key must not contain the NUL character".to_string(),
|
||||
));
|
||||
}
|
||||
match value {
|
||||
Some(value) => {
|
||||
// Make sure value is valid, otherwise set_var will panic
|
||||
if value.contains('\0') {
|
||||
return Err(Error::RuntimeError(
|
||||
"Value must not contain the NUL character".to_string(),
|
||||
));
|
||||
))
|
||||
} else {
|
||||
match value {
|
||||
Some(value) => {
|
||||
// Make sure value is valid, otherwise set_var will panic
|
||||
if value.contains('\0') {
|
||||
Err(Error::RuntimeError(
|
||||
"Value must not contain the NUL character".to_string(),
|
||||
))
|
||||
} else {
|
||||
env::set_var(&key, &value);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
None => {
|
||||
env::remove_var(&key);
|
||||
Ok(())
|
||||
}
|
||||
env::set_var(&key, &value);
|
||||
}
|
||||
None => env::remove_var(&key),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_env_iter<'lua>(lua: &'lua Lua, (_, _): (Value<'lua>, ())) -> Result<Function<'lua>> {
|
||||
|
|
|
@ -21,16 +21,14 @@ pub async fn create(lua: &Lua) -> Result<()> {
|
|||
}
|
||||
|
||||
fn get_or_create_thread_from_arg<'a>(lua: &'a Lua, arg: Value<'a>) -> Result<Thread<'a>> {
|
||||
Ok(match arg {
|
||||
Value::Thread(thread) => thread,
|
||||
Value::Function(func) => lua.create_thread(func)?,
|
||||
val => {
|
||||
return Err(Error::RuntimeError(format!(
|
||||
"Expected type thread or function, got {}",
|
||||
val.type_name()
|
||||
)))
|
||||
}
|
||||
})
|
||||
match arg {
|
||||
Value::Thread(thread) => Ok(thread),
|
||||
Value::Function(func) => Ok(lua.create_thread(func)?),
|
||||
val => Err(Error::RuntimeError(format!(
|
||||
"Expected type thread or function, got {}",
|
||||
val.type_name()
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
async fn resume_thread(lua: &Lua, thread: Thread<'_>, args: Vararg<'_>) -> Result<()> {
|
||||
|
|
Loading…
Reference in a new issue