[php-src] Issue #13301: Add an iterface to throw that will not collect debug_backtrace on "new" call
| From: | 6562680 | Date: | Thu, 01 Feb 2024 12:04:02 +0000 |
| Subject: | [php-src] Issue #13301: Add an iterface to throw that will not collect debug_backtrace on "new" call | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246385@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13301
Author: 6562680
### Description
Look for javascript.
It allows to throw anything. String, array, object. Most of times its because classes is a sugar for
js most of times.
But.
In PHP even already designed an methodology that data validation should be performed as much earlier
than possible and then you have to use valid DTO to escape validation stuff inside service code.
Old article of Martin Fowler also explains about "errors" is not the
"exceptions". And next i will explain why...
I've written tonns of API's and i've noticed that sometimes errors is returned in
responses (no exactly one error), errors could be in request preparation, even then response couldbe
misformatted and produces errors that is not inside response directly.
While you working with the queues - your common case "save all errors to queue task cells"
even if you process them in batches.
Batch processing forces you sometimes to (map) tasks, sometimes to (reduce) them to aggregations,
sometimes (reduce) out from aggregations. That way some errors could be [copied] to few records,
some could be [separated] by types/indexes/uuids.
Would be great to catch "error batch" and create class for but one thing is here, and
thats not only problem in PHP.
Imagine you try to send bulk data with 1 000 000 records via CSV file. Each record could return
warning or error.
If you write code that throws error or error batch from each record - you solve the task, but
"new \Exception" collects backtrace on create. So you lost 0.05 seconds on each from
million rows in worst case. Other class cannot be thrown!
You always need to write your own errorBag class and errorBagStack class to implement it like that:
```php
<?php
function fn_a() {
$errorBagCurrent = _error_bag();
$errorBagChild = _error_bag_push();
fn_aa();
_error_bag_merge_as_warnings($errorBagPop = _error_bag_pop(), 'fn_aa');
// var_dump($errorBagChild === $errorBagPop); // true
}
function fn_aa() {
_error_bag_error('Hello');
}
$errorBagCurrent = _error_bag();
$errorBagCurrent = _error_bag();
_error_bag_push();
fn_a();
_error_bag_merge_as_is(_error_bag_pop(), 'fn_a');
var_dump($errorBagCurrent);
```
Btw, javascript way is good for low-level, but not ideal, because while you work with chains usually
you always have TWO returns - errors and result, because
catch statement could replace
error to result. So most of cases returned arrays with [0,1] keys where 0
is an error/error-bag, 1 is a result, if 0 key is not empty, 1 key is ignored.
Please, implement some feature to the language core that covers all cases above.
I dont know what solution could be the better, but, am using above printed _error_bag() for now