diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-08-06 07:22:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-06 02:22:19 -0500 |
| commit | 5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch) | |
| tree | b006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-protocol/src/packets/game | |
| parent | 1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff) | |
| download | azalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz | |
Better errors (#14)
* make reading use thiserror
* finish implementing all the error things
* clippy warnings related to ok_or
* fix some errors in other places
* thiserror in more places
* don't use closures in a couple places
* errors in writing packet
* rip backtraces
* change some BufReadError::Custom to UnexpectedEnumVariant
* Errors say what packet is bad
* error on leftover data and fix
it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-protocol/src/packets/game')
13 files changed, 44 insertions, 35 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs index f45946d5..622b9ec7 100755 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -1,3 +1,4 @@ +use azalea_buf::BufReadError; use azalea_buf::McBuf; use azalea_buf::McBufVarReadable; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; @@ -24,7 +25,7 @@ pub struct BrigadierNumber<T> { max: Option<T>, } impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let flags = buf.read_byte()?; let min = if flags & 0x01 != 0 { Some(T::read_from(buf)?) @@ -123,7 +124,7 @@ pub enum BrigadierParser { } impl McBufReadable for BrigadierParser { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let parser_type = u32::var_read_from(buf)?; match parser_type { @@ -190,14 +191,16 @@ impl McBufReadable for BrigadierParser { 45 => Ok(BrigadierParser::TemplateMirror), 46 => Ok(BrigadierParser::TemplateRotation), 47 => Ok(BrigadierParser::Uuid), - _ => Err(format!("Unknown BrigadierParser type: {}", parser_type)), + _ => Err(BufReadError::UnexpectedEnumVariant { + id: parser_type as i32, + }), } } } // TODO: BrigadierNodeStub should have more stuff impl McBufReadable for BrigadierNodeStub { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let flags = u8::read_from(buf)?; if flags > 31 { println!( diff --git a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs index 8b52b8cf..d85f39cb 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::{McBufReadable, McBufVarReadable, McBufWritable}; +use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufWritable}; use azalea_core::ParticleData; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -20,7 +20,7 @@ pub struct ClientboundLevelParticlesPacket { } impl McBufReadable for ClientboundLevelParticlesPacket { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let particle_id = u32::var_read_from(buf)?; let override_limiter = bool::read_from(buf)?; let x = f64::read_from(buf)?; diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs index 9a312276..5ab187ae 100755 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable}; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -20,7 +20,7 @@ pub struct PlayerAbilitiesFlags { } impl McBufReadable for PlayerAbilitiesFlags { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let byte = buf.read_byte()?; Ok(PlayerAbilitiesFlags { invulnerable: byte & 1 != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs index a2caae4b..7414e10c 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::{BitSet, McBuf, McBufReadable, McBufVarWritable}; +use azalea_buf::{BitSet, BufReadError, McBuf, McBufReadable, McBufVarWritable}; use azalea_buf::{McBufVarReadable, McBufWritable}; use azalea_chat::component::Component; use azalea_crypto::{MessageSignature, SignedMessageHeader}; @@ -74,13 +74,15 @@ pub enum FilterMask { } impl McBufReadable for FilterMask { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let filter_mask = u32::var_read_from(buf)?; match filter_mask { 0 => Ok(FilterMask::PassThrough), 1 => Ok(FilterMask::FullyFiltered), 2 => Ok(FilterMask::PartiallyFiltered(BitSet::read_from(buf)?)), - _ => Err("Invalid filter mask".to_string()), + _ => Err(BufReadError::UnexpectedEnumVariant { + id: filter_mask as i32, + }), } } } diff --git a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs index 614a351e..d53774b7 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_chat::component::Component; use packet_macros::ClientboundGamePacket; @@ -63,7 +63,7 @@ pub struct RemovePlayer { } impl McBufReadable for Action { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let id = buf.read_byte()?; Ok(match id { 0 => Action::AddPlayer(Vec::<AddPlayer>::read_from(buf)?), @@ -71,7 +71,7 @@ impl McBufReadable for Action { 2 => Action::UpdateLatency(Vec::<UpdateLatency>::read_from(buf)?), 3 => Action::UpdateDisplayName(Vec::<UpdateDisplayName>::read_from(buf)?), 4 => Action::RemovePlayer(Vec::<RemovePlayer>::read_from(buf)?), - _ => panic!("Unknown player info action id: {}", id), + _ => return Err(BufReadError::UnexpectedEnumVariant { id: id.into() }), }) } } diff --git a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs index 758e6bb0..d4c9dcb5 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable}; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -28,7 +28,7 @@ pub struct RelativeArguments { } impl McBufReadable for RelativeArguments { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let byte = buf.read_byte()?; Ok(RelativeArguments { x: byte & 0b1 != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs index e6a5bf7f..05e96f69 100644 --- a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; @@ -41,7 +41,7 @@ impl McBufWritable for State { } } impl McBufReadable for State { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let state = buf.read_varint()?; Ok(match state { 0 => State::Init, diff --git a/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs b/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs index 0a83edf0..5dee398e 100644 --- a/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; use azalea_core::{ChunkSectionBlockPos, ChunkSectionPos}; use packet_macros::ClientboundGamePacket; @@ -18,7 +18,7 @@ pub struct BlockStateWithPosition { } impl McBufReadable for BlockStateWithPosition { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let data = u64::var_read_from(buf)?; let position_part = data & 4095; let state = (data >> 12) as u32; diff --git a/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs index 102e90e1..35f0d251 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_core::Slot; use packet_macros::ClientboundGamePacket; @@ -17,13 +17,17 @@ pub struct EquipmentSlots { } impl McBufReadable for EquipmentSlots { - fn read_from(buf: &mut impl std::io::Read) -> Result<Self, String> { + fn read_from(buf: &mut impl std::io::Read) -> Result<Self, BufReadError> { let mut slots = vec![]; loop { let equipment_byte = u8::read_from(buf)?; - let equipment_slot = EquipmentSlot::from_byte(equipment_byte & 127) - .ok_or_else(|| format!("Invalid equipment slot byte {}", equipment_byte))?; + let equipment_slot = + EquipmentSlot::from_byte(equipment_byte & 127).ok_or_else(|| { + BufReadError::UnexpectedEnumVariant { + id: equipment_byte.into(), + } + })?; let item = Slot::read_from(buf)?; slots.push((equipment_slot, item)); if equipment_byte & 128 == 0 { diff --git a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs index 396c0fa4..88cb15e9 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::{McBuf, McBufReadable, McBufWritable}; +use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; use azalea_chat::component::Component; use azalea_core::{ResourceLocation, Slot}; use packet_macros::ClientboundGamePacket; @@ -45,7 +45,7 @@ pub struct DisplayFlags { } impl McBufReadable for DisplayFlags { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let data = u32::read_from(buf)?; Ok(DisplayFlags { background: (data & 0b1) != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs index b8785a66..6bb41a81 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; @@ -34,12 +34,12 @@ enum Operation { } impl McBufReadable for Operation { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { match buf.read_byte()? { 0 => Ok(Operation::Addition), 1 => Ok(Operation::MultiplyBase), 2 => Ok(Operation::MultiplyTotal), - op => Err(format!("Unknown operation: {}", op)), + id => Err(BufReadError::UnexpectedEnumVariant { id: id.into() }), } } } diff --git a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs index 7a96629b..da1485d0 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -1,6 +1,6 @@ use std::io::{Read, Write}; -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_core::{ResourceLocation, Slot}; use packet_macros::ClientboundGamePacket; @@ -49,7 +49,7 @@ impl McBufWritable for ShapedRecipe { } } impl McBufReadable for ShapedRecipe { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let width = buf.read_varint()?.try_into().unwrap(); let height = buf.read_varint()?.try_into().unwrap(); let group = buf.read_utf()?; @@ -129,7 +129,7 @@ impl McBufWritable for Recipe { } impl McBufReadable for Recipe { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let recipe_type = ResourceLocation::read_from(buf)?; let identifier = ResourceLocation::read_from(buf)?; diff --git a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs index 6d567cfa..6f0fe778 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; @@ -23,7 +23,7 @@ pub struct Tags { pub struct TagMap(HashMap<ResourceLocation, Vec<Tags>>); impl McBufReadable for TagMap { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let length = buf.read_varint()? as usize; let mut data = HashMap::with_capacity(length); for _ in 0..length { @@ -51,7 +51,7 @@ impl McBufWritable for TagMap { } } impl McBufReadable for Tags { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let name = ResourceLocation::read_from(buf)?; let elements = buf.read_int_id_list()?; Ok(Tags { name, elements }) |
