Browse Source

* Simplify path returns
* Make error handling even more robust

Nathaniel van Diepen 7 years ago
parent
commit
662b6af3f2
2 changed files with 22 additions and 13 deletions
  1. 1 3
      App/path.class.php
  2. 21 10
      app.class.php

+ 1 - 3
App/path.class.php

@@ -18,9 +18,7 @@
 			$ret = true;
 			foreach($this->handles as $k => $fn){
 				try{
-					if($fn($req, $res, $args, $err) === false){
-						$ret = false;
-					}
+					$ret = $fn($req, $res, $args, $err) !== false;
 				}catch(\Exception $e){
 					$err = $e;
 				}

+ 21 - 10
app.class.php

@@ -73,17 +73,23 @@
 			$verb = Request::get_verb();
 			$url = Request::get_url();
 			$data = Request::get_body();
+			if(ob_get_level() > 0){
+				ob_clean();
+			}
 			foreach(static::$apps as $k => $app){
 				if($app instanceof App){
 					$app->handle($verb, $url, $data)->shutdown();
 				}
 			}
+			while(ob_get_level() > 0){
+				ob_end_flush();
+			}
 			if(is_callable('parent::__destruct')){
 				parent::__destruct();
 			}
 		}
 		public static function shutdown_error($error){
-			if(count(ob_list_handlers())){
+			while(ob_get_level() > 0){
 				ob_end_clean();
 			}
 			if(count(static::$apps)){
@@ -95,6 +101,7 @@
 					$app->onerror($app->request, $app->response, $error);
 					$app->response->shutdown();
 				}
+				flush();
 			}else{
 				$res =new Response();
 				$res->code(500)
@@ -126,7 +133,7 @@
 						while(is_string($router)){
 							$router = $this->domains[$router];
 						}
-						$router->handle($url["path"], $req, $res, null, $onerror);
+						$router->handle($url['path'], $req, $res, null, $onerror);
 						$handled = $handled || $router->handled;
 					}
 				}
@@ -140,7 +147,7 @@
 				// Base router for non-prefixed paths
 				$this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self, $url){
 					if(!$handled){
-						$self->onerror($req, $res, new Exception("{$url['scheme']}://{$url['host']}{$url["path"]} Not Found", 404));
+						$self->onerror($req, $res, new \Exception("{$url['scheme']}://{$url['host']}{$url["path"]} Not Found", 404));
 					}
 				}, $onerror);
 				$this->fire('afterhandle', $req, $res);
@@ -213,7 +220,7 @@
 				if(is_callable($fn)){
 					$fn($req, $res, $error);
 				}
-			}catch(Exception $e){
+			}catch(\Exception $e){
 				die("Error handlers failed {$e}");
 			}
 		}
@@ -228,14 +235,18 @@
 		App::shutdown_error($error);
 	});
 	set_error_handler(function($errno, $errstr, $errfile, $errline){
-		App::shutdown_error(App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace()));
-	}, E_ALL & ~E_WARNING);
+		// ignore warnings
+		if($errno &~ E_WARNING){
+			App::shutdown_error(new Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace()));
+			die();
+		}
+	}, E_ALL);
 	register_shutdown_function(function(){
-		error_reporting(E_ALL & ~E_WARNING);
-		ini_set('display_errors', 'On');
+		error_reporting(E_ALL &~ E_WARNING);
+		ini_set('display_errors', 'Off');
 		App::shutdown();
 	});
-	error_reporting(E_ALL & ~E_WARNING);
-	ini_set('display_errors', 'On');
+	error_reporting(E_ALL &~ E_WARNING);
+	ini_set('display_errors', 'Off');
 	gc_enable();
 ?>