Skip to content

Commit

Permalink
better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Oct 17, 2024
1 parent cf370b6 commit 2983c41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
4 changes: 4 additions & 0 deletions crates/torii/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub enum ParseError {
FromJsonStr(#[from] serde_json::Error),
#[error(transparent)]
FromSlice(#[from] std::array::TryFromSliceError),
#[error("Integer overflow: {0}")]
IntegerOverflow(String),
#[error("Expected data type: {0}")]
ExpectedDataType(String),
}

#[derive(Debug, thiserror::Error)]
Expand Down
2 changes: 2 additions & 0 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ impl From<SchemaError> for Error {
SchemaError::ParseIntError(err) => ParseError::ParseIntError(err).into(),
SchemaError::FromSlice(err) => ParseError::FromSlice(err).into(),
SchemaError::FromStr(err) => ParseError::FromStr(err).into(),
SchemaError::IntegerOverflow(err) => ParseError::IntegerOverflow(err).into(),
SchemaError::ExpectedDataType(err) => ParseError::ExpectedDataType(err).into(),
}
}
}
Expand Down
42 changes: 29 additions & 13 deletions crates/torii/grpc/src/types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub enum SchemaError {
FromSlice(#[from] std::array::TryFromSliceError),
#[error(transparent)]
FromStr(#[from] FromStrError),
#[error("Expected data type: {0}")]
ExpectedDataType(String),
#[error("Integer overflow: {0}")]
IntegerOverflow(String),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
Expand Down Expand Up @@ -68,7 +72,7 @@ impl proto::types::Ty {
if let Ty::Primitive(primitive) = ty {
*primitive = primitive_value.try_into()?;
} else {
return Err(SchemaError::MissingExpectedData("primitive".to_string()));
return Err(SchemaError::ExpectedDataType("primitive".to_string()));
}
}
proto::types::ty::TyType::Enum(enum_value) => {
Expand All @@ -78,7 +82,7 @@ impl proto::types::Ty {
ty.parse(&mut r#enum.options[enum_value.option as usize].ty)?;
}
} else {
return Err(SchemaError::MissingExpectedData("enum".to_string()));
return Err(SchemaError::ExpectedDataType("enum".to_string()));
}
}
proto::types::ty::TyType::Struct(struct_value) => {
Expand All @@ -89,7 +93,7 @@ impl proto::types::Ty {
ty.parse(&mut r#child.ty)?;
}
} else {
return Err(SchemaError::MissingExpectedData("struct".to_string()));
return Err(SchemaError::ExpectedDataType("struct".to_string()));
}
}
proto::types::ty::TyType::Tuple(tuple_value) => {
Expand All @@ -98,7 +102,7 @@ impl proto::types::Ty {
ty.parse(r#child)?;
}
} else {
return Err(SchemaError::MissingExpectedData("tuple".to_string()));
return Err(SchemaError::ExpectedDataType("tuple".to_string()));
}
}
proto::types::ty::TyType::Array(array_value) => {
Expand All @@ -111,14 +115,14 @@ impl proto::types::Ty {
array.push(element);
}
} else {
return Err(SchemaError::MissingExpectedData("array".to_string()));
return Err(SchemaError::ExpectedDataType("array".to_string()));
}
}
proto::types::ty::TyType::Bytearray(bytearray_value) => {
if let Ty::ByteArray(bytearray) = ty {
*bytearray = bytearray_value;
} else {
return Err(SchemaError::MissingExpectedData("bytearray".to_string()));
return Err(SchemaError::ExpectedDataType("bytearray".to_string()));
}
}
}
Expand Down Expand Up @@ -180,16 +184,28 @@ impl TryFrom<proto::types::Primitive> for Primitive {

let primitive = match &value {
proto::types::primitive::PrimitiveType::Bool(bool) => Primitive::Bool(Some(*bool)),
proto::types::primitive::PrimitiveType::I8(int) => Primitive::I8(Some(*int as i8)),
proto::types::primitive::PrimitiveType::I16(int) => Primitive::I16(Some(*int as i16)),
proto::types::primitive::PrimitiveType::I32(int) => Primitive::I32(Some(*int as i32)),
proto::types::primitive::PrimitiveType::I64(int) => Primitive::I64(Some(*int as i64)),
proto::types::primitive::PrimitiveType::I8(int) => Primitive::I8(Some(
i8::try_from(*int)
.map_err(|_| SchemaError::IntegerOverflow(format!("i8: {}", *int)))?,
)),
proto::types::primitive::PrimitiveType::I16(int) => Primitive::I16(Some(
i16::try_from(*int)
.map_err(|_| SchemaError::IntegerOverflow(format!("i16: {}", *int)))?,
)),
proto::types::primitive::PrimitiveType::I32(int) => Primitive::I32(Some(*int)),
proto::types::primitive::PrimitiveType::I64(int) => Primitive::I64(Some(*int)),
proto::types::primitive::PrimitiveType::I128(bytes) => Primitive::I128(Some(
i128::from_be_bytes(bytes.as_slice().try_into().map_err(SchemaError::FromSlice)?),
)),
proto::types::primitive::PrimitiveType::U8(int) => Primitive::U8(Some(*int as u8)),
proto::types::primitive::PrimitiveType::U16(int) => Primitive::U16(Some(*int as u16)),
proto::types::primitive::PrimitiveType::U32(int) => Primitive::U32(Some(*int as u32)),
proto::types::primitive::PrimitiveType::U8(int) => Primitive::U8(Some(
u8::try_from(*int)
.map_err(|_| SchemaError::IntegerOverflow(format!("u8: {}", *int)))?,
)),
proto::types::primitive::PrimitiveType::U16(int) => Primitive::U16(Some(
u16::try_from(*int)
.map_err(|_| SchemaError::IntegerOverflow(format!("u16: {}", *int)))?,
)),
proto::types::primitive::PrimitiveType::U32(int) => Primitive::U32(Some(*int)),
proto::types::primitive::PrimitiveType::U64(int) => Primitive::U64(Some(*int)),
proto::types::primitive::PrimitiveType::U128(bytes) => Primitive::U128(Some(
u128::from_be_bytes(bytes.as_slice().try_into().map_err(SchemaError::FromSlice)?),
Expand Down

0 comments on commit 2983c41

Please sign in to comment.