Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r committed Nov 27, 2024
1 parent 9548071 commit 1192b0c
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ var (

ErrTypeMismatch = fmt.Errorf("cannot invoke function with mismatched types")

errBadRequest = fmt.Errorf("bad request")
errFunctionMissing = fmt.Errorf("function not found")
errUnauthorized = fmt.Errorf("unauthorized")

// DefaultMaxBodySize is the default maximum size read within a single incoming
// invoke request (100MB).
DefaultMaxBodySize = 1024 * 1024 * 100
Expand Down Expand Up @@ -393,7 +397,18 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if err := h.invoke(w, r); err != nil {
w.WriteHeader(http.StatusInternalServerError)
status := http.StatusInternalServerError
if errors.Is(err, errFunctionMissing) {
// XXX: This is a 500 within the JS SDK. We should probably
// change the JS SDK's status code to 410. 404 indicates that
// the overall API for serving Inngest isn't found.
status = http.StatusGone
} else if errors.Is(err, errBadRequest) {
status = http.StatusBadRequest
} else if errors.Is(err, errUnauthorized) {
status = http.StatusUnauthorized
}
w.WriteHeader(status)
w.Header().Set("content-type", "application/json")
_ = json.NewEncoder(w).Encode(sdkrequest.ErrorResponse{
Message: err.Error(),
Expand Down Expand Up @@ -822,10 +837,7 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error {

if !h.isDev() {
if sig = r.Header.Get(HeaderKeySignature); sig == "" {
return publicerr.Error{
Message: "unauthorized",
Status: 401,
}
return errUnauthorized
}
}

Expand All @@ -836,10 +848,7 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error {
byt, err := io.ReadAll(http.MaxBytesReader(w, r.Body, int64(max)))
if err != nil {
h.Logger.Error("error decoding function request", "error", err)
return publicerr.Error{
Message: "Error reading request",
Status: 500,
}
return fmt.Errorf("%w: %s", errBadRequest, err)
}

if valid, _, err := ValidateRequestSignature(
Expand All @@ -851,21 +860,15 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error {
h.isDev(),
); !valid {
h.Logger.Error("unauthorized inngest invoke request", "error", err)
return publicerr.Error{
Message: "unauthorized",
Status: 401,
}
return errUnauthorized
}

fnID := r.URL.Query().Get("fnId")

request := &sdkrequest.Request{}
if err := json.Unmarshal(byt, request); err != nil {
h.Logger.Error("error decoding function request", "error", err)
return publicerr.Error{
Message: "malformed input",
Status: 400,
}
return fmt.Errorf("%w: %s", errBadRequest, err)
}

if request.UseAPI {
Expand All @@ -887,13 +890,7 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error {
h.l.RUnlock()

if fn == nil {
// XXX: This is a 500 within the JS SDK. We should probably change
// the JS SDK's status code to 410. 404 indicates that the overall
// API for serving Inngest isn't found.
return publicerr.Error{
Message: fmt.Sprintf("function not found: %s", fnID),
Status: 410,
}
return fmt.Errorf("%w: %s", errFunctionMissing, fnID)
}

l := h.Logger.With("fn", fnID, "call_ctx", request.CallCtx)
Expand Down

0 comments on commit 1192b0c

Please sign in to comment.