mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New set_error command #68
This commit is contained in:
parent
887a17bb3c
commit
e1f399b0c7
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.8
|
||||
|
||||
* New set_error command #68
|
||||
* New is_array command #70
|
||||
* Test errors in duckscript based tests will break build flow.
|
||||
* New --version cli option.
|
||||
|
|
32
docs/sdk.md
32
docs/sdk.md
|
@ -34,6 +34,7 @@
|
|||
* [std::error::GetLastError (get_last_error)](#std__error__GetLastError)
|
||||
* [std::error::GetLastErrorLine (get_last_error_line)](#std__error__GetLastErrorLine)
|
||||
* [std::error::GetLastErrorSource (get_last_error_source)](#std__error__GetLastErrorSource)
|
||||
* [std::error::SetError (set_error)](#std__error__SetError)
|
||||
* [std::error::SetExitOnError (exit_on_error, set_exit_on_error)](#std__error__SetExitOnError)
|
||||
* [std::fs::CopyPath (cp)](#std__fs__CopyPath)
|
||||
* [std::fs::CreateDirectory (mkdir)](#std__fs__CreateDirectory)
|
||||
|
@ -1290,6 +1291,37 @@ echo Error Source File: ${source}
|
|||
#### Aliases:
|
||||
get_last_error_source
|
||||
|
||||
<a name="std__error__SetError"></a>
|
||||
## std::error::SetError
|
||||
```sh
|
||||
set_error message
|
||||
```
|
||||
|
||||
Sets the last error which is accessible via get_last_error.<br>
|
||||
This command will not trigger the on_error command flow.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The error message.
|
||||
|
||||
#### Return Value
|
||||
|
||||
None
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
set_error "my error message"
|
||||
|
||||
error = get_last_error
|
||||
|
||||
assert_eq ${error} "my error message"
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
set_error
|
||||
|
||||
<a name="std__error__SetExitOnError"></a>
|
||||
## std::error::SetExitOnError
|
||||
```sh
|
||||
|
|
|
@ -3,6 +3,7 @@ mod get_last_error;
|
|||
mod get_last_error_line;
|
||||
mod get_last_error_source;
|
||||
mod on_error;
|
||||
mod set_error;
|
||||
|
||||
use crate::utils::pckg;
|
||||
use crate::utils::state::get_core_sub_state_for_command;
|
||||
|
@ -36,6 +37,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(get_last_error_line::create(&package))?;
|
||||
commands.set(get_last_error_source::create(&package))?;
|
||||
commands.set(on_error::create(&package))?;
|
||||
commands.set(set_error::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
24
duckscript_sdk/src/sdk/std/on_error/set_error/help.md
Normal file
24
duckscript_sdk/src/sdk/std/on_error/set_error/help.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
```sh
|
||||
set_error message
|
||||
```
|
||||
|
||||
Sets the last error which is accessible via get_last_error.<br>
|
||||
This command will not trigger the on_error command flow.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The error message.
|
||||
|
||||
#### Return Value
|
||||
|
||||
None
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
set_error "my error message"
|
||||
|
||||
error = get_last_error
|
||||
|
||||
assert_eq ${error} "my error message"
|
||||
```
|
63
duckscript_sdk/src/sdk/std/on_error/set_error/mod.rs
Executable file
63
duckscript_sdk/src/sdk/std/on_error/set_error/mod.rs
Executable file
|
@ -0,0 +1,63 @@
|
|||
use crate::sdk::std::on_error::STATE_KEY;
|
||||
use crate::utils::pckg;
|
||||
use crate::utils::state::get_core_sub_state_for_command;
|
||||
use duckscript::types::command::{Command, CommandResult, Commands};
|
||||
use duckscript::types::instruction::Instruction;
|
||||
use duckscript::types::runtime::StateValue;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "SetError")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["set_error".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn requires_context(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run_with_context(
|
||||
&self,
|
||||
arguments: Vec<String>,
|
||||
state: &mut HashMap<String, StateValue>,
|
||||
_variables: &mut HashMap<String, String>,
|
||||
_output_variable: Option<String>,
|
||||
_instructions: &Vec<Instruction>,
|
||||
_commands: &mut Commands,
|
||||
line: usize,
|
||||
) -> CommandResult {
|
||||
if !arguments.is_empty() {
|
||||
let error = arguments[0].clone();
|
||||
|
||||
let sub_state = get_core_sub_state_for_command(state, STATE_KEY.to_string());
|
||||
sub_state.insert("error".to_string(), StateValue::String(error));
|
||||
sub_state.insert("line".to_string(), StateValue::String(line.to_string()));
|
||||
sub_state.remove("source");
|
||||
|
||||
CommandResult::Continue(None)
|
||||
} else {
|
||||
CommandResult::Error("Invalid input provided.".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
38
duckscript_sdk/src/sdk/std/on_error/set_error/mod_test.rs
Normal file
38
duckscript_sdk/src/sdk/std/on_error/set_error/mod_test.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_invalid_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = set_error", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_valid() {
|
||||
let context = test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = set_error text",
|
||||
CommandValidation::None,
|
||||
);
|
||||
|
||||
let sub_state_value = context.state.get("duckscriptsdk::on_error").unwrap();
|
||||
match sub_state_value {
|
||||
StateValue::SubState(sub_state) => {
|
||||
match sub_state.get("error").unwrap() {
|
||||
StateValue::String(value) => assert_eq!(value, "text"),
|
||||
_ => panic!("Invalid value type."),
|
||||
};
|
||||
match sub_state.get("line").unwrap() {
|
||||
StateValue::String(value) => assert_eq!(value, "0"),
|
||||
_ => panic!("Invalid value type."),
|
||||
};
|
||||
assert!(sub_state.get("source").is_none());
|
||||
}
|
||||
_ => panic!("Invalid sub state type."),
|
||||
}
|
||||
}
|
8
test/std/on_error/set_error_test.ds
Normal file
8
test/std/on_error/set_error_test.ds
Normal file
|
@ -0,0 +1,8 @@
|
|||
|
||||
function test_set_error
|
||||
set_error "my error message"
|
||||
|
||||
error = get_last_error
|
||||
|
||||
assert_eq ${error} "my error message"
|
||||
end
|
Loading…
Reference in a new issue