diff options
Diffstat (limited to 'pkg/worker_handler/handler.go')
-rw-r--r-- | pkg/worker_handler/handler.go | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/pkg/worker_handler/handler.go b/pkg/worker_handler/handler.go index e0d1aae0..672c5838 100644 --- a/pkg/worker_handler/handler.go +++ b/pkg/worker_handler/handler.go @@ -100,14 +100,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { size, err := strconv.ParseInt(length, 10, 64) if err != nil { // if got an error while parsing -> assign 500 code to the writer and return - http.Error(w, errors.E(op, err).Error(), 500) + http.Error(w, "", 500) h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, errors.Str("error while parsing value from the `content-length` header")), start: start, elapsed: time.Since(start)}) return } if size > int64(h.maxRequestSize) { h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, errors.Str("request body max size is exceeded")), start: start, elapsed: time.Since(start)}) - http.Error(w, errors.E(op, errors.Str("request body max size is exceeded")).Error(), 500) + http.Error(w, errors.E(op, errors.Str("request body max size is exceeded")).Error(), http.StatusBadRequest) return } } @@ -135,21 +135,21 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { p, err := req.Payload() if err != nil { - http.Error(w, errors.E(op, err).Error(), 500) + h.handleError(w, r, start, err) h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, err), start: start, elapsed: time.Since(start)}) return } rsp, err := h.pool.Exec(p) if err != nil { - http.Error(w, errors.E(op, err).Error(), 500) + h.handleError(w, r, start, err) h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, err), start: start, elapsed: time.Since(start)}) return } resp, err := NewResponse(rsp) if err != nil { - http.Error(w, errors.E(op, err).Error(), resp.Status) + h.handleError(w, r, start, err) h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, err), start: start, elapsed: time.Since(start)}) return } @@ -162,6 +162,26 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// handleError will handle internal RR errors and return 500 +func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, start time.Time, err error) { + const op = errors.Op("handle_error") + // internal error types, user should not see them + if errors.Is(errors.SoftJob, err) || + errors.Is(errors.WatcherStopped, err) || + errors.Is(errors.WorkerAllocate, err) || + errors.Is(errors.NoFreeWorkers, err) || + errors.Is(errors.ExecTTL, err) || + errors.Is(errors.IdleTTL, err) || + errors.Is(errors.TTL, err) || + errors.Is(errors.Encode, err) || + errors.Is(errors.Decode, err) || + errors.Is(errors.Network, err) { + // write an internal server error + w.WriteHeader(http.StatusInternalServerError) + h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, err), start: start, elapsed: time.Since(start)}) + } +} + // handleResponse triggers response event. func (h *Handler) handleResponse(req *Request, resp *Response, start time.Time) { h.sendEvent(ResponseEvent{Request: req, Response: resp, start: start, elapsed: time.Since(start)}) |