Return http status code 500 for internal errors
This commit is contained in:
parent
d2d5df88f2
commit
c170298e49
@ -200,6 +200,9 @@ func (s *PublicServer) jsonHandler(handler func(r *http.Request) (interface{}, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
if _, isError := data.(jsonError); isError {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
json.NewEncoder(w).Encode(data)
|
json.NewEncoder(w).Encode(data)
|
||||||
}()
|
}()
|
||||||
data, err = handler(r)
|
data, err = handler(r)
|
||||||
@ -229,12 +232,11 @@ func (s *PublicServer) newTemplateData() *TemplateData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *PublicServer) newTemplateDataWithError(text string) *TemplateData {
|
func (s *PublicServer) newTemplateDataWithError(text string) *TemplateData {
|
||||||
return &TemplateData{
|
td := s.newTemplateData()
|
||||||
CoinName: s.is.Coin,
|
td.Error = &api.ApiError{Text: text}
|
||||||
CoinShortcut: s.is.CoinShortcut,
|
return td
|
||||||
Error: &api.ApiError{Text: text},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PublicServer) htmlTemplateHandler(handler func(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error)) func(w http.ResponseWriter, r *http.Request) {
|
func (s *PublicServer) htmlTemplateHandler(handler func(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error)) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var t tpl
|
var t tpl
|
||||||
@ -243,7 +245,7 @@ func (s *PublicServer) htmlTemplateHandler(handler func(w http.ResponseWriter, r
|
|||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
glog.Error(getFunctionName(handler), " recovered from panic: ", e)
|
glog.Error(getFunctionName(handler), " recovered from panic: ", e)
|
||||||
t = errorTpl
|
t = errorInternalTpl
|
||||||
if s.debug {
|
if s.debug {
|
||||||
data = s.newTemplateDataWithError(fmt.Sprint("Internal server error: recovered from panic ", e))
|
data = s.newTemplateDataWithError(fmt.Sprint("Internal server error: recovered from panic ", e))
|
||||||
} else {
|
} else {
|
||||||
@ -253,6 +255,10 @@ func (s *PublicServer) htmlTemplateHandler(handler func(w http.ResponseWriter, r
|
|||||||
// noTpl means the handler completely handled the request
|
// noTpl means the handler completely handled the request
|
||||||
if t != noTpl {
|
if t != noTpl {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
// return 500 Internal Server Error with errorInternalTpl
|
||||||
|
if t == errorInternalTpl {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
if err := s.templates[t].ExecuteTemplate(w, "base.html", data); err != nil {
|
if err := s.templates[t].ExecuteTemplate(w, "base.html", data); err != nil {
|
||||||
glog.Error(err)
|
glog.Error(err)
|
||||||
}
|
}
|
||||||
@ -265,10 +271,13 @@ func (s *PublicServer) htmlTemplateHandler(handler func(w http.ResponseWriter, r
|
|||||||
}
|
}
|
||||||
t, data, err = handler(w, r)
|
t, data, err = handler(w, r)
|
||||||
if err != nil || (data == nil && t != noTpl) {
|
if err != nil || (data == nil && t != noTpl) {
|
||||||
t = errorTpl
|
t = errorInternalTpl
|
||||||
if apiErr, ok := err.(*api.ApiError); ok {
|
if apiErr, ok := err.(*api.ApiError); ok {
|
||||||
data = s.newTemplateData()
|
data = s.newTemplateData()
|
||||||
data.Error = apiErr
|
data.Error = apiErr
|
||||||
|
if apiErr.Public {
|
||||||
|
t = errorTpl
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error(getFunctionName(handler), " error: ", err)
|
glog.Error(getFunctionName(handler), " error: ", err)
|
||||||
@ -288,6 +297,7 @@ type tpl int
|
|||||||
const (
|
const (
|
||||||
noTpl = tpl(iota)
|
noTpl = tpl(iota)
|
||||||
errorTpl
|
errorTpl
|
||||||
|
errorInternalTpl
|
||||||
indexTpl
|
indexTpl
|
||||||
txTpl
|
txTpl
|
||||||
addressTpl
|
addressTpl
|
||||||
@ -324,6 +334,7 @@ func parseTemplates() []*template.Template {
|
|||||||
}
|
}
|
||||||
t := make([]*template.Template, tplCount)
|
t := make([]*template.Template, tplCount)
|
||||||
t[errorTpl] = template.Must(template.New("error").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
t[errorTpl] = template.Must(template.New("error").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
||||||
|
t[errorInternalTpl] = template.Must(template.New("error").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
||||||
t[indexTpl] = template.Must(template.New("index").Funcs(templateFuncMap).ParseFiles("./static/templates/index.html", "./static/templates/base.html"))
|
t[indexTpl] = template.Must(template.New("index").Funcs(templateFuncMap).ParseFiles("./static/templates/index.html", "./static/templates/base.html"))
|
||||||
t[txTpl] = template.Must(template.New("tx").Funcs(templateFuncMap).ParseFiles("./static/templates/tx.html", "./static/templates/txdetail.html", "./static/templates/base.html"))
|
t[txTpl] = template.Must(template.New("tx").Funcs(templateFuncMap).ParseFiles("./static/templates/tx.html", "./static/templates/txdetail.html", "./static/templates/base.html"))
|
||||||
t[addressTpl] = template.Must(template.New("address").Funcs(templateFuncMap).ParseFiles("./static/templates/address.html", "./static/templates/txdetail.html", "./static/templates/paging.html", "./static/templates/base.html"))
|
t[addressTpl] = template.Must(template.New("address").Funcs(templateFuncMap).ParseFiles("./static/templates/address.html", "./static/templates/txdetail.html", "./static/templates/paging.html", "./static/templates/base.html"))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user