Use more appropriate exceptions in the protocol layer

This commit is contained in:
Dylan K. Taylor 2019-01-03 17:57:06 +00:00
parent 504cc3bf8b
commit 0f941410f6
7 changed files with 21 additions and 8 deletions

View File

@ -111,7 +111,7 @@ class BatchPacket extends DataPacket{
$pk = PacketPool::getPacket($buf); $pk = PacketPool::getPacket($buf);
if(!$pk->canBeBatched()){ if(!$pk->canBeBatched()){
throw new \InvalidArgumentException("Received invalid " . get_class($pk) . " inside BatchPacket"); throw new \UnexpectedValueException("Received invalid " . get_class($pk) . " inside BatchPacket");
} }
$session->handleDataPacket($pk); $session->handleDataPacket($pk);

View File

@ -109,7 +109,7 @@ class BookEditPacket extends DataPacket{
$this->putString($this->xuid); $this->putString($this->xuid);
break; break;
default: default:
throw new \UnexpectedValueException("Unknown book edit type $this->type!"); throw new \InvalidArgumentException("Unknown book edit type $this->type!");
} }
} }

View File

@ -91,7 +91,7 @@ class ClientboundMapItemDataPacket extends DataPacket{
}elseif($object->type === MapTrackedObject::TYPE_ENTITY){ }elseif($object->type === MapTrackedObject::TYPE_ENTITY){
$object->entityUniqueId = $this->getEntityUniqueId(); $object->entityUniqueId = $this->getEntityUniqueId();
}else{ }else{
throw new \UnexpectedValueException("Unknown map object type"); throw new \UnexpectedValueException("Unknown map object type $object->type");
} }
$this->trackedEntities[] = $object; $this->trackedEntities[] = $object;
} }
@ -163,7 +163,7 @@ class ClientboundMapItemDataPacket extends DataPacket{
}elseif($object->type === MapTrackedObject::TYPE_ENTITY){ }elseif($object->type === MapTrackedObject::TYPE_ENTITY){
$this->putEntityUniqueId($object->entityUniqueId); $this->putEntityUniqueId($object->entityUniqueId);
}else{ }else{
throw new \UnexpectedValueException("Unknown map object type"); throw new \InvalidArgumentException("Unknown map object type $object->type");
} }
} }

View File

@ -68,12 +68,20 @@ abstract class DataPacket extends NetworkBinaryStream{
return false; return false;
} }
/**
* @throws \OutOfBoundsException
* @throws \UnexpectedValueException
*/
public function decode(){ public function decode(){
$this->offset = 0; $this->offset = 0;
$this->decodeHeader(); $this->decodeHeader();
$this->decodePayload(); $this->decodePayload();
} }
/**
* @throws \OutOfBoundsException
* @throws \UnexpectedValueException
*/
protected function decodeHeader(){ protected function decodeHeader(){
$pid = $this->getUnsignedVarInt(); $pid = $this->getUnsignedVarInt();
if($pid !== static::NETWORK_ID){ if($pid !== static::NETWORK_ID){
@ -83,6 +91,9 @@ abstract class DataPacket extends NetworkBinaryStream{
/** /**
* Note for plugin developers: If you're adding your own packets, you should perform decoding in here. * Note for plugin developers: If you're adding your own packets, you should perform decoding in here.
*
* @throws \OutOfBoundsException
* @throws \UnexpectedValueException
*/ */
protected function decodePayload(){ protected function decodePayload(){

View File

@ -147,7 +147,7 @@ class InventoryTransactionPacket extends DataPacket{
$this->putVector3($this->trData->headPos); $this->putVector3($this->trData->headPos);
break; break;
default: default:
throw new \UnexpectedValueException("Unknown transaction type $this->transactionType"); throw new \InvalidArgumentException("Unknown transaction type $this->transactionType");
} }
} }

View File

@ -81,7 +81,7 @@ class SetScorePacket extends DataPacket{
$this->putString($entry->customName); $this->putString($entry->customName);
break; break;
default: default:
throw new \UnexpectedValueException("Unknown entry type $entry->type"); throw new \InvalidArgumentException("Unknown entry type $entry->type");
} }
} }
} }

View File

@ -151,7 +151,7 @@ class NetworkInventoryAction{
$packet->putVarInt($this->windowId); $packet->putVarInt($this->windowId);
break; break;
default: default:
throw new \UnexpectedValueException("Unknown inventory action source type $this->sourceType"); throw new \InvalidArgumentException("Unknown inventory action source type $this->sourceType");
} }
$packet->putUnsignedVarInt($this->inventorySlot); $packet->putUnsignedVarInt($this->inventorySlot);
@ -163,6 +163,8 @@ class NetworkInventoryAction{
* @param Player $player * @param Player $player
* *
* @return InventoryAction|null * @return InventoryAction|null
*
* @throws \UnexpectedValueException
*/ */
public function createInventoryAction(Player $player){ public function createInventoryAction(Player $player){
switch($this->sourceType){ switch($this->sourceType){
@ -172,7 +174,7 @@ class NetworkInventoryAction{
return new SlotChangeAction($window, $this->inventorySlot, $this->oldItem, $this->newItem); return new SlotChangeAction($window, $this->inventorySlot, $this->oldItem, $this->newItem);
} }
throw new \InvalidStateException("Player " . $player->getName() . " has no open container with window ID $this->windowId"); throw new \UnexpectedValueException("Player " . $player->getName() . " has no open container with window ID $this->windowId");
case self::SOURCE_WORLD: case self::SOURCE_WORLD:
if($this->inventorySlot !== self::ACTION_MAGIC_SLOT_DROP_ITEM){ if($this->inventorySlot !== self::ACTION_MAGIC_SLOT_DROP_ITEM){
throw new \UnexpectedValueException("Only expecting drop-item world actions from the client!"); throw new \UnexpectedValueException("Only expecting drop-item world actions from the client!");