Answer the question
In order to leave comments, you need to log in
Do I need to throw an exception when receiving erroneous (invalid) data?
Do I need to throw an exception in cases where the data did not pass validation? Or is it enough just to put the code and the error message into the errors array and work with it when outputting?
UPD. To be clear: I only want to use exceptions to log traces to a file. Is the information from Validation Exceptions useful during debugging?
UPD2. Understood. Discussed following. logic:
Working with erroneous data can cause an error directly during their processing. For example, an attempt to watermark a received file will cause an error if another type of file was received (**.exe, .*psd, etc.)
Answer the question
In order to leave comments, you need to log in
Yes, you do, in most cases. You can create a ValidationException class that takes an array of error messages in the constructor and inherit it from the Exception class:
class ValidationException extends Exception
{
/**
* Validation error messages.
*
* @var array
*/
protected $errors = [];
/**
* Constructor.
*
* @param array $errors
*/
public function __construct($errors)
{
$this -> errors = $errors;
}
/* геттер для свойства $errors */
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question