Skip to content

Commit b8b94d6

Browse files
committed
feat(serverHandler): render error status page rather than empty page
1 parent 22a9637 commit b8b94d6

File tree

7 files changed

+73
-67
lines changed

7 files changed

+73
-67
lines changed

src/i18n/translation.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ type Translation struct {
3333
DeleteLabel string
3434
DeleteConfirm string
3535

36-
Error401 string
37-
Error403 string
38-
Error404 string
39-
Error500 string
36+
Error401 string
37+
Error403 string
38+
Error404 string
39+
ErrorStatus string
4040
}
4141

4242
var translationEnUs = Translation{
@@ -72,10 +72,10 @@ var translationEnUs = Translation{
7272
DeleteLabel: "Delete",
7373
DeleteConfirm: "Confirm delete?",
7474

75-
Error401: "401 unauthorized",
76-
Error403: "403 resource is forbidden",
77-
Error404: "404 resource not found",
78-
Error500: "500 potential issue occurred",
75+
Error401: "unauthorized",
76+
Error403: "resource is forbidden",
77+
Error404: "resource not found",
78+
ErrorStatus: "potential issue occurred",
7979
}
8080

8181
var translationZhSimp = Translation{
@@ -111,10 +111,10 @@ var translationZhSimp = Translation{
111111
DeleteLabel: "删除",
112112
DeleteConfirm: "确认删除吗?",
113113

114-
Error401: "401 未授权",
115-
Error403: "403 禁止访问资源",
116-
Error404: "404 资源不存在",
117-
Error500: "500 发生潜在错误",
114+
Error401: "未授权",
115+
Error403: "禁止访问资源",
116+
Error404: "资源不存在",
117+
ErrorStatus: "发生潜在错误",
118118
}
119119

120120
var translationZhTrad = Translation{
@@ -150,8 +150,8 @@ var translationZhTrad = Translation{
150150
DeleteLabel: "刪除",
151151
DeleteConfirm: "確認刪除嗎?",
152152

153-
Error401: "401 未授權",
154-
Error403: "403 禁止訪問資源",
155-
Error404: "404 資源不存在",
156-
Error500: "500 發生潛在錯誤",
153+
Error401: "未授權",
154+
Error403: "禁止訪問資源",
155+
Error404: "資源不存在",
156+
ErrorStatus: "發生潛在錯誤",
157157
}

src/serverHandler/aliasHandler.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,22 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
118118

119119
header(w, session.headers)
120120

121-
if data.IsMutate {
122-
h.mutate(w, r, session, data)
121+
if data.IsMutate && h.mutate(w, r, session, data) {
123122
return
124-
}
125-
126-
// archive
127-
if len(r.URL.RawQuery) >= 3 {
123+
} else if len(r.URL.RawQuery) >= 3 {
128124
switch r.URL.RawQuery[:3] {
129125
case "tar":
130-
h.tar(w, r, session, data)
131-
return
126+
if h.tar(w, r, session, data) {
127+
return
128+
}
132129
case "tgz":
133-
h.tgz(w, r, session, data)
134-
return
130+
if h.tgz(w, r, session, data) {
131+
return
132+
}
135133
case "zip":
136-
h.zip(w, r, session, data)
137-
return
134+
if h.zip(w, r, session, data) {
135+
return
136+
}
138137
}
139138
}
140139
}

src/serverHandler/archiveTar.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ func writeTar(tw *tar.Writer, f *os.File, fInfo os.FileInfo, archivePath string)
5252
return nil
5353
}
5454

55-
func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
55+
func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) bool {
5656
if !data.CanArchive {
57-
w.WriteHeader(http.StatusBadRequest)
58-
return
57+
data.Status = http.StatusBadRequest
58+
return false
5959
}
6060

6161
selections, ok := h.normalizeArchiveSelections(r)
6262
if !ok {
63-
return
63+
data.Status = http.StatusBadRequest
64+
return false
6465
}
6566

6667
tw := tar.NewWriter(w)
@@ -81,22 +82,25 @@ func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, session *sess
8182
return writeTar(tw, f, fInfo, relPath)
8283
},
8384
)
85+
return true
8486
}
8587

86-
func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
88+
func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) bool {
8789
if !data.CanArchive {
88-
w.WriteHeader(http.StatusBadRequest)
89-
return
90+
data.Status = http.StatusBadRequest
91+
return false
9092
}
9193

9294
selections, ok := h.normalizeArchiveSelections(r)
9395
if !ok {
94-
return
96+
data.Status = http.StatusBadRequest
97+
return false
9598
}
9699

97100
gzw, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
98101
if h.logError(err) {
99-
return
102+
data.Status = http.StatusInternalServerError
103+
return false
100104
}
101105
defer func() {
102106
err := gzw.Close()
@@ -121,4 +125,5 @@ func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, session *sess
121125
return writeTar(tw, f, fInfo, relPath)
122126
},
123127
)
128+
return true
124129
}

src/serverHandler/archiveZip.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ func writeZip(zw *zip.Writer, f *os.File, fInfo os.FileInfo, archivePath string)
3737
return nil
3838
}
3939

