Logging\ErrorMsg add jump target list

This commit is contained in:
Clemens Schwaighofer
2023-10-02 14:04:59 +09:00
parent 0a885f215c
commit 71c9fd401d
2 changed files with 114 additions and 0 deletions

View File

@@ -17,6 +17,8 @@ class ErrorMessage
{
/** @var array<int,array{id:string,level:string,str:string,target:string,target_style:string,highlight:string[]}> */
private array $error_str = [];
/** @var array<string,string> */
private array $jump_targets;
/** @var \CoreLibs\Logging\Logging $log */
public \CoreLibs\Logging\Logging $log;
@@ -61,6 +63,8 @@ class ErrorMessage
* highlight is a list of other target points to highlight
* for highlight targets css names are $level without a prefix and should be
* nested in the target element "input .error { ... }"
* jump_target: a target id for to jump and message, is stored in separate jump array
* where the target is unique, first one set is used for info message
* target_style: if not set uses 'error-' $level as css style. applies to targets or main only
*
* @param string $error_id Any internal error ID for this error
@@ -70,6 +74,9 @@ class ErrorMessage
* @param string $target_style Alternate color style for the error message
* @param array<string> $highlight Any additional error data as error OR
* highlight points for field highlights
* @param array{}|array{target:string,info?:string} $jump_target with "target" for where to jump and
* "info" for string to show in jump list
* target must be set, if info not set, default message used
* @param string|null $message If abort/crash, non localized $str
* @param array<mixed> $context Additionl info for abort/crash messages
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
@@ -82,6 +89,7 @@ class ErrorMessage
string $target = '',
string $target_style = '',
array $highlight = [],
array $jump_target = [],
?string $message = null,
array $context = [],
?bool $log_error = null,
@@ -103,6 +111,8 @@ class ErrorMessage
'target_style' => $target_style,
'highlight' => $highlight,
];
// set a jump target
$this->setJumpTarget($jump_target['target'] ?? null, $jump_target['info'] ?? null);
// write to log for abort/crash
switch ($level) {
case 'notice':
@@ -152,6 +162,9 @@ class ErrorMessage
* @param string $target_style Alternate color style for the error message
* @param array<string> $highlight Any additional error data as error OR
* highlight points for field highlights
* @param array{}|array{target:string,info?:string} $jump_target with "target" for where to jump and
* "info" for string to show in jump list
* target must be set, if info not set, default message used
* @param string|null $message If abort/crash, non localized $str
* @param array<mixed> $context Additionl info for abort/crash messages
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
@@ -164,6 +177,7 @@ class ErrorMessage
string $target = '',
string $target_style = '',
array $highlight = [],
array $jump_target = [],
?string $message = null,
array $context = [],
?bool $log_error = null,
@@ -175,12 +189,38 @@ class ErrorMessage
$target,
$target_style,
$highlight,
$jump_target,
$message,
$context,
$log_error
);
}
/**
* Set a jump target. This can be used to jump directly a frontend html block
* with the target id set
*
* @param string|null $target
* @param string|null $info
* @return void
*/
public function setJumpTarget(
?string $target,
?string $info,
): void {
if (
empty($target) ||
!empty($this->jump_targets[$target])
// also check if this is an alphanumeric string? css id compatible?
) {
return;
}
if (empty($info)) {
$info = 'Jump to: ' . $target;
}
$this->jump_targets[$target] = $info;
}
// *********************************************************************
// GETTERS
// *********************************************************************
@@ -223,6 +263,16 @@ class ErrorMessage
];
}
/**
* Return the jump target list
*
* @return array{}|array{string,string} List of jump targets with info text, or empty array if not set
*/
public function getJumpTarget(): array
{
return $this->jump_targets ?? [];
}
// *********************************************************************
// FLAG SETTERS
// *********************************************************************

View File

@@ -421,6 +421,70 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
);
}
}
/**
* Undocumented function
*
* @testdox Test jump target set and reporting
*
* @return void
*/
public function testJumpTarget(): void
{
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesLogDebug',
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Debug,
'log_per_run' => true
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setJumpTarget(
'target-f',
'Target text'
);
$this->assertEquals(
[
'target-f' => 'Target text'
],
$em->getJumpTarget()
);
// set same target, keep as before
$em->setJumpTarget(
'target-f',
'Other text'
);
$this->assertEquals(
[
'target-f' => 'Target text'
],
$em->getJumpTarget()
);
// add new now two messages
$em->setJumpTarget(
'target-s',
'More text'
);
$this->assertEquals(
[
'target-f' => 'Target text',
'target-s' => 'More text'
],
$em->getJumpTarget()
);
// add empty info
$em->setJumpTarget(
'target-e',
''
);
$this->assertEquals(
[
'target-f' => 'Target text',
'target-s' => 'More text',
'target-e' => 'Jump to: target-e'
],
$em->getJumpTarget()
);
}
}
// __END__