From 66e182a55dd83d1e1719f9d6232e66a081b23dc2 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Wed, 13 Nov 2019 15:46:26 +0100 Subject: [PATCH] runservice: improve errors in logsHandler * return errNotExist in readTaskLogs when the executor task doesn't exist: so the client will receive a 404 instead of a 500 (since a generic error will be mapped to a 500). * Wrap the errNotExist returned by readTaskLogs with a new ErrNotExits reporting "log doesn't exist" --- internal/services/runservice/api/api.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/services/runservice/api/api.go b/internal/services/runservice/api/api.go index 65d27f5..a125788 100644 --- a/internal/services/runservice/api/api.go +++ b/internal/services/runservice/api/api.go @@ -195,9 +195,9 @@ func (h *LogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if sendError { switch { case util.IsNotExist(err): - http.Error(w, err.Error(), http.StatusNotFound) + httpError(w, util.NewErrNotExist(errors.Errorf("log doesn't exist: %w", err))) default: - http.Error(w, err.Error(), http.StatusInternalServerError) + httpError(w, err) } } } @@ -241,15 +241,18 @@ func (h *LogsHandler) readTaskLogs(ctx context.Context, runID, taskID string, se et, err := store.GetExecutorTask(ctx, h.e, task.ID) if err != nil { + if err == etcd.ErrKeyNotFound { + return util.NewErrNotExist(errors.Errorf("executor task with id %q doesn't exist", task.ID)), true + } return err, true } executor, err := store.GetExecutor(ctx, h.e, et.Spec.ExecutorID) - if err != nil && err != etcd.ErrKeyNotFound { + if err != nil { + if err == etcd.ErrKeyNotFound { + return util.NewErrNotExist(errors.Errorf("executor with id %q doesn't exist", et.Spec.ExecutorID)), true + } return err, true } - if executor == nil { - return util.NewErrNotExist(errors.Errorf("executor with id %q doesn't exist", et.Spec.ExecutorID)), true - } var url string if setup {