| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables\Services\Batch_Operations; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel; |
| 7 |
use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 8 |
use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 9 |
|
| 10 |
class Batch_Error_Formatter { |
| 11 |
|
| 12 |
private const ERROR_MAP = [ |
| 13 |
RecordNotFound::class => [ |
| 14 |
'code' => 'variable_not_found', |
| 15 |
'status' => 404, |
| 16 |
], |
| 17 |
DuplicatedLabel::class => [ |
| 18 |
'code' => 'duplicated_label', |
| 19 |
'status' => 400, |
| 20 |
], |
| 21 |
VariablesLimitReached::class => [ |
| 22 |
'code' => 'invalid_variable_limit_reached', |
| 23 |
'status' => 400, |
| 24 |
], |
| 25 |
]; |
| 26 |
|
| 27 |
public function status_for( Exception $e ): int { |
| 28 |
foreach ( self::ERROR_MAP as $class => $map ) { |
| 29 |
if ( $e instanceof $class ) { |
| 30 |
return $map['status']; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return 500; |
| 35 |
} |
| 36 |
|
| 37 |
public function error_code_for( Exception $e ): string { |
| 38 |
foreach ( self::ERROR_MAP as $class => $map ) { |
| 39 |
if ( $e instanceof $class ) { |
| 40 |
return $map['code']; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return 'unexpected_server_error'; |
| 45 |
} |
| 46 |
} |
| 47 |
|