@@ -36,7 +36,7 @@ import (
3636var templateMap map [int ]* template.Template
3737
3838//parameters needed for formatting the correct HTML page
39- type ErrorParams struct {
39+ type ResponseParams struct {
4040 Msg string
4141 Code int
4242 Timestamp string
@@ -75,45 +75,44 @@ func initErrHandling() {
7575//For example, if the user requests bzz:/<hash>/read and that manifest contains entries
7676//"readme.md" and "readinglist.txt", a HTML page is returned with this two links.
7777//This only applies if the manifest has no default entry
78- func ShowMultipleChoices (w http.ResponseWriter , r * http. Request , list api.ManifestList ) {
78+ func ShowMultipleChoices (w http.ResponseWriter , req * Request , list api.ManifestList ) {
7979 msg := ""
8080 if list .Entries == nil {
81- ShowError (w , r , "Internal Server Error" , http .StatusInternalServerError )
81+ Respond (w , req , "Internal Server Error" , http .StatusInternalServerError )
8282 return
8383 }
8484 //make links relative
8585 //requestURI comes with the prefix of the ambiguous path, e.g. "read" for "readme.md" and "readinglist.txt"
8686 //to get clickable links, need to remove the ambiguous path, i.e. "read"
87- idx := strings .LastIndex (r .RequestURI , "/" )
87+ idx := strings .LastIndex (req .RequestURI , "/" )
8888 if idx == - 1 {
89- ShowError (w , r , "Internal Server Error" , http .StatusInternalServerError )
89+ Respond (w , req , "Internal Server Error" , http .StatusInternalServerError )
9090 return
9191 }
9292 //remove ambiguous part
93- base := r .RequestURI [:idx + 1 ]
93+ base := req .RequestURI [:idx + 1 ]
9494 for _ , e := range list .Entries {
9595 //create clickable link for each entry
9696 msg += "<a href='" + base + e .Path + "'>" + e .Path + "</a><br/>"
9797 }
98- respond (w , r , & ErrorParams {
99- Code : http .StatusMultipleChoices ,
100- Details : template .HTML (msg ),
101- Timestamp : time .Now ().Format (time .RFC1123 ),
102- template : getTemplate (http .StatusMultipleChoices ),
103- })
98+
99+ Respond (w , req , msg , http .StatusMultipleChoices )
104100}
105101
106- //ShowError is used to show an HTML error page to a client.
102+ //Respond is used to show an HTML page to a client.
107103//If there is an `Accept` header of `application/json`, JSON will be returned instead
108104//The function just takes a string message which will be displayed in the error page.
109105//The code is used to evaluate which template will be displayed
110106//(and return the correct HTTP status code)
111- func ShowError (w http.ResponseWriter , r * http.Request , msg string , code int ) {
112- if code == http .StatusInternalServerError {
113- //log.Error(msg)
114- log .Output (msg , log .LvlError , 3 )
107+ func Respond (w http.ResponseWriter , req * Request , msg string , code int ) {
108+ switch code {
109+ case http .StatusInternalServerError :
110+ log .Output (msg , log .LvlError , 3 , "ruid" , req .ruid , "code" , code )
111+ default :
112+ log .Output (msg , log .LvlDebug , 3 , "ruid" , req .ruid , "code" , code )
115113 }
116- respond (w , r , & ErrorParams {
114+
115+ respond (w , & req .Request , & ResponseParams {
117116 Code : code ,
118117 Msg : msg ,
119118 Timestamp : time .Now ().Format (time .RFC1123 ),
@@ -122,7 +121,7 @@ func ShowError(w http.ResponseWriter, r *http.Request, msg string, code int) {
122121}
123122
124123//evaluate if client accepts html or json response
125- func respond (w http.ResponseWriter , r * http.Request , params * ErrorParams ) {
124+ func respond (w http.ResponseWriter , r * http.Request , params * ResponseParams ) {
126125 w .WriteHeader (params .Code )
127126 if r .Header .Get ("Accept" ) == "application/json" {
128127 respondJson (w , params )
@@ -132,15 +131,15 @@ func respond(w http.ResponseWriter, r *http.Request, params *ErrorParams) {
132131}
133132
134133//return a HTML page
135- func respondHtml (w http.ResponseWriter , params * ErrorParams ) {
134+ func respondHtml (w http.ResponseWriter , params * ResponseParams ) {
136135 err := params .template .Execute (w , params )
137136 if err != nil {
138137 log .Error (err .Error ())
139138 }
140139}
141140
142141//return JSON
143- func respondJson (w http.ResponseWriter , params * ErrorParams ) {
142+ func respondJson (w http.ResponseWriter , params * ResponseParams ) {
144143 w .Header ().Set ("Content-Type" , "application/json" )
145144 json .NewEncoder (w ).Encode (params )
146145}
0 commit comments