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) => {
|
Value::String(s) => {
|
||||||
let url = s.to_string_lossy().to_string();
|
let url = s.to_string_lossy().to_string();
|
||||||
let method = "GET".to_string();
|
let method = "GET".to_string();
|
||||||
(url, method, HashMap::new(), None)
|
Ok((url, method, HashMap::new(), None))
|
||||||
}
|
}
|
||||||
Value::Table(tab) => {
|
Value::Table(tab) => {
|
||||||
// Extract url
|
// Extract url
|
||||||
let url = match tab.raw_get::<&str, mlua::String>("url") {
|
let url = match tab.raw_get::<&str, mlua::String>("url") {
|
||||||
Ok(config_url) => config_url.to_string_lossy().to_string(),
|
Ok(config_url) => Ok(config_url.to_string_lossy().to_string()),
|
||||||
Err(_) => {
|
Err(_) => Err(Error::RuntimeError(
|
||||||
return Err(Error::RuntimeError(
|
|
||||||
"Missing 'url' in request config".to_string(),
|
"Missing 'url' in request config".to_string(),
|
||||||
))
|
)),
|
||||||
}
|
}?;
|
||||||
};
|
|
||||||
// Extract method
|
// Extract method
|
||||||
let method = match tab.raw_get::<&str, mlua::String>("method") {
|
let method = match tab.raw_get::<&str, mlua::String>("method") {
|
||||||
Ok(config_method) => config_method.to_string_lossy().trim().to_ascii_uppercase(),
|
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()),
|
Ok(config_body) => Some(config_body.as_bytes().to_owned()),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
(url, method, headers, body)
|
Ok((url, method, headers, body))
|
||||||
}
|
}
|
||||||
value => {
|
value => Err(Error::RuntimeError(format!(
|
||||||
return Err(Error::RuntimeError(format!(
|
|
||||||
"Invalid request config - expected string or table, got {}",
|
"Invalid request config - expected string or table, got {}",
|
||||||
value.type_name()
|
value.type_name()
|
||||||
)))
|
))),
|
||||||
}
|
}?;
|
||||||
};
|
|
||||||
// Convert method string into proper enum
|
// Convert method string into proper enum
|
||||||
let method = method.trim().to_ascii_uppercase();
|
let method = method.trim().to_ascii_uppercase();
|
||||||
let method = match method.as_ref() {
|
let method = match method.as_ref() {
|
||||||
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => &method,
|
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => Ok(&method),
|
||||||
_ => {
|
_ => Err(Error::RuntimeError(format!(
|
||||||
return Err(Error::RuntimeError(format!(
|
|
||||||
"Invalid request config method '{}'",
|
"Invalid request config method '{}'",
|
||||||
&method
|
&method
|
||||||
)))
|
))),
|
||||||
}
|
}?;
|
||||||
};
|
|
||||||
// Create and send the request
|
// Create and send the request
|
||||||
let mut request = ureq::request(method, &url);
|
let mut request = ureq::request(method, &url);
|
||||||
for (header, value) in headers {
|
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<()> {
|
fn process_env_set(_: &Lua, (_, key, value): (Value, String, Option<String>)) -> Result<()> {
|
||||||
// Make sure key is valid, otherwise set_var will panic
|
// Make sure key is valid, otherwise set_var will panic
|
||||||
if key.is_empty() {
|
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('=') {
|
} else if key.contains('=') {
|
||||||
return Err(Error::RuntimeError(
|
Err(Error::RuntimeError(
|
||||||
"Key must not contain the equals character '='".to_string(),
|
"Key must not contain the equals character '='".to_string(),
|
||||||
));
|
))
|
||||||
} else if key.contains('\0') {
|
} else if key.contains('\0') {
|
||||||
return Err(Error::RuntimeError(
|
Err(Error::RuntimeError(
|
||||||
"Key must not contain the NUL character".to_string(),
|
"Key must not contain the NUL character".to_string(),
|
||||||
));
|
))
|
||||||
}
|
} else {
|
||||||
match value {
|
match value {
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
// Make sure value is valid, otherwise set_var will panic
|
// Make sure value is valid, otherwise set_var will panic
|
||||||
if value.contains('\0') {
|
if value.contains('\0') {
|
||||||
return Err(Error::RuntimeError(
|
Err(Error::RuntimeError(
|
||||||
"Value must not contain the NUL character".to_string(),
|
"Value must not contain the NUL character".to_string(),
|
||||||
));
|
))
|
||||||
}
|
} else {
|
||||||
env::set_var(&key, &value);
|
env::set_var(&key, &value);
|
||||||
}
|
|
||||||
None => env::remove_var(&key),
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
env::remove_var(&key);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_env_iter<'lua>(lua: &'lua Lua, (_, _): (Value<'lua>, ())) -> Result<Function<'lua>> {
|
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>> {
|
fn get_or_create_thread_from_arg<'a>(lua: &'a Lua, arg: Value<'a>) -> Result<Thread<'a>> {
|
||||||
Ok(match arg {
|
match arg {
|
||||||
Value::Thread(thread) => thread,
|
Value::Thread(thread) => Ok(thread),
|
||||||
Value::Function(func) => lua.create_thread(func)?,
|
Value::Function(func) => Ok(lua.create_thread(func)?),
|
||||||
val => {
|
val => Err(Error::RuntimeError(format!(
|
||||||
return Err(Error::RuntimeError(format!(
|
|
||||||
"Expected type thread or function, got {}",
|
"Expected type thread or function, got {}",
|
||||||
val.type_name()
|
val.type_name()
|
||||||
)))
|
))),
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn resume_thread(lua: &Lua, thread: Thread<'_>, args: Vararg<'_>) -> Result<()> {
|
async fn resume_thread(lua: &Lua, thread: Thread<'_>, args: Vararg<'_>) -> Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue