Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function onPhpErrorCapturedByNativePart(PhpErrorData $phpErrorData): void
if (
$this->lastThrown !== null
&& $phpErrorData->message !== null
&& TextUtil::isPrefixOf('Uncaught Exception: ', $phpErrorData->message, /* isCaseSensitive: */ false)
&& TextUtil::isPrefixOf('Uncaught ' . get_class($this->lastThrown) . ':', $phpErrorData->message, /* isCaseSensitive: */ false)
) {
$relatedThrowable = $this->lastThrown;
$this->lastThrown = null;
Expand Down
112 changes: 112 additions & 0 deletions tests/ElasticApmTests/ComponentTests/ErrorComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,116 @@ function () use ($testArgs): void {
}
);
}

public static function appCodeForTestPhpErrorUncaughtExceptionSubclassWrapper(bool $justReturnLineNumber = false): int
{
if (!$justReturnLineNumber) {
ini_set('display_errors', '0');
}

return $justReturnLineNumber ? __LINE__ : appCodeForTestPhpErrorUncaughtExceptionSubclass();
}

private function implTestPhpErrorUncaughtExceptionSubclass(MixedMap $testArgs): void
{
AssertMessageStack::newScope(/* out */ $dbgCtx, AssertMessageStack::funcArgs());

$captureErrorsOptVal = $testArgs->getBool(OptionNames::CAPTURE_ERRORS);
$captureErrorsWithPhpPartOptVal = $testArgs->getBool(OptionNames::CAPTURE_ERRORS_WITH_PHP_PART);
$captureExceptionsOptVal = $testArgs->getNullableBool(OptionNames::CAPTURE_EXCEPTIONS);
$shouldCaptureExceptionsDerivedCfg = $captureExceptionsOptVal ?? $captureErrorsOptVal;
$devInternalCaptureErrorsOnlyToLogOptVal = $testArgs->getBool(OptionNames::DEV_INTERNAL_CAPTURE_ERRORS_ONLY_TO_LOG);

$testCaseHandle = $this->getTestCaseHandle();
$appCodeHost = self::ensureMainAppCodeHost($testCaseHandle, $testArgs);

$appCodeHost->sendRequest(
AppCodeTarget::asRouted([__CLASS__, 'appCodeForTestPhpErrorUncaughtExceptionSubclassWrapper']),
function (AppCodeRequestParams $appCodeRequestParams): void {
if ($appCodeRequestParams instanceof HttpAppCodeRequestParams) {
$appCodeRequestParams->expectedHttpResponseStatusCode = HttpConstantsForTests::STATUS_INTERNAL_SERVER_ERROR;
}
}
);

if ($captureErrorsWithPhpPartOptVal) {
$isErrorExpected = $shouldCaptureExceptionsDerivedCfg;
} else {
if (self::isMainAppCodeHostHttp()) {
$isErrorExpected = $captureErrorsOptVal || $shouldCaptureExceptionsDerivedCfg;
} else {
$isErrorExpected = $captureErrorsOptVal;
}
}
$isErrorExpected = $isErrorExpected && !$devInternalCaptureErrorsOnlyToLogOptVal;

$expectedErrorCount = $isErrorExpected ? 1 : 0;
$dataFromAgent = $testCaseHandle->waitForDataFromAgent((new ExpectedEventCounts())->transactions(1)->errors($expectedErrorCount));
$dbgCtx->add(compact('dataFromAgent'));

if (self::isMainAppCodeHostHttp()) {
self::assertSame(Constants::OUTCOME_FAILURE, $dataFromAgent->singleTransaction()->outcome);
}

self::assertCount($expectedErrorCount, $dataFromAgent->idToError);
if (!$isErrorExpected) {
return;
}

$err = $this->verifyError($dataFromAgent);

$appCodeFile = FileUtilForTests::listToPath([dirname(__FILE__), 'appCodeForTestPhpErrorUncaughtException.php']);
self::assertNotNull($err->exception);
self::assertNull($err->exception->module);
if ($shouldCaptureExceptionsDerivedCfg) {
$culpritFunction = __NAMESPACE__ . '\\appCodeForTestPhpErrorUncaughtExceptionSubclassImpl';
self::assertSame($culpritFunction, $err->culprit);

$defaultCode = (new \RuntimeException(''))->getCode();
self::assertSame($defaultCode, $err->exception->code);
self::assertSame(APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_MESSAGE, $err->exception->message);
self::assertSame('RuntimeException', $err->exception->type);
$expectedStackTraceTop = [
[
self::STACK_TRACE_FILE_NAME => $appCodeFile,
self::STACK_TRACE_FUNCTION => null,
self::STACK_TRACE_LINE_NUMBER => APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_ERROR_LINE_NUMBER,
],
[
self::STACK_TRACE_FILE_NAME => $appCodeFile,
self::STACK_TRACE_FUNCTION => __NAMESPACE__ . '\\appCodeForTestPhpErrorUncaughtExceptionSubclassImpl',
self::STACK_TRACE_LINE_NUMBER => APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_CALL_TO_IMPL_LINE_NUMBER,
],
[
self::STACK_TRACE_FILE_NAME => __FILE__,
self::STACK_TRACE_FUNCTION => __NAMESPACE__ . '\\appCodeForTestPhpErrorUncaughtExceptionSubclass',
self::STACK_TRACE_LINE_NUMBER => self::appCodeForTestPhpErrorUncaughtExceptionSubclassWrapper(/* justReturnLineNumber */ true),
],
[
self::STACK_TRACE_FUNCTION => __CLASS__ . '::appCodeForTestPhpErrorUncaughtExceptionSubclassWrapper',
],
];
self::verifyAppCodeStackTraceTop($expectedStackTraceTop, $err);
} else { // if ($shouldCaptureExceptionsDerivedCfg)
self::assertNull($err->culprit);

self::assertNotNull($err->exception->message);
self::assertStringContainsString(APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_MESSAGE, $err->exception->message);
self::assertNotNull($err->exception->stacktrace);
self::assertCount(0, $err->exception->stacktrace);
}
}

