Skip to content

Commit

Permalink
feat: write error on template execution error
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Nov 22, 2023
1 parent af84598 commit 5bfcf4f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion gateway/assets/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ func runTemplate(w http.ResponseWriter, filename string, data interface{}) {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
_ = tpl.Execute(w, data)
err = tpl.Execute(w, data)
if err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))
}
}

func main() {
Expand Down
5 changes: 4 additions & 1 deletion gateway/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,17 @@ func webError(w http.ResponseWriter, r *http.Request, c *Config, err error, defa
if acceptsHTML {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(code)
_ = assets.ErrorTemplate.Execute(w, assets.ErrorTemplateData{
err = assets.ErrorTemplate.Execute(w, assets.ErrorTemplateData{
GlobalData: assets.GlobalData{
Menu: c.Menu,
},
StatusCode: code,
StatusText: http.StatusText(code),
Error: err.Error(),
})
if err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))
}

Check warning on line 178 in gateway/errors.go

View check run for this annotation

Codecov / codecov/patch

gateway/errors.go#L177-L178

Added lines #L177 - L178 were not covered by tests
} else {
http.Error(w, err.Error(), code)
}
Expand Down
5 changes: 4 additions & 1 deletion gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,11 +912,14 @@ func (i *handler) handleSuperfluousNamespace(w http.ResponseWriter, r *http.Requ
// - redirects to intendedURL after a short delay

w.WriteHeader(http.StatusBadRequest)
_ = redirectTemplate.Execute(w, redirectTemplateData{
err = redirectTemplate.Execute(w, redirectTemplateData{

Check warning on line 915 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L915

Added line #L915 was not covered by tests
RedirectURL: intendedURL,
SuggestedPath: intendedPath.String(),
ErrorMsg: fmt.Sprintf("invalid path: %q should be %q", r.URL.Path, intendedPath.String()),
})
if err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))

Check warning on line 921 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L919-L921

Added lines #L919 - L921 were not covered by tests
}

return true
}
Expand Down
3 changes: 3 additions & 0 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *
CodecHex: fmt.Sprintf("0x%x", uint64(cidCodec)),
Node: parseNode(blockCid, blockData),
})
if err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))

Check warning on line 207 in gateway/handler_codec.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_codec.go#L207

Added line #L207 was not covered by tests
}

return err == nil
}
Expand Down
1 change: 1 addition & 0 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
rq.logger.Debugw("request processed", "tplDataDNSLink", globalData.DNSLink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash)

if err := assets.DirectoryTemplate.Execute(w, tplData); err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))
return false
}

Expand Down

0 comments on commit 5bfcf4f

Please sign in to comment.