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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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