Get rid of ThisShouldNotHappenException

This commit is contained in:
ErickSkrauch 2019-12-13 20:48:13 +03:00
parent 26f7d6213f
commit 830a17612b
18 changed files with 36 additions and 101 deletions

View File

@ -1,6 +0,0 @@
<?php
namespace api\exceptions;
class Exception extends \Exception {
}

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace api\exceptions;
/**
* The exception can be used for cases where the outcome doesn't seem to be expected,
* but can theoretically happen. The goal is to capture these areas and refine the logic
* if such situations do occur.
*
* @deprecated use \Webmozart\Assert\Assert to ensure, that action has been successfully performed
*/
class ThisShouldNotHappenException extends Exception {
}

View File

@ -3,7 +3,6 @@ namespace api\models\authentication;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\components\ReCaptcha\Validator as ReCaptchaValidator; use api\components\ReCaptcha\Validator as ReCaptchaValidator;
use api\exceptions\ThisShouldNotHappenException;
use api\models\base\ApiForm; use api\models\base\ApiForm;
use common\components\UserFriendlyRandomKey; use common\components\UserFriendlyRandomKey;
use common\helpers\Error as E; use common\helpers\Error as E;
@ -11,6 +10,7 @@ use common\models\Account;
use common\models\confirmations\RegistrationConfirmation; use common\models\confirmations\RegistrationConfirmation;
use common\models\EmailActivation; use common\models\EmailActivation;
use common\tasks\SendRegistrationEmail; use common\tasks\SendRegistrationEmail;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class RepeatAccountActivationForm extends ApiForm { class RepeatAccountActivationForm extends ApiForm {
@ -74,9 +74,7 @@ class RepeatAccountActivationForm extends ApiForm {
$activation = new RegistrationConfirmation(); $activation = new RegistrationConfirmation();
$activation->account_id = $account->id; $activation->account_id = $account->id;
$activation->key = UserFriendlyRandomKey::make(); $activation->key = UserFriendlyRandomKey::make();
if (!$activation->save()) { Assert::true($activation->save(), 'Unable save email-activation model.');
throw new ThisShouldNotHappenException('Unable save email-activation model.');
}
$this->emailActivation = $activation; $this->emailActivation = $activation;

View File

@ -1,10 +1,10 @@
<?php <?php
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\exceptions\ThisShouldNotHappenException;
use api\modules\internal\helpers\Error as E; use api\modules\internal\helpers\Error as E;
use common\models\Account; use common\models\Account;
use common\tasks\ClearAccountSessions; use common\tasks\ClearAccountSessions;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class BanAccountForm extends AccountActionForm { class BanAccountForm extends AccountActionForm {
@ -50,9 +50,7 @@ class BanAccountForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$account->status = Account::STATUS_BANNED; $account->status = Account::STATUS_BANNED;
if (!$account->save()) { Assert::true($account->save(), 'Cannot ban account');
throw new ThisShouldNotHappenException('Cannot ban account');
}
Yii::$app->queue->push(ClearAccountSessions::createFromAccount($account)); Yii::$app->queue->push(ClearAccountSessions::createFromAccount($account));

View File

@ -2,9 +2,9 @@
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\EmailActivationKeyValidator; use api\validators\EmailActivationKeyValidator;
use common\models\EmailActivation; use common\models\EmailActivation;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class ChangeEmailForm extends AccountActionForm { class ChangeEmailForm extends AccountActionForm {
@ -33,9 +33,7 @@ class ChangeEmailForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$account->email = $activation->newEmail; $account->email = $activation->newEmail;
if (!$account->save()) { Assert::true($account->save(), 'Cannot save new account email value');
throw new ThisShouldNotHappenException('Cannot save new account email value');
}
$transaction->commit(); $transaction->commit();

View File

@ -2,8 +2,8 @@
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\exceptions\ThisShouldNotHappenException;
use common\validators\LanguageValidator; use common\validators\LanguageValidator;
use Webmozart\Assert\Assert;
class ChangeLanguageForm extends AccountActionForm { class ChangeLanguageForm extends AccountActionForm {
@ -26,9 +26,7 @@ class ChangeLanguageForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$account->lang = $this->lang; $account->lang = $this->lang;
if (!$account->save()) { Assert::true($account->save(), 'Cannot change user language');
throw new ThisShouldNotHappenException('Cannot change user language');
}
return true; return true;
} }

View File

@ -3,10 +3,10 @@ namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\components\User\Component; use api\components\User\Component;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\PasswordRequiredValidator; use api\validators\PasswordRequiredValidator;
use common\helpers\Error as E; use common\helpers\Error as E;
use common\validators\PasswordValidator; use common\validators\PasswordValidator;
use Webmozart\Assert\Assert;
use Yii; use Yii;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
@ -61,9 +61,7 @@ class ChangePasswordForm extends AccountActionForm {
Yii::$app->user->terminateSessions($account, Component::KEEP_CURRENT_SESSION); Yii::$app->user->terminateSessions($account, Component::KEEP_CURRENT_SESSION);
} }
if (!$account->save()) { Assert::true($account->save(), 'Cannot save user model');
throw new ThisShouldNotHappenException('Cannot save user model');
}
$transaction->commit(); $transaction->commit();

View File

@ -2,11 +2,11 @@
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\PasswordRequiredValidator; use api\validators\PasswordRequiredValidator;
use common\models\UsernameHistory; use common\models\UsernameHistory;
use common\tasks\PullMojangUsername; use common\tasks\PullMojangUsername;
use common\validators\UsernameValidator; use common\validators\UsernameValidator;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class ChangeUsernameForm extends AccountActionForm { class ChangeUsernameForm extends AccountActionForm {
@ -40,16 +40,12 @@ class ChangeUsernameForm extends AccountActionForm {
$transaction = Yii::$app->db->beginTransaction(); $transaction = Yii::$app->db->beginTransaction();
$account->username = $this->username; $account->username = $this->username;
if (!$account->save()) { Assert::true($account->save(), 'Cannot save account model with new username');
throw new ThisShouldNotHappenException('Cannot save account model with new username');
}
$usernamesHistory = new UsernameHistory(); $usernamesHistory = new UsernameHistory();
$usernamesHistory->account_id = $account->id; $usernamesHistory->account_id = $account->id;
$usernamesHistory->username = $account->username; $usernamesHistory->username = $account->username;
if (!$usernamesHistory->save()) { Assert::true($usernamesHistory->save(), 'Cannot save username history record');
throw new ThisShouldNotHappenException('Cannot save username history record');
}
Yii::$app->queue->push(PullMojangUsername::createFromAccount($account)); Yii::$app->queue->push(PullMojangUsername::createFromAccount($account));

View File

@ -2,10 +2,10 @@
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\PasswordRequiredValidator; use api\validators\PasswordRequiredValidator;
use api\validators\TotpValidator; use api\validators\TotpValidator;
use common\helpers\Error as E; use common\helpers\Error as E;
use Webmozart\Assert\Assert;
class DisableTwoFactorAuthForm extends AccountActionForm { class DisableTwoFactorAuthForm extends AccountActionForm {
@ -33,9 +33,7 @@ class DisableTwoFactorAuthForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$account->is_otp_enabled = false; $account->is_otp_enabled = false;
$account->otp_secret = null; $account->otp_secret = null;
if (!$account->save()) { Assert::true($account->save(), 'Cannot disable otp for account');
throw new ThisShouldNotHappenException('Cannot disable otp for account');
}
return true; return true;
} }

View File

@ -3,10 +3,10 @@ namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\components\User\Component; use api\components\User\Component;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\PasswordRequiredValidator; use api\validators\PasswordRequiredValidator;
use api\validators\TotpValidator; use api\validators\TotpValidator;
use common\helpers\Error as E; use common\helpers\Error as E;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class EnableTwoFactorAuthForm extends AccountActionForm { class EnableTwoFactorAuthForm extends AccountActionForm {
@ -36,9 +36,7 @@ class EnableTwoFactorAuthForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$account->is_otp_enabled = true; $account->is_otp_enabled = true;
if (!$account->save()) { Assert::true($account->save(), 'Cannot enable otp for account');
throw new ThisShouldNotHappenException('Cannot enable otp for account');
}
Yii::$app->user->terminateSessions($account, Component::KEEP_CURRENT_SESSION); Yii::$app->user->terminateSessions($account, Component::KEEP_CURRENT_SESSION);

View File

@ -1,9 +1,9 @@
<?php <?php
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\exceptions\ThisShouldNotHappenException;
use api\modules\internal\helpers\Error as E; use api\modules\internal\helpers\Error as E;
use common\models\Account; use common\models\Account;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class PardonAccountForm extends AccountActionForm { class PardonAccountForm extends AccountActionForm {
@ -29,9 +29,7 @@ class PardonAccountForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$account->status = Account::STATUS_ACTIVE; $account->status = Account::STATUS_ACTIVE;
if (!$account->save()) { Assert::true($account->save(), 'Cannot pardon account');
throw new ThisShouldNotHappenException('Cannot pardon account');
}
$transaction->commit(); $transaction->commit();

View File

@ -2,12 +2,12 @@
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\PasswordRequiredValidator; use api\validators\PasswordRequiredValidator;
use common\helpers\Error as E; use common\helpers\Error as E;
use common\models\confirmations\CurrentEmailConfirmation; use common\models\confirmations\CurrentEmailConfirmation;
use common\models\EmailActivation; use common\models\EmailActivation;
use common\tasks\SendCurrentEmailConfirmation; use common\tasks\SendCurrentEmailConfirmation;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class SendEmailVerificationForm extends AccountActionForm { class SendEmailVerificationForm extends AccountActionForm {
@ -59,9 +59,7 @@ class SendEmailVerificationForm extends AccountActionForm {
$account = $this->getAccount(); $account = $this->getAccount();
$emailActivation = new CurrentEmailConfirmation(); $emailActivation = new CurrentEmailConfirmation();
$emailActivation->account_id = $account->id; $emailActivation->account_id = $account->id;
if (!$emailActivation->save()) { Assert::true($emailActivation->save(), 'Cannot save email activation model');
throw new ThisShouldNotHappenException('Cannot save email activation model');
}
return $emailActivation; return $emailActivation;
} }

View File

@ -2,12 +2,12 @@
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\aop\annotations\CollectModelMetrics; use api\aop\annotations\CollectModelMetrics;
use api\exceptions\ThisShouldNotHappenException;
use api\validators\EmailActivationKeyValidator; use api\validators\EmailActivationKeyValidator;
use common\models\confirmations\NewEmailConfirmation; use common\models\confirmations\NewEmailConfirmation;
use common\models\EmailActivation; use common\models\EmailActivation;
use common\tasks\SendNewEmailConfirmation; use common\tasks\SendNewEmailConfirmation;
use common\validators\EmailValidator; use common\validators\EmailValidator;
use Webmozart\Assert\Assert;
use Yii; use Yii;
class SendNewEmailVerificationForm extends AccountActionForm { class SendNewEmailVerificationForm extends AccountActionForm {
@ -50,9 +50,7 @@ class SendNewEmailVerificationForm extends AccountActionForm {
$emailActivation = new NewEmailConfirmation(); $emailActivation = new NewEmailConfirmation();
$emailActivation->account_id = $this->getAccount()->id; $emailActivation->account_id = $this->getAccount()->id;
$emailActivation->newEmail = $this->email; $emailActivation->newEmail = $this->email;
if (!$emailActivation->save()) { Assert::true($emailActivation->save(), 'Cannot save email activation model');
throw new ThisShouldNotHappenException('Cannot save email activation model');
}
return $emailActivation; return $emailActivation;
} }

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace api\modules\accounts\models; namespace api\modules\accounts\models;
use api\exceptions\ThisShouldNotHappenException;
use api\models\base\BaseAccountForm; use api\models\base\BaseAccountForm;
use BaconQrCode\Common\ErrorCorrectionLevel; use BaconQrCode\Common\ErrorCorrectionLevel;
use BaconQrCode\Encoder\Encoder; use BaconQrCode\Encoder\Encoder;
@ -14,6 +13,7 @@ use common\components\Qr\ElyDecorator;
use OTPHP\TOTP; use OTPHP\TOTP;
use OTPHP\TOTPInterface; use OTPHP\TOTPInterface;
use ParagonIE\ConstantTime\Base32; use ParagonIE\ConstantTime\Base32;
use Webmozart\Assert\Assert;
class TwoFactorAuthInfo extends BaseAccountForm { class TwoFactorAuthInfo extends BaseAccountForm {
@ -61,16 +61,10 @@ class TwoFactorAuthInfo extends BaseAccountForm {
return 'data:image/svg+xml,' . $svg; return 'data:image/svg+xml,' . $svg;
} }
/**
* @param int $length
* @throws ThisShouldNotHappenException
*/
private function setOtpSecret(int $length = 24): void { private function setOtpSecret(int $length = 24): void {
$account = $this->getAccount(); $account = $this->getAccount();
$account->otp_secret = $this->generateOtpSecret($length); $account->otp_secret = $this->generateOtpSecret($length);
if (!$account->save()) { Assert::true($account->save(), 'Cannot set account otp_secret');
throw new ThisShouldNotHappenException('Cannot set account otp_secret');
}
} }
/** /**

View File

@ -2,7 +2,6 @@
namespace api\modules\oauth\controllers; namespace api\modules\oauth\controllers;
use api\controllers\Controller; use api\controllers\Controller;
use api\exceptions\ThisShouldNotHappenException;
use api\modules\oauth\exceptions\UnsupportedOauthClientType; use api\modules\oauth\exceptions\UnsupportedOauthClientType;
use api\modules\oauth\models\OauthClientForm; use api\modules\oauth\models\OauthClientForm;
use api\modules\oauth\models\OauthClientFormFactory; use api\modules\oauth\models\OauthClientFormFactory;
@ -10,6 +9,7 @@ use api\modules\oauth\models\OauthClientTypeForm;
use api\rbac\Permissions as P; use api\rbac\Permissions as P;
use common\models\Account; use common\models\Account;
use common\models\OauthClient; use common\models\OauthClient;
use Webmozart\Assert\Assert;
use Yii; use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
@ -68,9 +68,7 @@ class ClientsController extends Controller {
public function actionCreate(string $type): array { public function actionCreate(string $type): array {
$account = Yii::$app->user->identity->getAccount(); $account = Yii::$app->user->identity->getAccount();
if ($account === null) { Assert::notNull($account === null, 'This form should not to be executed without associated account');
throw new ThisShouldNotHappenException('This form should not to be executed without associated account');
}
$client = new OauthClient(); $client = new OauthClient();
$client->account_id = $account->id; $client->account_id = $account->id;

View File

@ -3,10 +3,10 @@ declare(strict_types=1);
namespace api\modules\oauth\models; namespace api\modules\oauth\models;
use api\exceptions\ThisShouldNotHappenException;
use api\modules\oauth\exceptions\InvalidOauthClientState; use api\modules\oauth\exceptions\InvalidOauthClientState;
use common\models\OauthClient; use common\models\OauthClient;
use common\tasks\ClearOauthSessions; use common\tasks\ClearOauthSessions;
use Webmozart\Assert\Assert;
use Yii; use Yii;
use yii\helpers\Inflector; use yii\helpers\Inflector;
@ -48,9 +48,7 @@ class OauthClientForm {
$client->generateSecret(); $client->generateSecret();
} }
if (!$client->save()) { Assert::true($client->save(), 'Cannot save oauth client');
throw new ThisShouldNotHappenException('Cannot save oauth client');
}
return true; return true;
} }
@ -60,9 +58,7 @@ class OauthClientForm {
$client = $this->client; $client = $this->client;
$client->is_deleted = true; $client->is_deleted = true;
if (!$client->save()) { Assert::true($client->save(), 'Cannot update oauth client');
throw new ThisShouldNotHappenException('Cannot update oauth client');
}
Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client)); Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client));
@ -77,9 +73,7 @@ class OauthClientForm {
$client = $this->client; $client = $this->client;
if ($regenerateSecret) { if ($regenerateSecret) {
$client->generateSecret(); $client->generateSecret();
if (!$client->save()) { Assert::true($client->save(), 'Cannot update oauth client');
throw new ThisShouldNotHappenException('Cannot update oauth client');
}
} }
Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client, time())); Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client, time()));

View File

@ -3,13 +3,13 @@ declare(strict_types=1);
namespace common\tasks; namespace common\tasks;
use api\exceptions\ThisShouldNotHappenException;
use common\models\Account; use common\models\Account;
use common\models\MojangUsername; use common\models\MojangUsername;
use Ely\Mojang\Api as MojangApi; use Ely\Mojang\Api as MojangApi;
use Ely\Mojang\Exception\MojangApiException; use Ely\Mojang\Exception\MojangApiException;
use Ely\Mojang\Exception\NoContentException; use Ely\Mojang\Exception\NoContentException;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use Webmozart\Assert\Assert;
use Yii; use Yii;
use yii\queue\JobInterface; use yii\queue\JobInterface;
@ -60,9 +60,7 @@ class PullMojangUsername implements JobInterface {
$mojangUsername->touch('last_pulled_at'); $mojangUsername->touch('last_pulled_at');
} }
if (!$mojangUsername->save()) { Assert::true($mojangUsername->save(), 'Cannot save mojang username');
throw new ThisShouldNotHappenException('Cannot save mojang username');
}
} }
} }

View File

@ -3,9 +3,9 @@ declare(strict_types=1);
namespace console\models; namespace console\models;
use api\exceptions\ThisShouldNotHappenException;
use common\models\WebHook; use common\models\WebHook;
use common\models\WebHookEvent; use common\models\WebHookEvent;
use Webmozart\Assert\Assert;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
@ -43,17 +43,13 @@ class WebHookForm extends Model {
$webHook = $this->webHook; $webHook = $this->webHook;
$webHook->url = $this->url; $webHook->url = $this->url;
$webHook->secret = $this->secret; $webHook->secret = $this->secret;
if (!$webHook->save()) { Assert::true($webHook->save(), 'Cannot save webhook.');
throw new ThisShouldNotHappenException('Cannot save webhook.');
}
foreach ($this->events as $event) { foreach ($this->events as $event) {
$eventModel = new WebHookEvent(); $eventModel = new WebHookEvent();
$eventModel->webhook_id = $webHook->id; $eventModel->webhook_id = $webHook->id;
$eventModel->event_type = $event; $eventModel->event_type = $event;
if (!$eventModel->save()) { Assert::true($eventModel->save(), 'Cannot save webhook event.');
throw new ThisShouldNotHappenException('Cannot save webhook event.');
}
} }
$transaction->commit(); $transaction->commit();