Skip to main content
added 680 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

Magento throw, \Magento\Framework\Exception\AlreadyExistsException error type when we have we have tried to a new account on an existing email address.

$result = $connection->fetchOne($select, $bind);
        if ($result) {
            throw new AlreadyExistsException(
                __('A customer with the same email already exists in an associated website.')
            );
        }

So, on Controller, you can handler by this type of code

try{
......
}catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
    $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
 $message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
    $message = $e->getMessage();
    $this->_getSession()->addError($message);
}

BEIEF:

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before the understand how to define the exception and what type of exception, Magento use then you have to check Magento in -build exception type

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before the understand how to define the exception and what type of exception, Magento use then you have check Magento in -build exception type

Magento throw, \Magento\Framework\Exception\AlreadyExistsException error type when we have we have tried to a new account on an existing email address.

$result = $connection->fetchOne($select, $bind);
        if ($result) {
            throw new AlreadyExistsException(
                __('A customer with the same email already exists in an associated website.')
            );
        }

So, on Controller, you can handler by this type of code

try{
......
}catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
    $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
 $message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
    $message = $e->getMessage();
    $this->_getSession()->addError($message);
}

BEIEF:

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before the understand how to define the exception and what type of exception, Magento use then you have to check Magento in -build exception type

added 680 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before thrownthe understand how to define the exception and checkwhat type of exception, Magento use then you have to check Magento in build excpetion-build exception type

Here the list exception type from Magento/Framework/Exception

AbstractAggregateException.php  
AlreadyExistsException.php  
AuthenticationException.php
AuthorizationException.php  
ConfigurationMismatchException.php
CouldNotDeleteException.php
CouldNotSaveException.php
CronException.php
EmailNotConfirmedException.php
FileSystemException.php
InputException.php
IntegrationException.php
InvalidEmailOrPasswordException.php
LocalizedException.php
MailException.php
NoSuchEntityException.php
NotFoundException.php
PaymentException.php
RemoteServiceUnavailableException.php
RuntimeException.php
SecurityViolationException.php
SerializationException.php
SessionException.php
StateException.php
TemporaryStateExceptionInterface.php
ValidatorException.php

This exceptions type are used at Magento2.

How Magento define the exception.

Define Error type Suppose

Suppose, you want show throw exception of already to exist then

Then you throw error like

 if (Condition) {
        throw new \Magento\Framework\Exception\AlreadyExistsException(
            __('This record already exits')
        );
    }

catchCheck catch error Type So, on your try and catch

Check the error type and catch section using below pattern:

  catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $this->messageManager->addError($message);
}

For your case you can use below catch handling logic

catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
 $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
$message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
            $message = $e->getMessage();
 $this->_getSession()->addError($message);

}

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before thrown the exception and check you have to check Magento in build excpetion type

Here the list exception type from Magento/Framework/Exception

AbstractAggregateException.php  
AlreadyExistsException.php  
AuthenticationException.php
AuthorizationException.php  
ConfigurationMismatchException.php
CouldNotDeleteException.php
CouldNotSaveException.php
CronException.php
EmailNotConfirmedException.php
FileSystemException.php
InputException.php
IntegrationException.php
InvalidEmailOrPasswordException.php
LocalizedException.php
MailException.php
NoSuchEntityException.php
NotFoundException.php
PaymentException.php
RemoteServiceUnavailableException.php
RuntimeException.php
SecurityViolationException.php
SerializationException.php
SessionException.php
StateException.php
TemporaryStateExceptionInterface.php
ValidatorException.php

This exceptions type are used at Magento2.

How Magento define the exception.

Define Error type Suppose, you want show throw exception of already to exist then

Then you throw error like

 if (Condition) {
        throw new \Magento\Framework\Exception\AlreadyExistsException(
            __('This record already exits')
        );
    }

catch error So, on your try and catch section

  catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $this->messageManager->addError($message);
}

For your case you can use below catch handling logic

catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
 $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
$message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
            $message = $e->getMessage();
 $this->_getSession()->addError($message);

}

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before the understand how to define the exception and what type of exception, Magento use then you have check Magento in -build exception type

Here the list exception type from Magento/Framework/Exception

AbstractAggregateException
AlreadyExistsException
AuthenticationException
AuthorizationException
ConfigurationMismatchException
CouldNotDeleteException
CouldNotSaveException
CronException
EmailNotConfirmedException
FileSystemException
InputException
IntegrationException
InvalidEmailOrPasswordException
LocalizedException
MailException
NoSuchEntityException
NotFoundException
PaymentException
RemoteServiceUnavailableException
RuntimeException
SecurityViolationException
SerializationException
SessionException
StateException
TemporaryStateExceptionInterface
ValidatorException

