Преглед на файлове

fix(error handling): fixed server side error handling page

Torkel Ödegaard преди 9 години
родител
ревизия
18e965c775
променени са 3 файла, в които са добавени 41 реда и са изтрити 62 реда
  1. 1 0
      pkg/middleware/middleware.go
  2. 24 50
      pkg/middleware/recovery.go
  3. 16 12
      public/views/500.html

+ 1 - 0
pkg/middleware/middleware.go

@@ -187,6 +187,7 @@ func (ctx *Context) Handle(status int, title string, err error) {
 	}
 	}
 
 
 	ctx.Data["Title"] = title
 	ctx.Data["Title"] = title
+	ctx.Data["AppSubUrl"] = setting.AppSubUrl
 	ctx.HTML(status, strconv.Itoa(status))
 	ctx.HTML(status, strconv.Itoa(status))
 }
 }
 
 

+ 24 - 50
pkg/middleware/recovery.go

@@ -19,53 +19,14 @@ import (
 	"bytes"
 	"bytes"
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
-	"net/http"
 	"runtime"
 	"runtime"
 
 
 	"gopkg.in/macaron.v1"
 	"gopkg.in/macaron.v1"
 
 
-	"github.com/go-macaron/inject"
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/setting"
 )
 )
 
 
-const (
-	panicHtml = `<html>
-<head><title>PANIC: %s</title>
-<meta charset="utf-8" />
-<style type="text/css">
-html, body {
-	font-family: "Roboto", sans-serif;
-	color: #333333;
-	background-color: #ea5343;
-	margin: 0px;
-}
-h1 {
-	color: #d04526;
-	background-color: #ffffff;
-	padding: 20px;
-	border-bottom: 1px dashed #2b3848;
-}
-pre {
-	margin: 20px;
-	padding: 20px;
-	border: 2px solid #2b3848;
-	background-color: #ffffff;
-	white-space: pre-wrap;       /* css-3 */
-	white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
-	white-space: -pre-wrap;      /* Opera 4-6 */
-	white-space: -o-pre-wrap;    /* Opera 7 */
-	word-wrap: break-word;       /* Internet Explorer 5.5+ */
-}
-</style>
-</head><body>
-<h1>PANIC</h1>
-<pre style="font-weight: bold;">%s</pre>
-<pre>%s</pre>
-</body>
-</html>`
-)
-
 var (
 var (
 	dunno     = []byte("???")
 	dunno     = []byte("???")
 	centerDot = []byte("·")
 	centerDot = []byte("·")
@@ -151,21 +112,34 @@ func Recovery() macaron.Handler {
 
 
 				panicLogger.Error("Request error", "error", err, "stack", string(stack))
 				panicLogger.Error("Request error", "error", err, "stack", string(stack))
 
 
-				// Lookup the current responsewriter
-				val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
-				res := val.Interface().(http.ResponseWriter)
+				c.Data["Title"] = "Server Error"
+				c.Data["AppSubUrl"] = setting.AppSubUrl
 
 
-				// respond with panic message while in development mode
-				var body []byte
-				if setting.Env == setting.DEV {
-					res.Header().Set("Content-Type", "text/html")
-					body = []byte(fmt.Sprintf(panicHtml, err, err, stack))
+				if theErr, ok := err.(error); ok {
+					c.Data["Title"] = theErr.Error()
 				}
 				}
 
 
-				res.WriteHeader(http.StatusInternalServerError)
-				if nil != body {
-					res.Write(body)
+				if setting.Env == setting.DEV {
+					c.Data["ErrorMsg"] = string(stack)
 				}
 				}
+
+				c.HTML(500, "500")
+
+				// // Lookup the current responsewriter
+				// val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
+				// res := val.Interface().(http.ResponseWriter)
+				//
+				// // respond with panic message while in development mode
+				// var body []byte
+				// if setting.Env == setting.DEV {
+				// 	res.Header().Set("Content-Type", "text/html")
+				// 	body = []byte(fmt.Sprintf(panicHtml, err, err, stack))
+				// }
+				//
+				// res.WriteHeader(http.StatusInternalServerError)
+				// if nil != body {
+				// 	res.Write(body)
+				// }
 			}
 			}
 		}()
 		}()
 
 

+ 16 - 12
public/views/500.html

@@ -5,28 +5,32 @@
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
     <meta name="viewport" content="width=device-width">
     <meta name="viewport" content="width=device-width">
 
 
-    <title>Grafana</title>
+    <title>Grafana - Error</title>
+
+		<link href='[[.AppSubUrl]]/public/css/fonts.min.css' rel='stylesheet' type='text/css'>
+
+		<link rel="stylesheet" href="[[.AppSubUrl]]/public/css/grafana.dark.min.css">
 
 
-    <link rel="stylesheet" href="[[.AppSubUrl]]/public/css/grafana.dark.min.css" title="Dark">
     <link rel="icon" type="image/png" href="[[.AppSubUrl]]/public/img/fav32.png">
     <link rel="icon" type="image/png" href="[[.AppSubUrl]]/public/img/fav32.png">
 
 
+		<base href="[[.AppSubUrl]]/" />
+
   </head>
   </head>
 
 
 	<body>
 	<body>
 
 
-		<div class="gf-box" style="margin: 200px auto 0 auto; width: 500px;">
-			<div class="gf-box-header">
-				<span class="gf-box-title">
+		<div class="page-container">
+			<div class="page-header">
+				<h1>
 					Server side error :(
 					Server side error :(
-				</span>
+				</h1>
 			</div>
 			</div>
 
 
-			<div class="gf-box-body">
-				<h4>[[.Title]]</h4>
-				[[.ErrorMsg]]
-			</div>
-		</div>
+			<h4>[[.Title]]</h4>
+
+			<pre>[[.ErrorMsg]]</pre>
 
 
-  </body>
+		</div>
+	</body>
 
 
 </html>
 </html>