40-
func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
40+
func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) bool {
4141
if !data.CanArchive {
42-
w.WriteHeader(http.StatusBadRequest)
43-
return
42+
data.Status = http.StatusBadRequest
43+
return false
4444
}
4545

4646
selections, ok := h.normalizeArchiveSelections(r)
4747
if !ok {
48-
return
48+
data.Status = http.StatusBadRequest
49+
return false
4950
}
5051

5152
zipWriter := zip.NewWriter(w)
@@ -66,4 +67,5 @@ func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, session *sess
6667
return writeZip(zipWriter, f, fInfo, relPath)
6768
},
6869
)
70+
return true
6971
}

src/serverHandler/mutate.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,52 @@ import (
44
"net/http"
55
)
66

7-
func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
7+
func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) (ok bool) {
88
if r.Method != http.MethodPost {
9-
w.WriteHeader(http.StatusMethodNotAllowed)
9+
data.Status = http.StatusMethodNotAllowed
1010
return
1111
}
1212

13-
success := false
14-
1513
switch {
1614
case data.IsUpload:
1715
if data.CanUpload {
18-
success = h.saveUploadFiles(data.AuthUserName, h.fs+session.aliasReqPath, data.CanMkdir, data.CanDelete, data.AliasSubItems, r)
16+
ok = h.saveUploadFiles(data.AuthUserName, h.fs+session.aliasReqPath, data.CanMkdir, data.CanDelete, data.AliasSubItems, r)
1917
} else {
20-
w.WriteHeader(http.StatusBadRequest)
18+
data.Status = http.StatusBadRequest
2119
return
2220
}
2321
case data.IsMkdir:
2422
if data.CanMkdir && !h.logError(r.ParseForm()) {
25-
success = h.mkdirs(data.AuthUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
23+
ok = h.mkdirs(data.AuthUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
2624
} else {
27-
w.WriteHeader(http.StatusBadRequest)
25+
data.Status = http.StatusBadRequest
2826
return
2927
}
3028
case data.IsDelete:
3129
if data.CanDelete && !h.logError(r.ParseForm()) {
32-
success = h.deleteItems(data.AuthUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
30+
ok = h.deleteItems(data.AuthUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
3331
} else {
34-
w.WriteHeader(http.StatusBadRequest)
32+
data.Status = http.StatusBadRequest
3533
return
3634
}
3735
}
3836

3937
if session.wantJson {
4038
header := w.Header()
4139
header.Set("Content-Type", "application/json; charset=utf-8")
42-
header.Set("Cache-Control", "public, max-age=0")
4340

44-
if success {
41+
if ok {
4542
w.WriteHeader(http.StatusOK)
4643
w.Write([]byte(`{"success":true}`))
4744
} else {
4845
w.WriteHeader(http.StatusInternalServerError)
4946
w.Write([]byte(`{"success":false}`))
5047
}
51-
} else {
52-
reqPath := session.prefixReqPath
48+
return true
49+
}
5350

51+
if ok {
52+
reqPath := session.prefixReqPath
5453
ctxQsList := r.Form["contextquerystring"]
5554
ctxQsListLen := len(ctxQsList)
5655
if ctxQsListLen > 0 {
@@ -59,11 +58,10 @@ func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, session *s
5958
reqPath += ctxQs
6059
}
6160
}
62-
63-
if success {
64-
http.Redirect(w, r, reqPath, http.StatusFound)
65-
} else {
66-
w.WriteHeader(http.StatusInternalServerError)
67-
}
61+
http.Redirect(w, r, reqPath, http.StatusFound)
62+
return
6863
}
64+
65+
data.Status = http.StatusInternalServerError
66+
return
6967
}

src/tpl/defaultTheme/frontend/index.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@
128128
{{end}}
129129
</ul>
130130

131+
{{if ne .Status 200}}<div class="error">{{.Status}}
131132
{{if eq .Status 401}}
132-
<div class="error">{{.Trans.Error401}}</div>
133+
{{.Trans.Error401}}
133134
{{else if eq .Status 403}}
134-
<div class="error">{{.Trans.Error403}}</div>
135+
{{.Trans.Error403}}
135136
{{else if eq .Status 404}}
136-
<div class="error">{{.Trans.Error404}}</div>
137-
{{else if eq .Status 500}}
138-
<div class="error">{{.Trans.Error500}}</div>
137+
{{.Trans.Error404}}
138+
{{else}}
139+
{{.Trans.ErrorStatus}}</div>
139140
{{end}}
141+
</div>{{end}}
140142

141143
<script type="text/javascript" src="{{.RootRelPath}}?asset=index.js" defer="defer" async="async"></script>
142144
</body>

test/lib.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ curl_upload_content() {
5858
local name="$2"
5959
local value="$3"
6060
local filename="$4"
61-
curl -s -k -F "$name=$value;filename=$filename" "$url"
61+
curl -s -k -F "$name=$value;filename=$filename" "$url" > /dev/null
6262
}

0 commit comments

Comments
 (0)