Skip to content

Commit

Permalink
Refactor exceptions (#4)
Browse files Browse the repository at this point in the history
* Rename \GameQ\Exception\Protocol to \GameQ\Exception\ProtocolException
* Rename \GameQ\Exception\Server to \GameQ\Exception\ServerException
* Rename \GameQ\Exception\Query to \GameQ\Exception\QueryException
* Debug mode: Bubble up GameQ exceptions instead of wrapping them into a generic \Exception object
* Add missing @throws PHPDoc annotations
  • Loading branch information
Krymonota authored Sep 27, 2024
1 parent c95636a commit 70f5b38
Show file tree
Hide file tree
Showing 70 changed files with 337 additions and 263 deletions.
28 changes: 13 additions & 15 deletions src/GameQ/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace GameQ;

use GameQ\Exception\Protocol as Exception;
use GameQ\Exception\ProtocolException;

/**
* Class Buffer
Expand Down Expand Up @@ -99,13 +99,13 @@ public function getLength(): int
/**
* Read from the buffer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function read(int $length = 1): string
{

if (($length + $this->index) > $this->length) {
throw new Exception("Unable to read length=$length from buffer. Bad protocol format or return?");
throw new ProtocolException("Unable to read length=$length from buffer. Bad protocol format or return?");
}

$string = substr($this->data, $this->index, $length);
Expand Down Expand Up @@ -173,7 +173,7 @@ public function getPosition(): int
*
* If not found, return everything
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readString(string $delim = "\x00"): string
{
Expand All @@ -198,7 +198,7 @@ public function readString(string $delim = "\x00"): string
* @param int $offset Number of bits to cut off the end
* @param bool $read_offset True if the data after the offset is to be read
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readPascalString(int $offset = 0, bool $read_offset = false): string
{
Expand All @@ -220,7 +220,7 @@ public function readPascalString(int $offset = 0, bool $read_offset = false): st
*
* If not found, return everything
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*
* @todo: Check to see if this is even used anymore
*/
Expand Down Expand Up @@ -251,7 +251,7 @@ public function readStringMulti(array $delims, ?string &$delimfound = null): str
/**
* Read an 8-bit unsigned integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt8(): int
{
Expand All @@ -264,7 +264,7 @@ public function readInt8(): int
/**
* Read and 8-bit signed integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt8Signed(): int
{
Expand All @@ -277,7 +277,7 @@ public function readInt8Signed(): int
/**
* Read a 16-bit unsigned integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt16(): int
{
Expand All @@ -297,7 +297,7 @@ public function readInt16(): int
/**
* Read a 16-bit signed integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt16Signed(): int
{
Expand All @@ -320,7 +320,7 @@ public function readInt16Signed(): int
/**
* Read a 32-bit unsigned integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt32($length = 4): int
{
Expand Down Expand Up @@ -353,7 +353,7 @@ public function readInt32($length = 4): int
/**
* Read a 32-bit signed integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt32Signed(): int
{
Expand All @@ -376,7 +376,7 @@ public function readInt32Signed(): int
/**
* Read a 64-bit unsigned integer
*
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function readInt64(): int
{
Expand Down Expand Up @@ -415,8 +415,6 @@ public function readInt64(): int

/**
* Read a 32-bit float
*
* @throws \GameQ\Exception\Protocol
*/
public function readFloat32(): float
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
namespace GameQ\Exception;

/**
* Exception
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class Query extends \Exception
class ProtocolException extends \Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
namespace GameQ\Exception;

/**
* Exception
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class Server extends \Exception
class QueryException extends \Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
namespace GameQ\Exception;

/**
* Exception
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class Protocol extends \Exception
class ServerException extends \Exception
{
}
26 changes: 11 additions & 15 deletions src/GameQ/GameQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

namespace GameQ;

use GameQ\Exception\Protocol as ProtocolException;
use GameQ\Exception\Query as QueryException;
use GameQ\Exception\ProtocolException;
use GameQ\Exception\QueryException;
use GameQ\Filters\Base;
use GameQ\Query\Core;
use GameQ\Query\Native;
Expand Down Expand Up @@ -87,6 +87,8 @@ public static function factory(): self

/**
* Array of servers being queried
*
* @var Server[] $servers
*/
protected array $servers = [];

Expand Down Expand Up @@ -286,8 +288,6 @@ public function process(): array

// Now we should have some information to process for each server
foreach ($this->servers as $server) {
/* @var $server \GameQ\Server */

// Parse the responses for this server
$result = $this->doParseResponse($server);

Expand Down Expand Up @@ -317,8 +317,6 @@ protected function doChallenges(): void

// Do challenge packets
foreach ($this->servers as $server_id => $server) {
/* @var $server \GameQ\Server */

// This protocol has a challenge packet that needs to be sent
if ($server->protocol()->hasChallenge()) {
// We have a challenge, set the flag
Expand Down Expand Up @@ -347,7 +345,7 @@ protected function doChallenges(): void
} catch (QueryException $exception) {
// Check to see if we are in debug, if so bubble up the exception
if ($this->debug) {
throw new \Exception($exception->getMessage(), $exception->getCode(), $exception);
throw $exception;
}
}

Expand Down Expand Up @@ -377,7 +375,6 @@ protected function doChallenges(): void
$challenge = new Buffer(implode('', $response));

// Grab the server instance
/* @var $server \GameQ\Server */
$server = $this->servers[$server_id];

// Apply the challenge
Expand All @@ -402,7 +399,7 @@ protected function doQueries(): void

// Iterate over the server list
foreach ($this->servers as $server_id => $server) {
/* @var $server \GameQ\Server */
/* @var $server Server */

// Invoke the beforeSend method
$server->protocol()->beforeSend($server);
Expand Down Expand Up @@ -449,7 +446,7 @@ protected function doQueries(): void
} catch (QueryException $exception) {
// Check to see if we are in debug, if so bubble up the exception
if ($this->debug) {
throw new \Exception($exception->getMessage(), $exception->getCode(), $exception);
throw $exception;
}

continue;
Expand All @@ -473,7 +470,6 @@ protected function doQueries(): void
$server_id = $sockets[$socket_id]['server_id'];

// Grab the server instance
/* @var $server \GameQ\Server */
$server = $this->servers[$server_id];

// Save the response from this packet
Expand All @@ -484,7 +480,7 @@ protected function doQueries(): void

// Now we need to close all the sockets
foreach ($sockets as $socketInfo) {
/* @var $socket \GameQ\Query\Core */
/* @var $socket Core */
$socket = $socketInfo['socket'];

// Close the socket
Expand All @@ -499,7 +495,7 @@ protected function doQueries(): void
/**
* Parse the response for a specific server
*
* @throws \Exception
* @throws ProtocolException
*/
protected function doParseResponse(Server $server): array
{
Expand All @@ -517,10 +513,10 @@ protected function doParseResponse(Server $server): array

// Check for online before we do anything else
$results['gq_online'] = (count($results) > 0);
} catch (ProtocolException $e) {
} catch (ProtocolException $exception) {
// Check to see if we are in debug, if so bubble up the exception
if ($this->debug) {
throw new \Exception($e->getMessage(), $e->getCode(), $e);
throw $exception;
}

// We ignore this server
Expand Down
4 changes: 4 additions & 0 deletions src/GameQ/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace GameQ;

use GameQ\Exception\ProtocolException;

/**
* Handles the core functionality for the protocols
*
Expand Down Expand Up @@ -413,6 +415,8 @@ public function beforeSend(Server $server): void

/**
* Method called to process query response data. Each extending class has to have one of these functions.
*
* @throws ProtocolException
*/
abstract public function processResponse(): mixed;
}
3 changes: 2 additions & 1 deletion src/GameQ/Protocols/Arma3.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace GameQ\Protocols;

use GameQ\Buffer;
use GameQ\Exception\ProtocolException;
use GameQ\Result;

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ class Arma3 extends Source
* Process the rules since Arma3 changed their response for rules
*
* @return array
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
protected function processRules(Buffer $buffer)
{
Expand Down
11 changes: 8 additions & 3 deletions src/GameQ/Protocols/Ase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace GameQ\Protocols;

use GameQ\Exception\ProtocolException;
use GameQ\Protocol;
use GameQ\Buffer;
use GameQ\Result;
Expand Down Expand Up @@ -89,7 +90,7 @@ class Ase extends Protocol
* Process the response
*
* @return mixed
* @throws \GameQ\Exception\Protocol
* @throws ProtocolException
*/
public function processResponse(): mixed
{
Expand All @@ -98,15 +99,15 @@ public function processResponse(): mixed

// Check for valid response
if ($buffer->getLength() < 4) {
throw new \GameQ\Exception\Protocol(sprintf('%s The response from the server was empty.', __METHOD__));
throw new ProtocolException(sprintf('%s The response from the server was empty.', __METHOD__));
}

// Read the header
$header = $buffer->read(4);

// Verify header
if ($header !== 'EYE1') {
throw new \GameQ\Exception\Protocol(sprintf('%s The response header "%s" does not match expected "EYE1"', __METHOD__, $header));
throw new ProtocolException(sprintf('%s The response header "%s" does not match expected "EYE1"', __METHOD__, $header));
}

// Create a new result
Expand Down Expand Up @@ -141,6 +142,8 @@ public function processResponse(): mixed

/**
* Handles processing the extra key/value pairs for server settings
*
* @throws ProtocolException
*/
protected function processKeyValuePairs(Buffer $buffer, Result $result)
{
Expand All @@ -165,6 +168,8 @@ protected function processKeyValuePairs(Buffer $buffer, Result $result)

/**
* Handles processing the player and team data into a usable format
*
* @throws ProtocolException
*/
protected function processPlayersAndTeams(Buffer $buffer, Result $result)
{
Expand Down
Loading

0 comments on commit 70f5b38

Please sign in to comment.