Browse Source

Better AJAXy server error handling

Alan Hardman 3 years ago
parent
commit
8fe80f7e9e
2 changed files with 29 additions and 16 deletions
  1. 4 1
      app/view/issues/single.html
  2. 25 15
      index.php

+ 4 - 1
app/view/issues/single.html

@@ -419,7 +419,10 @@ $(function() {
 						'</div>');
 				$('#comment_textarea').val('').html('');
 				$('#comment_badge').text(parseInt($('#comment_badge').text()) + 1);
-			}, 'json');
+			}, 'json').fail(function() {
+				$('#comment_form .btn').prop('disabled', false);
+				alert('An error occurred saving this comment.');
+			});
 
 			$this.prop('disabled', true);
 		}

+ 25 - 15
index.php

@@ -40,21 +40,31 @@ $f3->config("app/routes.ini");
 
 // Set up error handling
 $f3->set("ONERROR", function(Base $f3) {
-	switch($f3->get("ERROR.code")) {
-		case 404:
-			$f3->set("title", "Not Found");
-			$f3->set("ESCAPE", false);
-			echo \Helper\View::instance()->render("error/404.html");
-			break;
-		case 403:
-			echo "You do not have access to this page.";
-			break;
-		default:
-			if(ob_get_level()) {
-				include "app/view/error/inline.html";
-			} else {
-				include "app/view/error/500.html";
-			}
+	if($f3->get("AJAX")) {
+		if(!headers_sent()) {
+			header("Content-type: application/json");
+		}
+		echo json_encode(array(
+			"error" => $f3->get("ERROR.title"),
+			"text" => $f3->get("ERROR.text")
+		));
+	} else {
+		switch($f3->get("ERROR.code")) {
+			case 404:
+				$f3->set("title", "Not Found");
+				$f3->set("ESCAPE", false);
+				echo \Helper\View::instance()->render("error/404.html");
+				break;
+			case 403:
+				echo "You do not have access to this page.";
+				break;
+			default:
+				if(ob_get_level()) {
+					include "app/view/error/inline.html";
+				} else {
+					include "app/view/error/500.html";
+				}
+		}
 	}
 });