Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error types and messages by using thiserror #600

Merged
merged 119 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
073b828
use thiserror crate
colifran Mar 11, 2024
0459a74
created error enum and added more specific error types
colifran Mar 11, 2024
d665281
updated transmute error to more descriptive error types and added uni…
colifran Mar 11, 2024
03485e0
remove commented out test case
colifran Mar 11, 2024
dff7332
updated resource type error message
colifran Mar 11, 2024
3702e3f
updated resource type error message
colifran Mar 11, 2024
5e332d4
unsupported language error
colifran Mar 11, 2024
3d4f8ec
update thiserror dependency
colifran Mar 12, 2024
65369fd
updates for synthesizers and implemented new error types in python, t…
colifran Mar 14, 2024
cf4402d
update csharp synthesizer
colifran Mar 15, 2024
b4e4220
formatting
colifran Mar 15, 2024
03802e4
Merge branch 'main' into colifran/use-thiserror-crate
colifran Mar 26, 2024
449ad7a
formatting
colifran Mar 26, 2024
97c8efe
primitive error and csharp synthesizer
colifran Mar 26, 2024
410c30c
golang synthesizer
colifran Mar 26, 2024
164c0f8
formatting
colifran Mar 26, 2024
1f3603a
formatting
colifran Mar 26, 2024
38068df
formatting
colifran Mar 26, 2024
3ad6940
formatting
colifran Mar 26, 2024
4dc9a05
last formatting update...
colifran Mar 26, 2024
d8e0ace
formatting
colifran Mar 26, 2024
fc1b23c
formatting
colifran Mar 26, 2024
d658baf
formatting
colifran Mar 26, 2024
24a5bd5
unit tests
colifran Mar 26, 2024
2976eae
formatting
colifran Mar 26, 2024
3bff751
formattinh
colifran Mar 26, 2024
414f4aa
formatting
colifran Mar 26, 2024
424dd62
refactor
colifran Mar 26, 2024
49b0638
formatting
colifran Mar 26, 2024
616bbcb
unit tests
colifran Mar 27, 2024
c91fae4
unit tests
colifran Mar 27, 2024
43ce434
go home clippy
colifran Mar 27, 2024
7558a1b
codecov test
colifran Mar 27, 2024
40604a4
end-to-end stack set
colifran Mar 27, 2024
a79fc14
no synth java
colifran Mar 27, 2024
5aec865
ec2 encryption end-to-end
colifran Mar 27, 2024
535b889
formatting
colifran Mar 27, 2024
57a5947
invalid org unit test for csharp
colifran Mar 27, 2024
7077eb4
clippy go home
colifran Mar 27, 2024
98dd5ab
formatting
colifran Mar 27, 2024
a8934ad
updated ec2 encryption end-to-end test
colifran Mar 27, 2024
a5fcb7e
updated ec2 encryption end-to-end template
colifran Mar 27, 2024
4b87ea3
unit tests for bad org for all synthesizers
colifran Mar 27, 2024
9e17c83
invalid resource object structure unit test
colifran Mar 27, 2024
6148ffd
clippy
colifran Mar 27, 2024
89d48d6
ec2 encryption update
colifran Mar 27, 2024
2086eec
another python bug fix and updated template
colifran Mar 27, 2024
0cd4455
formatting
colifran Mar 27, 2024
9538bd8
more bug fixes more template updates
colifran Mar 27, 2024
23d2a97
fn split in csharp synthesizer test
colifran Mar 27, 2024
396317a
remove prints
colifran Mar 27, 2024
48adb4e
condition map unit test
colifran Mar 27, 2024
0923601
more unit tests
colifran Mar 27, 2024
ae85ffc
formatting
colifran Mar 27, 2024
5c697cc
resource unit tests
colifran Mar 27, 2024
c327c8a
formatting
colifran Mar 27, 2024
580393e
select index range error test
colifran Mar 27, 2024
be8cf99
formatting
colifran Mar 27, 2024
c338d74
csharp unit tests
colifran Mar 27, 2024
b7c72d7
formatting
colifran Mar 27, 2024
dd4e711
csharp unit tests
colifran Mar 27, 2024
2ad8fc7
added synthesizer test files for better test organization
colifran Mar 29, 2024
c0a14ce
remove unused imports
colifran Mar 29, 2024
b54b136
formatting
colifran Mar 29, 2024
63c0637
formatting
colifran Mar 29, 2024
5d85c0c
java unit tests
colifran Mar 29, 2024
bbd2bb1
fix failing java unit test
colifran Mar 29, 2024
0cb8d11
clippy
colifran Mar 29, 2024
b251602
golang unit tests
colifran Mar 29, 2024
e2061d5
add trailer to go context
colifran Mar 29, 2024
8ae35ee
resource ir double unit tests for go
colifran Mar 30, 2024
9d0746c
formatting
colifran Mar 30, 2024
abe020b
emit tag value unit tests for java
colifran Mar 30, 2024
0253f96
importer unit test
colifran Mar 30, 2024
18a3311
golang resource ir object unit testing
colifran Mar 30, 2024
918632e
formatting
colifran Mar 30, 2024
ccb2b6f
formatting
colifran Mar 30, 2024
4722689
java unit test
colifran Mar 30, 2024
4b90fee
java unit test
colifran Mar 30, 2024
471697e
more java unit tests
colifran Mar 30, 2024
4318110
formatting
colifran Mar 30, 2024
b974087
csharp unit test
colifran Mar 30, 2024
7fbf2bc
python unit tests
colifran Mar 30, 2024
c914cd8
formatting
colifran Mar 30, 2024
e031de3
refactor resource ir code for csharp synthesizer
colifran Mar 30, 2024
2abea1b
formatting
colifran Mar 30, 2024
03d8eb6
formatting
colifran Mar 30, 2024
2c9105a
formatting
colifran Mar 30, 2024
d52f0f3
refactor sub parse error code
colifran Mar 30, 2024
83c690f
clippy
colifran Mar 30, 2024
9172cee
clippy
colifran Mar 30, 2024
fd8f7af
Merge branch 'main' into colifran/use-thiserror-crate
colifran Mar 30, 2024
5455cb3
golang and cshap unit tests
colifran Mar 30, 2024
00915c5
formatting
colifran Mar 30, 2024
52ef3dc
formatting
colifran Mar 30, 2024
c931ff3
unit tests
colifran Mar 30, 2024
8b2c6a9
golang unit test
colifran Mar 30, 2024
73acaf6
test decorator
colifran Mar 30, 2024
b9c15a6
java update policy unit tests
colifran Mar 30, 2024
84e1731
remove println
colifran Mar 30, 2024
8e27a4e
refactor main -- testing to see how coverage is impacted
colifran Mar 31, 2024
f12703c
revert main changes
colifran Mar 31, 2024
01c159e
unsupported language JsError
colifran Mar 31, 2024
f123b4c
formatting
colifran Mar 31, 2024
a8a8884
testiing codecov diff
colifran Mar 31, 2024
a1502bf
typescrip alex org unit test
colifran Mar 31, 2024
6abebf2
java resource condition unit test
colifran Mar 31, 2024
692696e
resource ir cidr string unit test hava
colifran Mar 31, 2024
4ab5206
golang primitive unit tests
colifran Mar 31, 2024
5ced4ae
condition ir shouldn't implement csharp emitter
colifran Mar 31, 2024
2ad9849
golang primitive unknown test
colifran Mar 31, 2024
a849e73
ec2_encryption csharp
colifran Mar 31, 2024
7925fb4
csharp unit tests
colifran Mar 31, 2024
b8142bb
update unit test
colifran Mar 31, 2024
c17b03b
csharp unit tests
colifran Mar 31, 2024
eebb604
formatting
colifran Mar 31, 2024
da7de08
unit tests
colifran Mar 31, 2024
04e1ce5
formatting
colifran Mar 31, 2024
41900cb
Merge branch 'main' into colifran/use-thiserror-crate
cdklabs-automation Apr 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ serde = { version = "^1.0.197", features = ["derive"] }
serde-enum-str = "^0.4.0"
serde_with = "^3.7.0"
serde_yaml = "^0.9.34"
thiserror = "^1.0.57"
topological-sort = "^0.2.2"
voca_rs = "^1.15.2"
wasm-bindgen = "^0.2.92"
Expand Down
59 changes: 29 additions & 30 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
use std::error::Error;
use std::fmt;
use thiserror::Error;

