[英]PHP exception and user message
我有一個項目對象。 用戶可以為其分配工作對象。
在99%的情況下,項目對象已設置了所有適當的字段。 在某些情況下,項目缺少字段。
為了將工作人員分配給項目,項目必須具有所有必填字段設置。
為了解決這個問題,我拋出了這樣的異常:(不要試圖在這里找到模式,實際的例子更復雜)
if ($project->startDate == false ) { throw new Exception("Missing startDate attribute for project id: $id"); } if ($project->finishDate == false ) { throw new Exception("Missing finishDate attribute for project id: $id"); } if ($project->startDate > $project->finishDate ) { throw new Exception("Invalid start date for project id: $id"); }
問題是我需要每次向用戶顯示自定義消息。 例如,如果引發第一個錯誤,則用戶應看到:“請設置項目開始日期”,依此類推。
我怎樣才能做到這一點 ?
只需try .. catch
定義自己的異常類和整個代碼即可:
class FormException extends Exception {
private var $userMessage;
public function __construct($message, $userMessage) {
parent::__construct($message);
$this->userMessage = $userMessage;
}
public function getUserMessage() {return $this->userMessage;}
}
try {
// Whole code goes here, probably a function call
throw new FormException("Missing startDate attribute for project id: $id",
'Please setup the project start date');
} catch (FormException $e) {
echo $e->getUserMessage();
error_log($e->getMessage());
}
順便說一句,如果要在字符串中包含變量內容,請使用雙引號( "id: $id"
)或連接( 'id: ' . $id
)。
嘗試這個:
try
{
$Message = '';
if ($project->startDate == false ) {
$Message = "Please setup the project start date\n";
throw new Exception("Missing startDate attribute for project id: $id");
}
if ($project->finishDate == false ) {
$Message = "Please setup the project finish date\n";
throw new Exception("Missing finishDate attribute for project id: $id");
}
if ($project->approved == false ) {
$Message = "Please setup the project approved field\n";
throw new Exception("Missing approved attribute for project id: $id");
}
}
catch(Exception $Ex)
{
// Log in some way you like the $Ex-getMessage();
echo nl2br($Message);
}
在您的項目類中,創建一個新方法,可能稱為getErrorMessage
。
該功能應進行檢查(無需在項目外部具有具體的驗證方式或為項目創建驗證對象)。 然后:
if ($message = $project->getErrorMessage())
{
throw new Exception(sprintf('%s (project id: %d)', $message, $id));
}
如果您出於設計原因決定不拋出異常,則仍然有用。
驗證程序對象可以自然地提供比單個方法更多的詳細信息,從而獲得更大的靈活性。 因此,這樣做可能更好:
class ProjectValidator
{
private $project;
private $errors;
public function __construct($project)
{
$this->project = $project;
}
public function isValid()
{
// run your checks, add errors to the array as appropriate.
$errors = array();
if (!$this->project->startDate)
{
$errors[] = 'Missing startDate attribute';
}
if (!$this->project->finishDate)
{
$errors[] = 'Missing finishDate attribute';
}
if (!$this->project->approved)
{
$errors[] = 'Missing approved attribute';
}
$this->errors = $errors;
return (bool) count($this->errors);
}
public function getErrors()
{
return $this->errors;
}
}
$validator = new ProjectValidator($project);
if (!$validator->isValid())
{
// throw an exception or do whatever you want in case it's not valid
$errors = $validator->getMessages();
$message = sprintf("Project id: %d has the following %d error(s):\n", $id, count($errors));
foreach($errors as $index => $error)
{
$message .= sprintf("%d. %s\n", $index+1, $error);
}
throw new Exception($message);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.