Skip to content

Commit

Permalink
Some fixes to the previous PR
Browse files Browse the repository at this point in the history
  • Loading branch information
arduano committed Apr 21, 2024
1 parent 7ee35a0 commit a5d4937
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 68 deletions.
2 changes: 1 addition & 1 deletion nixjs-rt/src/errors/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class NixMissingAttributeError {
constructor(public readonly attrPath: string[]) {}

toDefaultErrorMessage(): ErrorMessage {
return err`Attribute '${highlighted(this.attrPath.join("."))}' is missing'`;
return err`Attribute '${highlighted(this.attrPath.join("."))}' is missing`;
}
}

Expand Down
7 changes: 1 addition & 6 deletions nixjs-rt/src/errors/errorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ export function stringifyErrorMessage(message: ErrorMessage): string {
return message.map((part) => part.value).join("");
}

type ErrorMessageBuilderPart =
| string
// | NixTypeClass
// | NixTypeClass[]
// | NixTypeInstance
| ErrorMessage;
type ErrorMessageBuilderPart = string | ErrorMessage;

function builderPartToErrMessage(part: ErrorMessageBuilderPart): ErrorMessage {
if (typeof part === "string") {
Expand Down
119 changes: 58 additions & 61 deletions src/eval/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::helpers::{call_js_function, get_js_value_key, is_nixrt_type};
use super::{
helpers::{call_js_function, get_js_value_key, is_nixrt_type},
types::NixTypeKind,
};

#[derive(Debug)]
pub struct NixError {
Expand Down Expand Up @@ -56,19 +59,6 @@ pub enum NixErrorMessagePart {
Highlighted(String),
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum NixTypeKind {
Bool,
Float,
Int,
List,
Null,
String,
Path,
Lambda,
Set,
}

#[derive(Debug, PartialEq, Eq)]
pub enum NixErrorKind {
Abort {
Expand Down Expand Up @@ -136,53 +126,60 @@ fn try_js_error_to_rust(

let kind_js = get_js_value_key(scope, &error, "kind")?;

let kind = if is_nixrt_type(scope, &nixrt, &kind_js, "NixAbortError")? {
let message_js = get_js_value_key(scope, &kind_js, "message")?;
let message = message_js.to_rust_string_lossy(scope);
NixErrorKind::Abort { message }
} else if is_nixrt_type(scope, &nixrt, &kind_js, "NixCouldntFindVariableError")? {
let var_name_js = get_js_value_key(scope, &kind_js, "varName")?;
let var_name = var_name_js.to_rust_string_lossy(scope);
NixErrorKind::CouldntFindVariable { var_name }
} else if is_nixrt_type(scope, &nixrt, &kind_js, "NixTypeMismatchError")? {
let expected_js = get_js_value_key(scope, &kind_js, "expected")?;
let got_js = get_js_value_key(scope, &kind_js, "got")?;

let mut expected = nix_type_class_array_to_enum_array(scope, nixrt, expected_js)?;
let got = nix_type_class_to_enum(scope, nixrt, got_js)?;

// Sort expected array, for normalization
expected.sort_unstable();

NixErrorKind::TypeMismatch { expected, got }
} else if is_nixrt_type(scope, &nixrt, &kind_js, "NixOtherError")? {
let message_js = get_js_value_key(scope, &kind_js, "message")?;
let message = message_js.to_rust_string_lossy(scope);
NixErrorKind::Other { message }
} else if is_nixrt_type(scope, &nixrt, &kind_js, "NixMissingAttributeError")? {
let attr_path_js = get_js_value_key(scope, &kind_js, "attrPath")?;
let attr_path = js_string_array_to_rust_string_array(scope, attr_path_js)?;
NixErrorKind::MissingAttribute { attr_path }
} else if is_nixrt_type(scope, &nixrt, &kind_js, "NixAttributeAlreadyDefinedError")? {
let attr_path_js = get_js_value_key(scope, &kind_js, "attrPath")?;
let attr_path = js_string_array_to_rust_string_array(scope, attr_path_js)?;
NixErrorKind::AttributeAlreadyDefined { attr_path }
} else if is_nixrt_type(
scope,
&nixrt,
&kind_js,
"NixFunctionCallWithoutArgumentError",
)? {
let argument_js = get_js_value_key(scope, &kind_js, "argument")?;
let argument = argument_js.to_rust_string_lossy(scope);
NixErrorKind::FunctionCallWithoutArgument { argument }
} else {
return Ok(NixError {
message: vec![NixErrorMessagePart::Plain("An error occurred.".to_owned())],
kind: NixErrorKind::UnexpectedJsError {
message: error.to_rust_string_lossy(scope),
},
});
let err_constructor = get_js_value_key(scope, &error, "constructor")?;
let err_name = get_js_value_key(scope, &err_constructor, "name")?;

let kind = match err_name.to_rust_string_lossy(scope).as_str() {
"NixAbortError" => {
let message_js = get_js_value_key(scope, &kind_js, "message")?;
let message = message_js.to_rust_string_lossy(scope);
NixErrorKind::Abort { message }
}
"NixCouldntFindVariableError" => {
let var_name_js = get_js_value_key(scope, &kind_js, "varName")?;
let var_name = var_name_js.to_rust_string_lossy(scope);
NixErrorKind::CouldntFindVariable { var_name }
}
"NixTypeMismatchError" => {
let expected_js = get_js_value_key(scope, &kind_js, "expected")?;
let got_js = get_js_value_key(scope, &kind_js, "got")?;

let mut expected = nix_type_class_array_to_enum_array(scope, nixrt, expected_js)?;
let got = nix_type_class_to_enum(scope, nixrt, got_js)?;

// Sort expected array, for normalization
expected.sort_unstable();

NixErrorKind::TypeMismatch { expected, got }
}
"NixOtherError" => {
let message_js = get_js_value_key(scope, &kind_js, "message")?;
let message = message_js.to_rust_string_lossy(scope);
NixErrorKind::Other { message }
}
"NixMissingAttributeError" => {
let attr_path_js = get_js_value_key(scope, &kind_js, "attrPath")?;
let attr_path = js_string_array_to_rust_string_array(scope, attr_path_js)?;
NixErrorKind::MissingAttribute { attr_path }
}
"NixAttributeAlreadyDefinedError" => {
let attr_path_js = get_js_value_key(scope, &kind_js, "attrPath")?;
let attr_path = js_string_array_to_rust_string_array(scope, attr_path_js)?;
NixErrorKind::AttributeAlreadyDefined { attr_path }
}
"NixFunctionCallWithoutArgumentError" => {
let argument_js = get_js_value_key(scope, &kind_js, "argument")?;
let argument = argument_js.to_rust_string_lossy(scope);
NixErrorKind::FunctionCallWithoutArgument { argument }
}
_ => {
return Ok(NixError {
message: vec![NixErrorMessagePart::Plain("An error occurred.".to_owned())],
kind: NixErrorKind::UnexpectedJsError {
message: error.to_rust_string_lossy(scope),
},
});
}
};

Ok(NixError { message, kind })
Expand Down
13 changes: 13 additions & 0 deletions src/eval/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ pub enum Value {
Str(String),
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum NixTypeKind {
Bool,
Float,
Int,
List,
Null,
String,
Path,
Lambda,
Set,
}

pub type EvalResult = Result<Value, NixError>;

pub fn js_value_to_nix(
Expand Down

0 comments on commit a5d4937

Please sign in to comment.