#[derive(Debug)]
pub struct TransmuteError {
details: String,
#[derive(Debug, Error)]
pub enum Error {
#[error("{message}")]
ImportInstructionError { message: String },
#[error("{message}")]
ResourceTranslationError { message: String },
#[error("{message}")]
SubParseError { message: String },
#[error("{message}")]
ResourceInstructionError { message: String },
#[error("{message}")]
ResourceTypeError { message: String },
#[error(transparent)]
YamlParseError {
#[from]
err: serde_yaml::Error,
},
#[error("{language} is not a supported language")]
UnsupportedLanguageError { language: String },
#[error(transparent)]
IOError {
#[from]
err: std::io::Error,
},
#[error("{message}")]
TypeReferenceError { message: String },
#[error("{message}")]
PrimitiveError { message: String },
}

impl TransmuteError {
#[inline(always)]
pub(crate) fn new(msg: impl ToString) -> TransmuteError {
TransmuteError {
details: msg.to_string(),
}
}
}

impl From<serde_yaml::Error> for TransmuteError {
#[inline]
fn from(val: serde_yaml::Error) -> Self {
TransmuteError::new(val)
}
}

impl fmt::Display for TransmuteError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TransmuteError: {}", self.details)
}
}

impl Error for TransmuteError {}

#[cfg(test)]
mod tests;
69 changes: 61 additions & 8 deletions src/errors/tests.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
use serde::de::Error;

