return bad function http resp error (#728)

* return bad function http resp error

this was being thrown into the fn server logs but it's relatively easy to get
this to crop up if a function user forgets that they left a `println` laying
around that gets written to stdout, it garbles the http (or json, in its case)
output and they just see 'internal server error'. for certain clients i could
see that we really do want to keep this as 'internal server error' but for
things like e.g. docker image not authorized we're showing that in the
response, so this seems apt.

json likely needs the same treatment, will file a bug.

as always, my error messages are rarely helpful enough, help me please :)

closes #355

* add formatting directive

* fix up http error

* output bad jasons to user

closes #729

woo
This commit is contained in:
Reed Allman
2018-02-12 17:51:45 -08:00
committed by GitHub
parent 9d3b66d807
commit 97194b3d8b
3 changed files with 13 additions and 10 deletions

View File

@@ -3,8 +3,11 @@ package protocol
import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"github.com/fnproject/fn/api/models"
)
// HTTPProtocol converts stdin/stdout streams into HTTP/1.1 compliant
@@ -37,7 +40,7 @@ func (h *HTTPProtocol) Dispatch(ctx context.Context, ci CallInfo, w io.Writer) e
resp, err := http.ReadResponse(bufio.NewReader(h.out), ci.Request())
if err != nil {
return err
return models.NewAPIError(http.StatusBadGateway, fmt.Errorf("invalid http response from function err: %v", err))
}
defer resp.Body.Close()

View File

@@ -7,6 +7,8 @@ import (
"fmt"
"io"
"net/http"
"github.com/fnproject/fn/api/models"
)
// This is sent into the function
@@ -197,7 +199,7 @@ func (h *JSONProtocol) Dispatch(ctx context.Context, ci CallInfo, w io.Writer) e
jout := new(jsonOut)
dec := json.NewDecoder(h.out)
if err := dec.Decode(jout); err != nil {
return fmt.Errorf("error decoding JSON from user function: %v", err)
return models.NewAPIError(http.StatusBadGateway, fmt.Errorf("invalid json response from function err: %v", err))
}
if rw, ok := w.(http.ResponseWriter); ok {
// this has to be done for pulling out: