{"action":"created","issue":{"url":"https://api.github.com/repos/preillyme/v8js/issues/144","labels_url":"https://api.github.com/repos/preillyme/v8js/issues/144/labels{/name}","comments_url":"https://api.github.com/repos/preillyme/v8js/issues/144/comments","events_url":"https://api.github.com/repos/preillyme/v8js/issues/144/events","html_url":"https://github.com/preillyme/v8js/issues/144","id":90651769,"number":144,"title":"Catching PHP Exceptions in Javascript?","user":{"login":"tahpot","id":164973,"avatar_url":"https://avatars.githubusercontent.com/u/164973?v=3","gravatar_id":"","url":"https://api.github.com/users/tahpot","html_url":"https://github.com/tahpot","followers_url":"https://api.github.com/users/tahpot/followers","following_url":"https://api.github.com/users/tahpot/following{/other_user}","gists_url":"https://api.github.com/users/tahpot/gists{/gist_id}","starred_url":"https://api.github.com/users/tahpot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tahpot/subscriptions","organizations_url":"https://api.github.com/users/tahpot/orgs","repos_url":"https://api.github.com/users/tahpot/repos","events_url":"https://api.github.com/users/tahpot/events{/privacy}","received_events_url":"https://api.github.com/users/tahpot/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-06-24T11:41:16Z","updated_at":"2015-08-21T13:50:52Z","closed_at":null,"body":"I regularly have JS code executing in V8 which calls PHP code. If that PHP code raises an exception, I want to catch the exception in Javascript.\r\n\r\nIs that possible at all?\r\n\r\nHere's some example Javascript:\r\n\r\n```\r\ntry {\r\n  PHP.throwException();\r\n}\r\ncatch (err) {\r\n  // do something in javascript\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/preillyme/v8js/issues/comments/133429939","html_url":"https://github.com/preillyme/v8js/issues/144#issuecomment-133429939","issue_url":"https://api.github.com/repos/preillyme/v8js/issues/144","id":133429939,"user":{"login":"stesie","id":113068,"avatar_url":"https://avatars.githubusercontent.com/u/113068?v=3","gravatar_id":"","url":"https://api.github.com/users/stesie","html_url":"https://github.com/stesie","followers_url":"https://api.github.com/users/stesie/followers","following_url":"https://api.github.com/users/stesie/following{/other_user}","gists_url":"https://api.github.com/users/stesie/gists{/gist_id}","starred_url":"https://api.github.com/users/stesie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stesie/subscriptions","organizations_url":"https://api.github.com/users/stesie/orgs","repos_url":"https://api.github.com/users/stesie/repos","events_url":"https://api.github.com/users/stesie/events{/privacy}","received_events_url":"https://api.github.com/users/stesie/received_events","type":"User","site_admin":false},"created_at":"2015-08-21T13:50:52Z","updated_at":"2015-08-21T13:50:52Z","body":"Hmm, `tests/exception_propagation_2.phpt` even relies on this. I'm currently a bit unsure on how to fix this...\r\n\r\n* the problem occurs if immediate exception propagation is disabled\r\n* currently if `executeString` exits it checks for pending exceptions *from previous calls* and throws these; however *after* the code itself was executed\r\n\r\nSo if we now make sure to immediately stop JS execution if a PHP exception is thrown, then ...\r\n\r\n```php\r\nclass Foo {\r\n        private $v8 = NULL;\r\n\r\n        public function __construct()\r\n        {\r\n                $this->v8 = new V8Js(null, array(), array(), false);\r\n                $this->v8->foo = $this;\r\n                $this->v8->executeString('fooobar', 'throw_0');\r\n                var_dump($this->v8->getPendingException());\r\n                $this->v8->executeString('try { PHP.foo.bar(); } catch (e) { print(e + \" caught!\\n\"); }', 'trycatch1');\r\n                $this->v8->executeString('try { PHP.foo.bar(); } catch (e) { print(e + \" caught!\\n\"); }', 'trycatch2');\r\n        }\r\n\r\n        public function bar()\r\n        {\r\n                echo \"To Bar!\\n\";\r\n                $this->v8->executeString('throw new Error();', 'throw_1');\r\n        }\r\n}\r\n\r\ntry {\r\n        $foo = new Foo();\r\n```\r\n\r\n... fails.\r\n\r\n* the first `executeString` call causes an JS error, which is stored as a pending exception\r\n* the `getPendingException` call displays it, however doesn't clear it\r\n* the succeeding `executeString` call provides a try/catch for and Error thrown and calls `bar` before exiting\r\n* ... the `bar` function calls `executeString` to throw the Error; and `executeString` will exit now, hence tests for the pending exception, notices it and throws a PHP exception + stops execution there\r\n* hence the catch-part of the second `executeString` is now *not* executed anymore\r\n\r\nBesides this test also actually tests that the third `executeString` in `__construct` isn't called, since a PHP exception is now pending.\r\n\r\nThis leads me to some options\r\n\r\n1. consider the test borked, leave pending exception behaviour as before and terminate execution as described above\r\n2. throw pending exceptions only on out-most executeString level (so that might absorb pending exceptions)\r\n3. like 2, throw pending exceptions if further exceptions occur, otherwise don't\r\n4. in pending exception mode JS execution should not be terminated\r\n\r\nI think options 2 to 4 might feel a bit magic to the user; I tend to pick option 1 ... what do you think?\r\n\r\nThis will get particularly funny if we throw PHP exceptions to JS; in the example above the JS-catch wouldn't get the expected Error thrown by bar, but the pending exception from a prior executeString call ...\r\n... which again would be kindof magic to the user; hence should we only throw any other PHP exceptions to JS? \r\nWhat about other V8Js exceptions? e.g. V8 calls PHP code, that calls V8 code in turn, that causes some kind of script exception or timeout?  Shall the JS code be able to see other V8 instances errors then?\r\n\r\nI tend to accept this behaviour, if the implementor enables PHP->JS exception passing, any exception is passed, even unhandled pending ones\r\n\r\nenough stuff for discussion, what do you think? @tahpot, @rosmo and others?"}}