2013-09-09 16:00:11 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
2013-09-11 09:00:39 +00:00
|
|
|
"strconv"
|
2013-09-09 16:00:11 +00:00
|
|
|
)
|
|
|
|
|
2013-09-11 08:31:18 +00:00
|
|
|
var (
|
|
|
|
AppName string
|
|
|
|
VERSION string
|
|
|
|
)
|
2013-09-09 16:00:11 +00:00
|
|
|
var tpl = `
|
2013-12-06 05:44:54 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
2013-09-09 16:00:11 +00:00
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
|
|
|
<title>beego application error</title>
|
|
|
|
<style>
|
|
|
|
html, body, body * {padding: 0; margin: 0;}
|
|
|
|
#header {background:#ffd; border-bottom:solid 2px #A31515; padding: 20px 10px;}
|
|
|
|
#header h2{ }
|
|
|
|
#footer {border-top:solid 1px #aaa; padding: 5px 10px; font-size: 12px; color:green;}
|
|
|
|
#content {padding: 5px;}
|
|
|
|
#content .stack b{ font-size: 13px; color: red;}
|
|
|
|
#content .stack pre{padding-left: 10px;}
|
|
|
|
table {}
|
|
|
|
td.t {text-align: right; padding-right: 5px; color: #888;}
|
2013-12-06 05:44:54 +00:00
|
|
|
</style>
|
2013-09-09 16:00:11 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
</script>
|
2013-12-06 05:44:54 +00:00
|
|
|
</head>
|
2013-09-09 16:00:11 +00:00
|
|
|
<body>
|
|
|
|
<div id="header">
|
|
|
|
<h2>{{.AppError}}</h2>
|
|
|
|
</div>
|
|
|
|
<div id="content">
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td class="t">Request Method: </td><td>{{.RequestMethod}}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td class="t">Request URL: </td><td>{{.RequestURL}}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td class="t">RemoteAddr: </td><td>{{.RemoteAddr }}</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<div class="stack">
|
|
|
|
<b>Stack</b>
|
|
|
|
<pre>{{.Stack}}</pre>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="footer">
|
|
|
|
<p>beego {{ .BeegoVersion }} (beego framework)</p>
|
|
|
|
<p>golang version: {{.GoVersion}}</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
2013-12-06 05:44:54 +00:00
|
|
|
</html>
|
2013-09-09 16:00:11 +00:00
|
|
|
`
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// render default application error page with error and stack string.
|
2013-09-09 16:00:11 +00:00
|
|
|
func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) {
|
|
|
|
t, _ := template.New("beegoerrortemp").Parse(tpl)
|
|
|
|
data := make(map[string]string)
|
|
|
|
data["AppError"] = AppName + ":" + fmt.Sprint(err)
|
|
|
|
data["RequestMethod"] = r.Method
|
|
|
|
data["RequestURL"] = r.RequestURI
|
|
|
|
data["RemoteAddr"] = r.RemoteAddr
|
|
|
|
data["Stack"] = Stack
|
|
|
|
data["BeegoVersion"] = VERSION
|
|
|
|
data["GoVersion"] = runtime.Version()
|
2014-01-05 07:43:48 +00:00
|
|
|
rw.WriteHeader(500)
|
2013-09-09 16:00:11 +00:00
|
|
|
t.Execute(rw, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errtpl = `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
|
|
<title>{{.Title}}</title>
|
|
|
|
<style type="text/css">
|
|
|
|
* {
|
|
|
|
margin:0;
|
|
|
|
padding:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
body {
|
|
|
|
background-color:#EFEFEF;
|
|
|
|
font: .9em "Lucida Sans Unicode", "Lucida Grande", sans-serif;
|
|
|
|
}
|
|
|
|
|
|
|
|
#wrapper{
|
|
|
|
width:600px;
|
|
|
|
margin:40px auto 0;
|
|
|
|
text-align:center;
|
|
|
|
-moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
|
|
|
|
-webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
|
|
|
|
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#wrapper h1{
|
|
|
|
color:#FFF;
|
|
|
|
text-align:center;
|
|
|
|
margin-bottom:20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
#wrapper a{
|
|
|
|
display:block;
|
|
|
|
font-size:.9em;
|
|
|
|
padding-top:20px;
|
|
|
|
color:#FFF;
|
|
|
|
text-decoration:none;
|
|
|
|
text-align:center;
|
|
|
|
}
|
|
|
|
|
|
|
|
#container {
|
|
|
|
width:600px;
|
|
|
|
padding-bottom:15px;
|
|
|
|
background-color:#FFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
.navtop{
|
|
|
|
height:40px;
|
|
|
|
background-color:#24B2EB;
|
|
|
|
padding:13px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.content {
|
|
|
|
padding:10px 10px 25px;
|
|
|
|
background: #FFFFFF;
|
|
|
|
margin:;
|
|
|
|
color:#333;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.button{
|
|
|
|
color:white;
|
|
|
|
padding:15px 20px;
|
|
|
|
text-shadow:1px 1px 0 #00A5FF;
|
|
|
|
font-weight:bold;
|
|
|
|
text-align:center;
|
|
|
|
border:1px solid #24B2EB;
|
|
|
|
margin:0px 200px;
|
|
|
|
clear:both;
|
|
|
|
background-color: #24B2EB;
|
|
|
|
border-radius:100px;
|
|
|
|
-moz-border-radius:100px;
|
|
|
|
-webkit-border-radius:100px;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.button:hover{
|
|
|
|
text-decoration:none;
|
|
|
|
background-color: #24B2EB;
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="wrapper">
|
|
|
|
<div id="container">
|
|
|
|
<div class="navtop">
|
|
|
|
<h1>{{.Title}}</h1>
|
|
|
|
</div>
|
|
|
|
<div id="content">
|
|
|
|
{{.Content}}
|
|
|
|
<a href="/" title="Home" class="button">Go Home</a><br />
|
|
|
|
|
2014-02-10 04:55:53 +00:00
|
|
|
<br>Powered by beego {{.BeegoVersion}}
|
2013-09-09 16:00:11 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// map of http handlers for each error string.
|
2013-09-09 16:00:11 +00:00
|
|
|
var ErrorMaps map[string]http.HandlerFunc
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ErrorMaps = make(map[string]http.HandlerFunc)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show 404 notfound error.
|
2013-09-09 16:00:11 +00:00
|
|
|
func NotFound(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["Title"] = "Page Not Found"
|
2014-01-02 14:53:09 +00:00
|
|
|
data["Content"] = template.HTML("<br>The page you have requested has flown the coop." +
|
2013-09-09 16:00:11 +00:00
|
|
|
"<br>Perhaps you are here because:" +
|
2014-01-02 14:54:15 +00:00
|
|
|
"<br><br><ul>" +
|
2013-09-09 16:00:11 +00:00
|
|
|
"<br>The page has moved" +
|
|
|
|
"<br>The page no longer exists" +
|
|
|
|
"<br>You were looking for your puppy and got lost" +
|
|
|
|
"<br>You like 404 pages" +
|
|
|
|
"</ul>")
|
|
|
|
data["BeegoVersion"] = VERSION
|
2013-12-18 12:53:23 +00:00
|
|
|
//rw.WriteHeader(http.StatusNotFound)
|
2013-09-09 16:00:11 +00:00
|
|
|
t.Execute(rw, data)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show 401 unauthorized error.
|
2013-09-09 16:00:11 +00:00
|
|
|
func Unauthorized(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["Title"] = "Unauthorized"
|
2014-01-02 14:53:09 +00:00
|
|
|
data["Content"] = template.HTML("<br>The page you have requested can't be authorized." +
|
2013-09-09 16:00:11 +00:00
|
|
|
"<br>Perhaps you are here because:" +
|
|
|
|
"<br><br><ul>" +
|
2014-01-02 14:53:09 +00:00
|
|
|
"<br>The credentials you supplied are incorrect" +
|
|
|
|
"<br>There are errors in the website address" +
|
2013-09-09 16:00:11 +00:00
|
|
|
"</ul>")
|
|
|
|
data["BeegoVersion"] = VERSION
|
2013-12-18 12:53:23 +00:00
|
|
|
//rw.WriteHeader(http.StatusUnauthorized)
|
2013-09-09 16:00:11 +00:00
|
|
|
t.Execute(rw, data)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show 403 forbidden error.
|
2013-09-09 16:00:11 +00:00
|
|
|
func Forbidden(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["Title"] = "Forbidden"
|
2014-01-02 14:53:09 +00:00
|
|
|
data["Content"] = template.HTML("<br>The page you have requested is forbidden." +
|
2013-09-09 16:00:11 +00:00
|
|
|
"<br>Perhaps you are here because:" +
|
|
|
|
"<br><br><ul>" +
|
|
|
|
"<br>Your address may be blocked" +
|
|
|
|
"<br>The site may be disabled" +
|
|
|
|
"<br>You need to log in" +
|
|
|
|
"</ul>")
|
|
|
|
data["BeegoVersion"] = VERSION
|
2013-12-18 12:53:23 +00:00
|
|
|
//rw.WriteHeader(http.StatusForbidden)
|
2013-09-09 16:00:11 +00:00
|
|
|
t.Execute(rw, data)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show 503 service unavailable error.
|
2013-09-09 16:00:11 +00:00
|
|
|
func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["Title"] = "Service Unavailable"
|
2014-01-02 14:53:09 +00:00
|
|
|
data["Content"] = template.HTML("<br>The page you have requested is unavailable." +
|
2013-09-09 16:00:11 +00:00
|
|
|
"<br>Perhaps you are here because:" +
|
|
|
|
"<br><br><ul>" +
|
|
|
|
"<br><br>The page is overloaded" +
|
|
|
|
"<br>Please try again later." +
|
|
|
|
"</ul>")
|
|
|
|
data["BeegoVersion"] = VERSION
|
2013-12-18 12:53:23 +00:00
|
|
|
//rw.WriteHeader(http.StatusServiceUnavailable)
|
2013-09-09 16:00:11 +00:00
|
|
|
t.Execute(rw, data)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show 500 internal server error.
|
2013-09-09 16:00:11 +00:00
|
|
|
func InternalServerError(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["Title"] = "Internal Server Error"
|
2014-01-02 14:53:09 +00:00
|
|
|
data["Content"] = template.HTML("<br>The page you have requested is down right now." +
|
2013-09-09 16:00:11 +00:00
|
|
|
"<br><br><ul>" +
|
2014-01-02 14:53:09 +00:00
|
|
|
"<br>Please try again later and report the error to the website administrator" +
|
|
|
|
"<br></ul>")
|
2013-09-09 16:00:11 +00:00
|
|
|
data["BeegoVersion"] = VERSION
|
2013-12-18 12:53:23 +00:00
|
|
|
//rw.WriteHeader(http.StatusInternalServerError)
|
2013-09-09 16:00:11 +00:00
|
|
|
t.Execute(rw, data)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show 500 internal error with simple text string.
|
2013-12-06 05:44:54 +00:00
|
|
|
func SimpleServerError(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// add http handler for given error string.
|
2013-09-11 08:23:41 +00:00
|
|
|
func Errorhandler(err string, h http.HandlerFunc) {
|
|
|
|
ErrorMaps[err] = h
|
|
|
|
}
|
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// register default error http handlers, 404,401,403,500 and 503.
|
|
|
|
func RegisterErrorHandler() {
|
2013-09-09 16:00:11 +00:00
|
|
|
if _, ok := ErrorMaps["404"]; !ok {
|
|
|
|
ErrorMaps["404"] = NotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ErrorMaps["401"]; !ok {
|
|
|
|
ErrorMaps["401"] = Unauthorized
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ErrorMaps["403"]; !ok {
|
|
|
|
ErrorMaps["403"] = Forbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ErrorMaps["503"]; !ok {
|
|
|
|
ErrorMaps["503"] = ServiceUnavailable
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ErrorMaps["500"]; !ok {
|
|
|
|
ErrorMaps["500"] = InternalServerError
|
|
|
|
}
|
|
|
|
}
|
2013-09-11 09:00:39 +00:00
|
|
|
|
2014-01-17 08:03:01 +00:00
|
|
|
// show error string as simple text message.
|
|
|
|
// if error string is empty, show 500 error as default.
|
2013-09-11 09:00:39 +00:00
|
|
|
func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) {
|
|
|
|
if h, ok := ErrorMaps[errcode]; ok {
|
2013-12-18 13:00:45 +00:00
|
|
|
isint, err := strconv.Atoi(errcode)
|
|
|
|
if err != nil {
|
|
|
|
isint = 500
|
|
|
|
}
|
|
|
|
w.WriteHeader(isint)
|
2013-09-11 09:00:39 +00:00
|
|
|
h(w, r)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
isint, err := strconv.Atoi(errcode)
|
|
|
|
if err != nil {
|
|
|
|
isint = 500
|
|
|
|
}
|
|
|
|
if isint == 400 {
|
|
|
|
msg = "404 page not found"
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
w.WriteHeader(isint)
|
|
|
|
fmt.Fprintln(w, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|