This exceptions type are used at Magento2.

How Magento define the exception.

Define Error type

Suppose, you want show throw exception of already to exist then

Then you throw error like

 if (Condition) {
        throw new \Magento\Framework\Exception\AlreadyExistsException(
            __('This record already exits')
        );
    }

Check catch error Type

Check the error type and catch section using below pattern:

  catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $this->messageManager->addError($message);
}

For your case you can use below catch handling logic

catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
 $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
$message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
            $message = $e->getMessage();
 $this->_getSession()->addError($message);

}
added 680 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before thrown the exception and check you have to check Magento in build excpetion type

Here the list exception type from Magento/Framework/Exception

AbstractAggregateException.php  
AlreadyExistsException.php  
AuthenticationException.php
AuthorizationException.php  
ConfigurationMismatchException.php
CouldNotDeleteException.php
CouldNotSaveException.php
CronException.php
EmailNotConfirmedException.php
FileSystemException.php
InputException.php
IntegrationException.php
InvalidEmailOrPasswordException.php
LocalizedException.php
MailException.php
NoSuchEntityException.php
NotFoundException.php
PaymentException.php
RemoteServiceUnavailableException.php
RuntimeException.php
SecurityViolationException.php
SerializationException.php
SessionException.php
StateException.php
TemporaryStateExceptionInterface.php
ValidatorException.php

This exceptions type are used at Magento2.

How Magento define the exception.

Define Error type Suppose, you want show throw exception of already to exist then

Then you throw error like

 if (Condition) {
        throw new \Magento\Framework\Exception\AlreadyExistsException(
            __('This record already exits')
        );
    }

catch error So, on your try and catch section

  catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $this->messageManager->addError($message);
}

For your case you can use below catch handling logic

catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
 $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
$message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
            $message = $e->getMessage();
 $this->_getSession()->addError($message);

}

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before thrown the exception and check you have to check Magento in build excpetion type

Here the list exception type from Magento/Framework/Exception

AbstractAggregateException.php  
AlreadyExistsException.php  
AuthenticationException.php
AuthorizationException.php  
ConfigurationMismatchException.php
CouldNotDeleteException.php
CouldNotSaveException.php
CronException.php
EmailNotConfirmedException.php
FileSystemException.php
InputException.php
IntegrationException.php
InvalidEmailOrPasswordException.php
LocalizedException.php
MailException.php
NoSuchEntityException.php
NotFoundException.php
PaymentException.php
RemoteServiceUnavailableException.php
RuntimeException.php
SecurityViolationException.php
SerializationException.php
SessionException.php
StateException.php
TemporaryStateExceptionInterface.php
ValidatorException.php

This exceptions type are used at Magento2.

How Magento define the exception.

Define Error type Suppose, you want show throw exception of already to exist then

Then you throw error like

 if (Condition) {
        throw new \Magento\Framework\Exception\AlreadyExistsException(
            __('This record already exits')
        );
    }

catch error So, on your try and catch section

  catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $this->messageManager->addError($message);
}

Magento2 have used little bit different type of exception handling for creating thrown exception and check the exception type.

Before thrown the exception and check you have to check Magento in build excpetion type

Here the list exception type from Magento/Framework/Exception

AbstractAggregateException.php  
AlreadyExistsException.php  
AuthenticationException.php
AuthorizationException.php  
ConfigurationMismatchException.php
CouldNotDeleteException.php
CouldNotSaveException.php
CronException.php
EmailNotConfirmedException.php
FileSystemException.php
InputException.php
IntegrationException.php
InvalidEmailOrPasswordException.php
LocalizedException.php
MailException.php
NoSuchEntityException.php
NotFoundException.php
PaymentException.php
RemoteServiceUnavailableException.php
RuntimeException.php
SecurityViolationException.php
SerializationException.php
SessionException.php
StateException.php
TemporaryStateExceptionInterface.php
ValidatorException.php

This exceptions type are used at Magento2.

How Magento define the exception.

Define Error type Suppose, you want show throw exception of already to exist then

Then you throw error like

 if (Condition) {
        throw new \Magento\Framework\Exception\AlreadyExistsException(
            __('This record already exits')
        );
    }

catch error So, on your try and catch section

  catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $this->messageManager->addError($message);
}

For your case you can use below catch handling logic

catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
 $this->_getSession()->addError($message);
}catch(\Magento\Framework\Exception\LocalizedException $e){
$message = $e->getMessage();
 $this->_getSession()->addError($message);
}catch (\Exception $e) {
            $message = $e->getMessage();
 $this->_getSession()->addError($message);

}
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
Loading