/**
* @dataProvider dataProviderForTestsForErrorCausedByException
*/
public function testPhpErrorUncaughtExceptionSubclass(MixedMap $testArgs): void
{
self::runAndEscalateLogLevelOnFailure(
self::buildDbgDescForTestWithArtgs(__CLASS__, __FUNCTION__, $testArgs),
function () use ($testArgs): void {
$this->implTestPhpErrorUncaughtExceptionSubclass($testArgs);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,33 @@ function appCodeForTestPhpErrorUncaughtException(): int
TestCase::assertSame(APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_CALL_TO_IMPL_LINE_NUMBER, __LINE__ + 1);
appCodeForTestPhpErrorUncaughtExceptionImpl(); // <- APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_CALL_TO_IMPL_LINE_NUMBER
}

const APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_MESSAGE = 'Message for uncaught RuntimeException subclass';

const APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_ERROR_LINE_NUMBER = 89;

/**
* @return never
*
* @throws \RuntimeException
*
* @noinspection PhpReturnDocTypeMismatchInspection
*/
function appCodeForTestPhpErrorUncaughtExceptionSubclassImpl()
{
TestCase::assertSame(APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_ERROR_LINE_NUMBER, __LINE__ + 1);
throw new \RuntimeException(APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_MESSAGE); // <- APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_ERROR_LINE_NUMBER
}

const APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_CALL_TO_IMPL_LINE_NUMBER = 102;

/**
* @return never
*
* @throws \RuntimeException
*/
function appCodeForTestPhpErrorUncaughtExceptionSubclass(): int
{
TestCase::assertSame(APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_CALL_TO_IMPL_LINE_NUMBER, __LINE__ + 1);
appCodeForTestPhpErrorUncaughtExceptionSubclassImpl(); // <- APP_CODE_FOR_TEST_PHP_ERROR_UNCAUGHT_EXCEPTION_SUBCLASS_CALL_TO_IMPL_LINE_NUMBER
}