Browse Source

Small modifications to PHP exception/error handling (#4973)

* Small modifications to PHP exception/error handling

make HExceptions with an error code != null catchable, i.e. HExceptions from _hx_error_handler
remove duplicated error message, filename and line from  _hx_error_handler

* add simple test

* fix test
Andreas Mokros 9 years ago
parent
commit
019c78a4bb
3 changed files with 14 additions and 3 deletions
  1. 1 1
      src/generators/genphp.ml
  2. 2 2
      std/php/Boot.hx
  3. 11 0
      tests/unit/src/unit/issues/Issue4973.hx

+ 1 - 1
src/generators/genphp.ml

@@ -1572,7 +1572,7 @@ and gen_expr ctx e =
 		let catchall = ref false in
 		let evar = define_local ctx "_ex_" in
 		newline ctx;
-		print ctx "$%s = ($%s instanceof HException) ? $%s->e : $%s" evar ex ex ex;
+		print ctx "$%s = ($%s instanceof HException) && $%s->getCode() == null ? $%s->e : $%s" evar ex ex ex ex;
 		old();
 		List.iter (fun (v,e) ->
 			let ev = define_local ctx v.v_name in

+ 2 - 2
std/php/Boot.hx

@@ -319,8 +319,8 @@ function _hx_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
 	if (!(error_reporting() & $errno)) {
 		return false;
 	}
-	$msg = $errmsg . ' (errno: ' . $errno . ') in ' . $filename . ' at line #' . $linenum;
-	$e = new HException($msg, $errmsg, $errno, _hx_anonymous(array('fileName' => 'Boot.hx', 'lineNumber' => __LINE__, 'className' => 'php.Boot', 'methodName' => '_hx_error_handler')));
+	$msg = $errmsg . ' (errno: ' . $errno . ')';
+	$e = new HException($msg, '', $errno, _hx_anonymous(array('fileName' => 'Boot.hx', 'lineNumber' => __LINE__, 'className' => 'php.Boot', 'methodName' => '_hx_error_handler')));
 	$e->setFile($filename);
 	$e->setLine($linenum);
 	throw $e;

+ 11 - 0
tests/unit/src/unit/issues/Issue4973.hx

@@ -0,0 +1,11 @@
+package unit.issues;
+
+class Issue4973 extends Test {
+	#if php
+	function test() {
+		try sys.io.File.getContent("not-existant")
+		catch(exc:php.Exception) t(untyped __php__("{0} instanceof Exception", exc))
+		catch(exc:Dynamic) t(untyped __php__("{0} instanceof Exception", exc));
+	}
+	#end
+}