-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathExceptionCollector.php
67 lines (60 loc) · 1.64 KB
/
ExceptionCollector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Exceptions\Collector;
class ExceptionCollector
{
/**
* Private array containing all errors to be thrown as part of the exception.
*
* @var array
*/
private $errors = [];
/**
* Function to add a filename and message for the filename
*
* @param string $filename
* @param string $message
* @return void
*/
public function addError($filename, $message)
{
$error[$filename] = $message;
$this->errors = array_merge_recursive($this->errors, $error);
}
/**
* Function which throws an exception when there are errors present.
*
* @return void
* @throws \Exception
*/
public function throwException()
{
if (empty($this->errors)) {
return;
}
$errorMsg = implode("\n\n", $this->formatErrors($this->errors));
throw new \Exception("\n" . $errorMsg);
}
/**
* If there are multiple exceptions for a single file, the function flattens the array so they can be printed
* as separate messages.
*
* @param array $errors
* @return array
*/
private function formatErrors($errors)
{
$flattenedErrors = [];
foreach ($errors as $errorMsg) {
if (is_array($errorMsg)) {
$flattenedErrors = array_merge($flattenedErrors, $this->formatErrors($errorMsg));
continue;
}
$flattenedErrors[] = $errorMsg;
}
return $flattenedErrors;
}
}