Skip to content

Commit 0fb4934

Browse files
committed
feat(serverHandler): provide main error information
make `HasNotFoundError` and `HasInternalError` accessible for template, so that it could show hints for end users.
1 parent 5162607 commit 0fb4934

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

src/serverHandler/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func (h *handler) json(w http.ResponseWriter, r *http.Request, data *responseDat
5454
header.Set("Content-Type", "application/json; charset=utf-8")
5555
header.Set("Cache-Control", "public, max-age=0")
5656

57-
if data.hasInternalError {
57+
if data.HasInternalError {
5858
w.WriteHeader(http.StatusInternalServerError)
59-
} else if data.hasNotFoundError {
59+
} else if data.HasNotFoundError {
6060
w.WriteHeader(http.StatusNotFound)
6161
} else {
6262
w.WriteHeader(http.StatusOK)

src/serverHandler/page.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseDat
77
header.Set("Content-Type", "text/html; charset=utf-8")
88
header.Set("Cache-Control", "public, max-age=0")
99

10-
if data.hasInternalError {
10+
if data.HasInternalError {
1111
w.WriteHeader(http.StatusInternalServerError)
12-
} else if data.hasNotFoundError {
12+
} else if data.HasNotFoundError {
1313
w.WriteHeader(http.StatusNotFound)
1414
} else {
1515
w.WriteHeader(http.StatusOK)

src/serverHandler/responseData.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ type responseData struct {
4040
rawReqPath string
4141
handlerReqPath string
4242

43-
hasNotFoundError bool
44-
hasInternalError bool
4543
errors []error
44+
HasNotFoundError bool
45+
HasInternalError bool
4646

4747
IsRoot bool
4848
Path string
@@ -265,9 +265,9 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
265265

266266
rawReqPath := util.CleanUrlPath(requestUri)
267267
reqPath := util.CleanUrlPath(rawReqPath[len(h.urlPrefix):]) // strip url prefix path
268+
errs := []error{}
268269
notFound := false
269270
internalError := false
270-
errs := []error{}
271271

272272
isRoot := rawReqPath == "/"
273273

@@ -321,9 +321,9 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
321321
rawReqPath: rawReqPath,
322322
handlerReqPath: reqPath,
323323

324-
hasNotFoundError: notFound,
325-
hasInternalError: internalError,
326324
errors: errs,
325+
HasNotFoundError: notFound,
326+
HasInternalError: internalError,
327327

328328
IsRoot: isRoot,
329329
Path: rawReqPath,

src/tpl/assert/main.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ em {
213213
float: right;
214214
}
215215

216+
.error {
217+
margin: 1em;
218+
padding: 1em;
219+
background: #ffc;
220+
}
221+
216222
@media only screen and (max-width: 350px) {
217223
.item-list .time {
218224
display: none;

src/tpl/assert/main.css.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ white-space: nowrap;
184184
overflow: hidden;
185185
float: right;
186186
}
187+
.error {
188+
margin: 1em;
189+
padding: 1em;
190+
background: #ffc;
191+
}
187192
@media only screen and (max-width: 350px) {
188193
.item-list .time {
189194
display: none;

src/tpl/page.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
{{end}}{{end}}
5757
</ul>
5858

59+
{{if .HasNotFoundError}}<div class="error">resource not found</div>{{end}}
60+
{{if .HasInternalError}}<div class="error">potential issue occurred</div>{{end}}
61+
5962
<script type="text/javascript" src="{{.RootRelPath}}/../assert/main.js"></script>
6063
</body>
6164
</html>

src/tpl/page.html.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const pageTplStr = `
6060
</li>
6161
{{end}}{{end}}
6262
</ul>
63+
{{if .HasNotFoundError}}<div class="error">resource not found</div>{{end}}
64+
{{if .HasInternalError}}<div class="error">potential issue occurred</div>{{end}}
6365
<script type="text/javascript" src="{{.RootRelPath}}?assert=main.js"></script>
6466
</body>
6567
</html>

0 commit comments

Comments
 (0)