Skip to content

Commit

Permalink
Moved exception base class, use better exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pavarnos committed Aug 14, 2018
1 parent 40d5278 commit b97fca8
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Threema/Console/Symfony/AbstractNetworkedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\Core\Exception;
use Threema\MsgApi\Commands\Results\Result;
use Threema\MsgApi\Connection;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Exceptions\Exception;
use Threema\MsgApi\Exceptions\InvalidArgumentException;

abstract class AbstractNetworkedCommand extends AbstractLocalCommand
Expand Down
2 changes: 2 additions & 0 deletions src/Threema/Core/AssocArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Threema\Core;

use Threema\MsgApi\Exceptions\Exception;

class AssocArray
{
/**
Expand Down
8 changes: 4 additions & 4 deletions src/Threema/MsgApi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function fetchPublicKey(string $threemaId): FetchPublicKeyResult
* @param string $toPublicKeyHex
* @param string $text
* @return \Threema\MsgApi\Commands\Results\SendE2EResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public function sendTextMessage(string $myPrivateKeyHex, string $toThreemaId, string $toPublicKeyHex,
string $text): SendE2EResult
Expand All @@ -189,7 +189,7 @@ public function sendTextMessage(string $myPrivateKeyHex, string $toThreemaId, st
* @param string $toPublicKeyHex
* @param string $imagePath
* @return \Threema\MsgApi\Commands\Results\SendE2EResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public function sendImageMessage(string $myPrivateKeyHex, string $toThreemaId, string $toPublicKeyHex,
string $imagePath): SendE2EResult
Expand All @@ -207,7 +207,7 @@ public function sendImageMessage(string $myPrivateKeyHex, string $toThreemaId, s
* @param string $filePath
* @param string $thumbnailPath
* @return \Threema\MsgApi\Commands\Results\SendE2EResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public final function sendFileMessage(string $myPrivateKeyHex, string $toThreemaId, string $toPublicKeyHex,
string $filePath, string $thumbnailPath = '')
Expand Down Expand Up @@ -252,7 +252,7 @@ public final function macIsValid(string $threemaId, string $gatewayId, string $m
* null=current folder, false=do not download files
* @param \Closure $shouldDownload
* @return \Threema\MsgApi\Helpers\ReceiveMessageResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
* @throws \Threema\MsgApi\Exceptions\BadMessageException
* @throws \Threema\MsgApi\Exceptions\DecryptionFailedException
* @throws \Threema\MsgApi\Exceptions\HttpException
Expand Down
20 changes: 5 additions & 15 deletions src/Threema/MsgApi/Encryptor/AbstractEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Threema\MsgApi\Encryptor;

use Threema\Core\AssocArray;
use Threema\Core\Exception;
use Threema\Core\KeyPair;
use Threema\MsgApi\Commands\Results\UploadFileResult;
use Threema\MsgApi\Exceptions\BadMessageException;
use Threema\MsgApi\Exceptions\DecryptionFailedException;
use Threema\MsgApi\Exceptions\Exception;
use Threema\MsgApi\Exceptions\UnsupportedMessageTypeException;
use Threema\MsgApi\Helpers\EncryptResult;
use Threema\MsgApi\Helpers\FileAnalysisResult;
Expand Down Expand Up @@ -76,7 +76,7 @@ final public function encryptMessageText($text, $senderPrivateKey, $recipientPub
* @param string $recipientPublicKey the public key of the receiving ID (as binary)
* @param string $nonce the nonce to be used for the encryption (usually 24 random bytes)
* @return string
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
final public function encryptImageMessage(
UploadFileResult $uploadFileResult,
Expand Down Expand Up @@ -140,7 +140,7 @@ final public function encryptFileMessage(UploadFileResult $uploadFileResult,
* @throws BadMessageException
* @throws DecryptionFailedException
* @throws UnsupportedMessageTypeException
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKey, $nonce)
{
Expand Down Expand Up @@ -282,14 +282,6 @@ abstract public function derivePublicKey($privateKey);
*/
abstract public function isSupported();

/**
* Validate crypt tool
*
* @return bool
* @throws Exception
*/
abstract public function validate();

/**
* @param string $data
* @return EncryptResult
Expand Down Expand Up @@ -391,14 +383,12 @@ public function bin2hex($binaryString)
*
* @param string $hexString The hex string to convert
* @param string|null $ignore (optional) Characters to ignore
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
* @return string
*/
public function hex2bin($hexString, $ignore = null)
{
if ($ignore !== null) {
throw new Exception('$ignore parameter is not supported');
}
assert($ignore == null, '$ignore parameter is not supported');
return hex2bin($hexString) ?: '';
}

Expand Down
16 changes: 1 addition & 15 deletions src/Threema/MsgApi/Encryptor/SodiumEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace Threema\MsgApi\Encryptor;

use Threema\Core\Exception;
use Threema\Core\KeyPair;
use Threema\MsgApi\Exceptions\Exception;

/**
* Contains static methods to do various Threema cryptography related tasks.
Expand Down Expand Up @@ -53,20 +53,6 @@ public function isSupported()
return function_exists('sodium_crypto_box');
}

/**
* Validate crypt tool
*
* @return bool
* @throws Exception
*/
public function validate()
{
if (false === $this->isSupported()) {
throw new Exception('Sodium implementation not supported');
}
return true;
}

/**
* @return string
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Threema/MsgApi/Exceptions/BadMessageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Threema\MsgApi\Exceptions;

use Threema\Core\Exception;

class BadMessageException extends Exception
{
}
2 changes: 0 additions & 2 deletions src/Threema/MsgApi/Exceptions/DecryptionFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Threema\MsgApi\Exceptions;

use Threema\Core\Exception;

class DecryptionFailedException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @copyright Copyright (c) 2015-2016 Threema GmbH
*/

namespace Threema\Core;
namespace Threema\MsgApi\Exceptions;

class Exception extends \Exception
{
Expand Down
2 changes: 0 additions & 2 deletions src/Threema/MsgApi/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Threema\MsgApi\Exceptions;

use Threema\Core\Exception;

class HttpException extends Exception
{
}
2 changes: 0 additions & 2 deletions src/Threema/MsgApi/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Threema\MsgApi\Exceptions;

use Threema\Core\Exception;

class InvalidArgumentException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Threema\MsgApi\Exceptions;

use Threema\Core\Exception;

class UnsupportedMessageTypeException extends Exception
{
}
13 changes: 7 additions & 6 deletions src/Threema/MsgApi/Helpers/E2EHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

namespace Threema\MsgApi\Helpers;

use Threema\Core\Exception;
use Threema\MsgApi\Commands\Results\CapabilityResult;
use Threema\MsgApi\Connection;
use Threema\MsgApi\Encryptor\AbstractEncryptor;
use Threema\MsgApi\Exceptions\DecryptionFailedException;
use Threema\MsgApi\Exceptions\Exception;
use Threema\MsgApi\Exceptions\HttpException;
use Threema\MsgApi\Exceptions\InvalidArgumentException;
use Threema\MsgApi\Exceptions\UnsupportedMessageTypeException;
use Threema\MsgApi\Messages\FileMessage;
use Threema\MsgApi\Messages\ImageMessage;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function __construct(string $privateKeyBinary, Connection $connection, Ab
* @param string $receiverPublicKey binary format
* @param string $text
* @return \Threema\MsgApi\Commands\Results\SendE2EResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public final function sendTextMessage(string $threemaId, string $receiverPublicKey, string $text)
{
Expand All @@ -80,7 +81,7 @@ public final function sendTextMessage(string $threemaId, string $receiverPublicK
* @param string $receiverPublicKey binary format
* @param string $imagePath
* @return \Threema\MsgApi\Commands\Results\SendE2EResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public final function sendImageMessage(string $threemaId, string $receiverPublicKey, string $imagePath)
{
Expand All @@ -90,7 +91,7 @@ public final function sendImageMessage(string $threemaId, string $receiverPublic
'image/jpg',
'image/jpeg',
'image/png'])) {
throw new Exception('file is not a jpg or png');
throw new InvalidArgumentException('file is not a jpg or png');
}

$this->assertIsCapable($threemaId, CapabilityResult::IMAGE);
Expand Down Expand Up @@ -126,7 +127,7 @@ public final function sendImageMessage(string $threemaId, string $receiverPublic
* @param string $filePath
* @param string $thumbnailPath
* @return \Threema\MsgApi\Commands\Results\SendE2EResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public final function sendFileMessage(string $threemaId, string $receiverPublicKey, string $filePath,
string $thumbnailPath = '')
Expand Down Expand Up @@ -186,7 +187,7 @@ public final function sendFileMessage(string $threemaId, string $receiverPublicK
* null=current folder, false=do not download files
* @param \Closure $shouldDownload
* @return ReceiveMessageResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\Exception
* @throws \Threema\MsgApi\Exceptions\BadMessageException
* @throws \Threema\MsgApi\Exceptions\DecryptionFailedException
* @throws \Threema\MsgApi\Exceptions\HttpException
Expand Down
8 changes: 4 additions & 4 deletions src/Threema/MsgApi/Helpers/FileAnalysisTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

namespace Threema\MsgApi\Helpers;

use Threema\Core\Exception;
use Threema\MsgApi\Exceptions\InvalidArgumentException;

final class FileAnalysisTool
{
/**
* @param string $file
* @return FileAnalysisResult
* @throws \Threema\Core\Exception
* @throws \Threema\MsgApi\Exceptions\InvalidArgumentException
*/
public static function analyseOrDie(string $file): FileAnalysisResult
{
if (false === file_exists($file)) {
throw new Exception('No such file ' . $file);
throw new InvalidArgumentException('No such file ' . $file);
}

if (false === is_file($file)) {
throw new Exception('Not a file: ' . $file);
throw new InvalidArgumentException('Not a file: ' . $file);
}

$mimeType = '';
Expand Down

0 comments on commit b97fca8

Please sign in to comment.