PHP:
class LibxmlException extends Exception {
public function __construct(LibXMLError $error) {
$this->libxmlError = $error;
}
public function __tostring() {
return $this->libxmlError->message;
}
}
class MyDOMdocument extends DOMdocument {
public function load($filename) {
// prevent libxml from generating php errors
$old_use_internal_errors = libxml_use_internal_errors(true);
// clear prev. libxml errors
libxml_clear_errors();
// load xml document
$doc = DOMdocument::load($filename);
// if loading failed, throw a LibxmlException exception
if ($doc === false) {
throw new LibxmlException(libxml_get_last_error());
}
// restore old use_internal_errors value
libxml_use_internal_errors($old_use_internal_errors);
return $doc;
}
}
-~{}~ 27.10.06 18:31:
Wikkid: не выскажешь мнение насчет
http://bugs.php.net/bug.php?id=29495 ?
Centaur: ну amt-php.net там сослался на
Священный Стандарт, в котором написано:
>
If an error occurs the document will fire an "error" event so that the caller knows that the load failed (see ParseErrorEvent).
и
>
No Exceptions
Wikkid: т.е. они все делают правильно?
Centaur: я не могу утверждать, что текущая реализация правильная, ибо я её не видел, но что я бы пометил request как wontfix и rejected -- факт
Wikkid: понятно, сенкс
Wikkid: не будем тогда на них ругаться %)
Centaur: если и ругаться тут, то на w3c
но и они всё придумали правильно, потому что, если метод вызывается асинхронно, то excpetion бросать некуда
Wikkid: асинхронно?
Centaur: *
Interface DocumentLS
** Attributes
*** async of type boolean
Indicates whether the method load should be synchronous or asynchronous. When the async attribute is set to true the load method returns control to the caller before the document has completed loading. The default value of this attribute is false.
** Methods
*** load
[...]nvoking this method will either block the caller or return to the caller immediately depending on the value of the async attribute. Once the document is fully loaded the document will fire a "load" event that the caller can register as a listener for. If an error occurs the document will fire an "error" event so that the caller knows that the load failed (see ParseErrorEvent).[/...]
Wikkid: более-менее понятно
Wikkid: ладно, будем, значит "на своих двоих"
Wikkid: а-ля вот так:
http://phpclub.ru/talk/showthread.php?postid=662709#post662709
Wikkid: поскольку асинхронной загрузки в пхп не ожидается, а без эксепшенов жить тяжело
Centaur: это как ты звонишь сотруднику и говоришь -- "загрузи XML, как будет готово, перезвони мне", он говорит "ok" и вешает трубку
он ещё не знает, что XML не загрузится, ему только остаётся, когда загрузка не удастся, перезвонить тебе (вызвать callback) и сказать "шеф, оно не грузиццо"
Centaur: а синхронно -- это ты говоришь "загрузи" и ждёшь, пока он загрузит
Centaur: ну там должна быть какая-то возможность повесить обработчик event'а error, по которому запоминать, что случилась ж##а, и какая именно, а потом по результату
load() === false трансформировать параметры этого события в exception
Wikkid: ну я в своем коде примерно так и делаю. Только, похоже, что в пхпшной реализации все таки callback'а не предусмотрено
Centaur: ну это хуже
Wikkid: есть только [m]libxml_get_last_error[/m](), которая просто отдает последнюю ошибку