use super::*;
#[test]
fn test_import_instruction_error() {
let error = crate::Error::ImportInstructionError {
message: "Import instruction error".to_string(),
};
assert_eq!(error.to_string(), "Import instruction error");
}

#[test]
fn test_resource_translation_error() {
let error = crate::Error::ResourceTranslationError {
message: "Resource instruction error".to_string(),
};
assert_eq!(error.to_string(), "Resource instruction error");
}

#[test]
fn test_transmute_error() {
let error = TransmuteError::new("Test error message");
fn test_sub_parse_error() {
let error = crate::Error::SubParseError {
message: "Sub parse error".to_string(),
};
assert_eq!(error.to_string(), "Sub parse error");
}

#[test]
fn test_resource_instruction_error() {
let error = crate::Error::ResourceInstructionError {
message: "Resource instruction error".to_string(),
};
assert_eq!(error.to_string(), "Resource instruction error");
}

assert_eq!(error.details, "Test error message");
assert_eq!(error.to_string(), "TransmuteError: Test error message");
#[test]
fn test_resource_type_error() {
let error = crate::Error::ResourceTypeError {
message: "Resource type error".to_string(),
};
assert_eq!(error.to_string(), "Resource type error");
}

#[test]
fn test_transmute_error_from() {
fn test_yaml_parse_error() {
let yaml_error = serde_yaml::Error::custom("YAML parsing error");
let transmute_error: TransmuteError = yaml_error.into();
let error: crate::Error = yaml_error.into();
assert_eq!(error.to_string(), "YAML parsing error");
}

#[test]
fn test_unsupported_language_error() {
let error = crate::Error::UnsupportedLanguageError {
language: "php".to_string(),
};
assert_eq!(error.to_string(), "php is not a supported language");
}

assert_eq!(transmute_error.details, "YAML parsing error");
#[test]
fn test_type_reference_error() {
let error = crate::Error::TypeReferenceError {
message: "Type reference error".to_string(),
};
assert_eq!(error.to_string(), "Type reference error");
}

#[test]
fn test_primitive_error() {
let error = crate::Error::PrimitiveError {
message: "Primitive error".to_string(),
};
assert_eq!(error.to_string(), "Primitive error");
}
4 changes: 2 additions & 2 deletions src/ir/constructor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{parser::parameters::Parameter, Hasher};
use indexmap::IndexMap;
use voca_rs::case::camel_case;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Constructor {
pub inputs: Vec<ConstructorParameter>,
}
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Constructor {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct ConstructorParameter {
pub name: String,
pub description: Option<String>,
Expand Down
13 changes: 8 additions & 5 deletions src/ir/importer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use indexmap::IndexMap;

use crate::parser::resource::ResourceAttributes;
use crate::{Hasher, TransmuteError};
use crate::{Error, Hasher};

// ImportInstruction look something like:
// import * as $name from '$path[0]/$path[1]...';
Expand All @@ -17,7 +17,7 @@ pub struct ImportInstruction {
impl ImportInstruction {
pub(super) fn from(
parse_tree: &IndexMap<String, ResourceAttributes, Hasher>,
) -> Result<Vec<Self>, TransmuteError> {
) -> Result<Vec<Self>, Error> {
let mut type_names = HashSet::new();
for (_, resource) in parse_tree {
let type_name = &resource.resource_type;
Expand All @@ -29,9 +29,9 @@ impl ImportInstruction {
}) {
triple
} else {
return Err(TransmuteError::new(format!(
"invalid resource type name: {type_name}"
)));
return Err(Error::ImportInstructionError {
message: format!("Invalid resource type name: {type_name}"),
});
};

let service_name = if service.to_string().to_lowercase().eq("serverless") {
Expand Down Expand Up @@ -70,3 +70,6 @@ struct TypeName {
organization: String,
service: Option<String>,
}

#[cfg(test)]
mod tests;
24 changes: 24 additions & 0 deletions src/ir/importer/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use indexmap::IndexMap;

use crate::parser::resource::ResourceAttributes;

use super::ImportInstruction;

#[test]
fn test_invalid_resource_type_name() {
let resource_attributes = ResourceAttributes {
resource_type: "AWS:Invalid:Resource:Type".to_string(),
condition: Option::None,
metadata: Option::None,
depends_on: vec![],
update_policy: Option::None,
deletion_policy: Option::None,
properties: IndexMap::new(),
};
let parse_tree = IndexMap::from([("Resource".to_string(), resource_attributes)]);
let import_instruction = ImportInstruction::from(&parse_tree).unwrap_err();
assert_eq!(
"Invalid resource type name: AWS:Invalid:Resource:Type",
import_instruction.to_string()
);
}
6 changes: 3 additions & 3 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ir::importer::ImportInstruction;
use crate::ir::mappings::MappingInstruction;
use crate::ir::outputs::OutputInstruction;
use crate::ir::resources::ResourceInstruction;
use crate::{CloudformationParseTree, TransmuteError};
use crate::{CloudformationParseTree, Error};

use self::reference::{Origin, PseudoParameter};

Expand All @@ -20,7 +20,7 @@ pub mod reference;
pub mod resources;
pub mod sub;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct CloudformationProgramIr {
pub description: Option<String>,
pub transforms: Vec<String>,
Expand All @@ -40,7 +40,7 @@ impl CloudformationProgramIr {
pub fn from(
parse_tree: CloudformationParseTree,
schema: &Schema,
) -> Result<CloudformationProgramIr, TransmuteError> {
) -> Result<CloudformationProgramIr, Error> {
let origins = ReferenceOrigins::new(&parse_tree);

Ok(CloudformationProgramIr {
Expand Down
4 changes: 2 additions & 2 deletions src/ir/outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ir::resources::{ResourceIr, ResourceTranslator};
use crate::parser::output::Output;
use crate::parser::resource::ResourceValue;
use crate::util::Hasher;
use crate::TransmuteError;
use crate::Error;

use super::ReferenceOrigins;

Expand All @@ -23,7 +23,7 @@ impl OutputInstruction {
parse_tree: IndexMap<String, Output, Hasher>,
schema: &Schema,
origins: &ReferenceOrigins,
) -> Result<Vec<Self>, TransmuteError> {
) -> Result<Vec<Self>, Error> {
let mut list = Vec::with_capacity(parse_tree.len());

for (name, output) in parse_tree {
Expand Down
Loading
Loading