feat: don't send reponse on http error

This commit is contained in:
franklin 2021-05-21 15:25:09 +02:00
parent 430911ed9d
commit add9ac903a
4 changed files with 42 additions and 11 deletions

View File

@ -332,11 +332,14 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
executionStart := time.Now()
helloRet := p.service.Hello(w, r, arg_number)
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
helloRet := p.service.Hello(&rw, r, arg_number)
if callStats != nil {
callStats.Execution = time.Now().Sub(executionStart)
}
if rw.Status() == http.StatusOK {
gotsrpc.Reply([]interface{}{helloRet}, callStats, r, w)
}
return
case "Inheritance":
var (

13
go.go
View File

@ -311,19 +311,26 @@ func renderTSRPCServiceProxies(services ServiceList, fullPackageName string, pac
returnValueNames = append(returnValueNames, lcfirst(method.Name)+ucfirst(retArgName))
}
g.l("executionStart := time.Now()")
if isSessionRequest {
g.l("rw := gotsrpc.ResponseWriter{ResponseWriter: w}")
callArgs = append([]string{"&rw", "r"}, callArgs...)
}
if len(returnValueNames) > 0 {
g.app(strings.Join(returnValueNames, ", ") + " := ")
}
if isSessionRequest {
callArgs = append([]string{"w", "r"}, callArgs...)
}
g.app("p.service." + method.Name + "(" + strings.Join(callArgs, ", ") + ")")
g.nl()
g.l("if callStats != nil {")
g.ind(1).l("callStats.Execution = time.Now().Sub(executionStart)").ind(-1)
g.l("}")
if isSessionRequest {
g.l("if rw.Status() == http.StatusOK {").ind(1)
}
g.l("gotsrpc.Reply([]interface{}{" + strings.Join(returnValueNames, ", ") + "}, callStats, r, w)")
if isSessionRequest {
g.ind(-1).l("}")
}
g.l("return")
g.ind(-1)
}

View File

@ -28,18 +28,15 @@ func GetCalledFunc(r *http.Request, endPoint string) string {
}
func ErrorFuncNotFound(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("method not found"))
http.Error(w, "method not found", http.StatusNotFound)
}
func ErrorCouldNotLoadArgs(w http.ResponseWriter) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("could not load args"))
http.Error(w, "could not load args", http.StatusBadRequest)
}
func ErrorMethodNotAllowed(w http.ResponseWriter) {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("you gotta POST"))
http.Error(w, "you gotta POST", http.StatusMethodNotAllowed)
}
func LoadArgs(args interface{}, callStats *CallStats, r *http.Request) error {

24
responsewriter.go Normal file
View File

@ -0,0 +1,24 @@
package gotsrpc
import (
"net/http"
)
type ResponseWriter struct {
http.ResponseWriter
wroteHeader bool
status int
}
func (r *ResponseWriter) WriteHeader(status int) {
r.status = status
r.wroteHeader = true
r.ResponseWriter.WriteHeader(status)
}
func (r *ResponseWriter) Status() int {
if !r.wroteHeader {
return http.StatusOK
}
return r.status
}