Skip to content

Commit b7aeaf9

Browse files
authored
ENGCOM-7532: Improve developer experience: RuntimeException thrown by AbstractFactory::createObject now contains the reason. #26572
2 parents 61b94c5 + e31db8c commit b7aeaf9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ protected function createObject($type, $args)
129129
);
130130

131131
throw new RuntimeException(
132-
new Phrase('Type Error occurred when creating object: %type', ['type' => $type])
132+
new Phrase('Type Error occurred when creating object: %type, %msg', [
133+
'type' => $type,
134+
'msg' => $exception->getMessage()
135+
])
133136
);
134137
}
135138
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/CompiledTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\ObjectManager\Test\Unit\Factory;
99

10+
use Magento\Framework\Exception\RuntimeException;
1011
use Magento\Framework\ObjectManager\ConfigInterface;
1112
use Magento\Framework\ObjectManager\DefinitionInterface;
1213
use Magento\Framework\ObjectManager\Factory\Compiled;
@@ -19,6 +20,8 @@
1920
use PHPUnit\Framework\TestCase;
2021

2122
/**
23+
* Test for \Magento\Framework\ObjectManager\Factory\Compiled.
24+
*
2225
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2326
*/
2427
class CompiledTest extends TestCase
@@ -116,6 +119,27 @@ public function testCreateSimple()
116119
$this->assertNull($result->getNullValue());
117120
}
118121

122+
/**
123+
* Create class with exception
124+
*
125+
* @return void
126+
*/
127+
public function testCreateSimpleWithException(): void
128+
{
129+
$requestedType = 'requestedType';
130+
$className = SimpleClassTesting::class;
131+
132+
$this->config->expects($this->atLeastOnce())
133+
->method('getInstanceType')
134+
->willReturn($className);
135+
136+
$this->expectException(RuntimeException::class);
137+
$this->expectExceptionMessage(
138+
'Type Error occurred when creating object: ' . $className . ', Too few arguments to function ' . $className
139+
);
140+
$this->factory->create($requestedType, []);
141+
}
142+
119143
/**
120144
* Test create simple configured arguments
121145
*/

0 commit comments

Comments
 (0)