Symfony - 基于结果的异常消息

Symfony - Exception message based on result

在我的 Symfony 项目中,我执行了简单的密码验证检查。

我想做的是根据该方法中回显的消息捕获异常。

我不知道如何实现它。

这是我的通过检查方法:

public function validatePasswordStrength($password, $username): void
{
    $error = null;

    if ($password === '') {
        echo "Password not entered!";
    } elseif ($password === $username) {
        echo "Password and username can not be same!";
    } elseif (strlen($password) < 8) {
        echo "Password must be at least 8 characters long!";
    } elseif (!preg_match("#[a-z]+#", $password)) {
        echo "Password must include at least one lowercase letter!";
    } elseif (!preg_match("#[A-Z]+#", $password)) {
        echo "Password must include at least one uppercase letter!";
    } elseif (!preg_match("#\W+#", $password)) {
        echo "Password must include at least one symbol!";
    }

    if ($error) {
        throw new \Exception('Here I want to put echo message.');
    }
}

实际上,想法是定义任何此消息,如果语句为真,则异常消息应该是来自该语句的消息。这就是想法。

这是我在 Controller 中的端点方法:

/**
 * @Route("/check-password", name="check_password")
 */
public function validatePassword(Request $request, $password = 'S0235ds-')
{
    $username = 'Apple';

    try {
        $this->methodService->validatePasswordStrength($password, $username);

        return $this->json(["message" => "SUCCESS"]);
    } catch (Exception $e) {
        return $this->json(["message" => $e->getMessage()], Response::HTTP_BAD_REQUEST);
    }
}

类似的东西?

public function validatePasswordStrength($password, $username): void
{
    $error = null;

    if ($password === '') {
        $error = "Password not entered!";
    } elseif ($password === $username) {
        $error = "Password and username can not be same!";
    } elseif (strlen($password) < 8) {
        $error = "Password must be at least 8 characters long!";
    } elseif (!preg_match("#[a-z]+#", $password)) {
        $error = "Password must include at least one lowercase letter!";
    } elseif (!preg_match("#[A-Z]+#", $password)) {
        $error = "Password must include at least one uppercase letter!";
    } elseif (!preg_match("#\W+#", $password)) {
        $error = "Password must include at least one symbol!";
    }

    if ($error) {
        echo $error; // I am not sure you need this, but just to make it behave the same as your original code.
        throw new \Exception($error);
    }
}

不要使用异常进行验证 - 特别是因为除了消息本身之外您没有使用异常,因为它包含的信息比消息多得多。异常是指某些东西——顾名思义——异常的、不同寻常的。无效的用户输入并不罕见,事实上这是很常见的事情。

重新排列您的代码,使其 returns 您可以在控制器中捕获的错误消息:

public function validatePasswordStrength($password, $username): string
{
    if ($password === '') {
        return "Password not entered!";
    }
    if ($password === $username) {
        return "Password and username can not be same!";
    }
    if (strlen($password) < 8) {
        return "Password must be at least 8 characters long!";
    }
    if (!preg_match("#[a-z]+#", $password)) {
        return "Password must include at least one lowercase letter!";
    }
    if (!preg_match("#[A-Z]+#", $password)) {
        return "Password must include at least one uppercase letter!";
    }
    if (!preg_match("#\W+#", $password)) {
        return "Password must include at least one symbol!";
    }

    return '';
}

/**
 * @Route("/check-password", name="check_password")
 */
public function validatePassword(Request $request, $password = 'S0235ds-')
{
    $username = 'Apple';
    $errorMessage = $this->methodService->validatePasswordStrength($password, $username);
    if ($errorMessage) {
        return $this->json(["message" => $errorMessage], Response::HTTP_BAD_REQUEST);
    }
    return $this->json(["message" => "SUCCESS"]);
}

更简单、更清晰并且不会不必要地使用作为异常对象的复杂构造。