ErrorMsg update with target_style

This commit is contained in:
2023-09-15 18:32:55 +09:00
parent b3617954eb
commit e10987ce8b
2 changed files with 48 additions and 23 deletions

View File

@@ -15,7 +15,7 @@ use CoreLibs\Logging\Logger\MessageLevel;
class ErrorMessage class ErrorMessage
{ {
/** @var array<int,array{id:string,level:string,str:string,target:string,highlight:string[]}> */ /** @var array<int,array{id:string,level:string,str:string,target:string,target_style:string,highlight:string[]}> */
private array $error_str = []; private array $error_str = [];
/** @var \CoreLibs\Logging\Logging $log */ /** @var \CoreLibs\Logging\Logging $log */
public \CoreLibs\Logging\Logging $log; public \CoreLibs\Logging\Logging $log;
@@ -52,23 +52,28 @@ class ErrorMessage
* not set: unkown, will be logged as "emergency" * not set: unkown, will be logged as "emergency"
* target/highlight: id target name for frontend where to attach this message * target/highlight: id target name for frontend where to attach this message
* highlight is a list of other target points to highlight * 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 { ... }"
* 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 * @param string $error_id Any internal error ID for this error
* @param string $level Error level in ok/info/warn/error * @param string $level Error level in ok/info/warn/error
* @param string $str Error message (out) * @param string $str Error message (out)
* @param string $target alternate attachment point for this error message * @param string $target alternate attachment point for this error message
* @param array<string> $highlight Any additional error data as error OR * @param string $target_style Alternate color style for the error message
* highlight points for field highlights * @param array<string> $highlight Any additional error data as error OR
* @param string|null $message If abort/crash, non localized $str * highlight points for field highlights
* @param array<mixed> $context Additionl info for abort/crash messages * @param string|null $message If abort/crash, non localized $str
* @param bool|null $log_error [=null] log level 'error' to error, if null use global, * @param array<mixed> $context Additionl info for abort/crash messages
* else set for this call only * @param bool|null $log_error [=null] log level 'error' to error, if null use global,
* else set for this call only
*/ */
public function setErrorMsg( public function setErrorMsg(
string $error_id, string $error_id,
string $level, string $level,
string $str, string $str,
string $target = '', string $target = '',
string $target_style = '',
array $highlight = [], array $highlight = [],
?string $message = null, ?string $message = null,
array $context = [], array $context = [],
@@ -88,6 +93,7 @@ class ErrorMessage
'level' => $level, 'level' => $level,
'str' => $str, 'str' => $str,
'target' => $target, 'target' => $target,
'target_style' => $target_style,
'highlight' => $highlight, 'highlight' => $highlight,
]; ];
// write to log for abort/crash // write to log for abort/crash
@@ -126,28 +132,40 @@ class ErrorMessage
* Note, the parameter order is different and does not need an error id * Note, the parameter order is different and does not need an error id
* This is for backend alerts * This is for backend alerts
* *
* @param string $level error level (ok/warn/info/error) * @param string $level error level (ok/warn/info/error)
* @param string $str error string * @param string $str error string
* @param string|null $error_id optional error id for precise error lookup * @param string|null $error_id optional error id for precise error lookup
* @param string $target Alternate id name for output target on frontend * @param string $target Alternate id name for output target on frontend
* @param array<string> $highlight Any additional error data as error OR * @param string $target_style Alternate color style for the error message
* highlight points for field highlights * @param array<string> $highlight Any additional error data as error OR
* @param string|null $message If abort/crash, non localized $str * highlight points for field highlights
* @param array<mixed> $context Additionl info for abort/crash messages * @param string|null $message If abort/crash, non localized $str
* @param bool|null $log_error [=null] log level 'error' to error, if null use global, * @param array<mixed> $context Additionl info for abort/crash messages
* else set for this call only * @param bool|null $log_error [=null] log level 'error' to error, if null use global,
* else set for this call only
*/ */
public function setMessage( public function setMessage(
string $level, string $level,
string $str, string $str,
?string $error_id = null, ?string $error_id = null,
string $target = '', string $target = '',
string $target_style = '',
array $highlight = [], array $highlight = [],
?string $message = null, ?string $message = null,
array $context = [], array $context = [],
?bool $log_error = null, ?bool $log_error = null,
): void { ): void {
$this->setErrorMsg($error_id ?? '', $level, $str, $target, $highlight, $message, $context, $log_error); $this->setErrorMsg(
$error_id ?? '',
$level,
$str,
$target,
$target_style,
$highlight,
$message,
$context,
$log_error
);
} }
// ********************************************************************* // *********************************************************************
@@ -178,7 +196,7 @@ class ErrorMessage
* Gets the LAST entry in the array list. * Gets the LAST entry in the array list.
* If nothing found returns empty array set * If nothing found returns empty array set
* *
* @return array{id:string,level:string,str:string,target:string,highlight:string[]} Error block * @return array{id:string,level:string,str:string,target:string,target:string,highlight:string[]} Error block
*/ */
public function getLastErrorMsg(): array public function getLastErrorMsg(): array
{ {
@@ -187,6 +205,7 @@ class ErrorMessage
'str' => '', 'str' => '',
'id' => '', 'id' => '',
'target' => '', 'target' => '',
'target_string' => '',
'highlight' => [], 'highlight' => [],
]; ];
} }

View File

@@ -106,6 +106,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => $str, 'str' => $str,
'id' => '', 'id' => '',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
$em->getLastErrorMsg() $em->getLastErrorMsg()
@@ -139,6 +140,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'info', 'level' => 'info',
'str' => 'INFO MESSAGE', 'str' => 'INFO MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
$em->getLastErrorMsg() $em->getLastErrorMsg()
@@ -154,6 +156,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'info', 'level' => 'info',
'str' => 'INFO MESSAGE', 'str' => 'INFO MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
] ]
], ],
@@ -171,6 +174,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'error', 'level' => 'error',
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
$em->getLastErrorMsg() $em->getLastErrorMsg()
@@ -186,6 +190,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'info', 'level' => 'info',
'str' => 'INFO MESSAGE', 'str' => 'INFO MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
[ [
@@ -193,6 +198,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'error', 'level' => 'error',
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
] ]
], ],