Skip to content

Commit

Permalink
imporved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jun 10, 2024
1 parent 4668a49 commit b122ce1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ pub(crate) fn run_cli() {
};

handle_cli_error(cli_result);


}

fn handle_cli_error(cli_result: Result<(), CliError>) {
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/native_fn/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ pub(crate) fn create_cmd_native_function() -> NativeFunction {
name: String::from("cmd"),
},
executor: |arguments: HashMap<String, ByteBuffer>| {
let arg0 = get_argument_at(&arguments, 0)?;
let command = get_argument_at(&arguments, 0)?
.as_string()
.map_err(|e| NativeFunctionError::Unknown(e.to_string()))?;

let command = arg0.as_string();
let output = std::process::Command::new(command)
.output()
.map_err(|e|
Expand All @@ -86,7 +87,8 @@ pub(crate) fn create_read_file_native_function() -> NativeFunction {
executor: |arguments: HashMap<String, ByteBuffer>| {
let arg0 = get_argument_at(&arguments, 0)?;

let file_path = arg0.as_string();
let file_path = arg0.as_string()
.map_err(|e| NativeFunctionError::Unknown(e.to_string()))?;

let mut file = File::open(file_path)
.map_err(|e|
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/util/byte_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::compiler::util::encoding::to_shrunk_bytes;
use std::fmt::{Debug, Formatter};
use std::string::FromUtf8Error;

#[derive(Clone)]
pub(crate) struct ByteBuffer {
Expand Down Expand Up @@ -67,8 +68,8 @@ impl ByteBuffer {
(padded.inner[3] as usize)
}

pub(crate) fn as_string(&self) -> String {
String::from_utf8(self.inner.clone()).unwrap()
pub(crate) fn as_string(&self) -> Result<String, FromUtf8Error> {
return String::from_utf8(self.inner.clone())
}
}

Expand Down

0 comments on commit b122ce1

Please sign in to comment.