Skip to content

Commit

Permalink
feat: exit process with error
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 2, 2023
1 parent 3468419 commit c1d4512
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/plugin/networkx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer n.mu.RUnlock()

proc := process.New()
var procErr error
defer func() {
proc.Stack().Wait()
proc.Exit(nil)
proc.Exit(procErr)
}()

go func() {
Expand All @@ -399,11 +400,13 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {

req, err := n.request(r)
if err != nil {
procErr = err
_ = n.response(r, w, n.errorPayload(proc, UnsupportedMediaType))
return
}
outPayload, err := primitive.MarshalText(req)
if err != nil {
procErr = err
_ = n.response(r, w, n.errorPayload(proc, BadRequest))
return
}
Expand All @@ -419,6 +422,7 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if ioStream.Links()+outStream.Links() == 0 {
procErr = node.ErrDiscardPacket
return
}

Expand All @@ -435,6 +439,7 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {
stream = ioStream
}
if !ok {
procErr = node.ErrDiscardPacket
_ = n.response(r, w, n.errorPayload(proc, ServiceUnavailable))
return
}
Expand All @@ -451,15 +456,21 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {

var res HTTPPayload
if err := primitive.Unmarshal(inPayload, &res); err != nil {
if _, ok := packet.AsError(inPck); ok {
if err, ok := packet.AsError(inPck); ok {
procErr = err
res = InternalServerError
} else {
res.Body = inPayload
}
}

if err := n.response(r, w, res); err != nil {
_ = n.response(r, w, n.errorPayload(proc, InternalServerError))
if err = n.response(r, w, res); err != nil {
res = n.errorPayload(proc, InternalServerError)
_ = n.response(r, w, res)
}

if procErr == nil && res.Status >= 400 && res.Status < 600 {
procErr = errors.New(http.StatusText(res.Status))
}

break
Expand Down

0 comments on commit c1d4512

Please sign in to comment.