Skip to content

Exception #0 (ReflectionException): Class "***********************" does not exist. Wrong logic. #38480

Open
@Genaker

Description

@Genaker

Preconditions and environment

Magento 2 Core all versions have a core issue. When the source model of the EAV class from the Database does not exist magento throws

ReflectionException

Exception #0 (ReflectionException): Class "**\Model\Config\Source***\Options" does not exist

/var/www/html/magento/vendor/magento/framework/Code/Reader/ClassReader.php

 public function getConstructor($className)
    {
        $class = new ReflectionClass($className); <-- Exception happens here 

however, it has logic to tell you exactly what attribute Model is missing

/var/www/html/magento/vendor/magento/module-eav/Model/Entity/Attribute/AbstractAttribute.php

public function getSource()
    {
        if (empty($this->_source)) {
            if (!$this->getSourceModel()) {
                $this->_source = $this->_getDefaultSourceModel();
            } else {
                $this->_source = $this->getSourceModel();
            }
        
            if (!$source) {
                throw new LocalizedException(
                    __(
                        'Source model "%1" not found for attribute "%2"',
                        $this->getSourceModel(),
                        $this->getAttributeCode()
                    )
                );
            }

but this logic never happens because it is an Exception(ReflectionException) not a bool value

you need to add this code and check exception

public function getSource()
{
if (empty($this->_source)) {
if (!$this->getSourceModel()) {
$this->_source = $this->_getDefaultSourceModel();
} else {
$this->_source = $this->getSourceModel();
}
try {
$source = $this->_universalFactory->create($this->_source);
} catch (\ReflectionException $e) {
$source = false;
}
if (!$source) {
throw new LocalizedException(
__(
'Source model "%1" not found for attribute "%2"',
$this->getSourceModel(),
$this->getAttributeCode()
)
);
}

then an exception will be self-explained:

Exception #0 (Magento\Framework\Exception\LocalizedException): Source model "*****\Model\Config\Source*\Options" not found for attribute "some_attribute"

Please fix it ASAP.

Related issue: #30278

Steps to reproduce

  • Create a custom product attribute programmatically with dropdown type.
  • Don't create a source class for the attribute.
  • Try to open a product in admin panel.
  • ReflectionException is thrown.

Expected result

Normal Exception explanation

Exception #0 (Magento\Framework\Exception\LocalizedException): Source model "*****\Model\Config\Source*\Options" not found for attribute "some_attribute"

Actual result

Exception #0 (ReflectionException): Class "***********************" does not exist.

Additional information

No response

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Labels

Area: FrameworkComponent: EavIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: ready for devReported on 2.4.xIndicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions