/
error.go
executable file
·46 lines (40 loc) · 1.16 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2013 Toby<quflylong@qq.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"net/http"
"fmt"
"errors"
)
var(
AuthError = errors.New("Auth Faild")
PathAuthError = errors.New("Path Auth Faild")
)
func reportError(w http.ResponseWriter, e error, p string) {
w.Header().Set("Content-Type", "text/html;charset=utf-8")
data := make(map[string] string)
data["title"] = "访问出错!"
data["head"] = "<strong>当前位置:</strong> " + getPathLink(p)
var text string
switch {
case os.IsNotExist(e) :
text = "您访问的文件不存在!"
w.WriteHeader(404)
case os.IsPermission(e) :
text = "当前目录或文件不允许访问!"
w.WriteHeader(403)
case PathAuthError == e :
text = "您没有当前路径的访问权限!"
w.WriteHeader(403)
case AuthError == e :
text = "认证失败!"
w.Header().Set("WWW-Authenticate", "Basic realm=\"Please login\"")
w.WriteHeader(401)
default :
text = "未知错误!"
}
data["content"] = fmt.Sprintf("<i><%s></i>", text)
render(w, data)
}