From e5d973b4c55007ccbc192ae574e35e4a4744a496 Mon Sep 17 00:00:00 2001 From: Alrim42 Date: Wed, 9 Aug 2017 14:51:22 +0200 Subject: [PATCH] Logs now shows complete error with fields --- cmd/snaptel/task.go | 4 +- core/task.go | 8 ++++ core/task_medium_test.go | 81 +++++++++++++--------------------------- 3 files changed, 36 insertions(+), 57 deletions(-) diff --git a/cmd/snaptel/task.go b/cmd/snaptel/task.go index 25b759aba..77e230f71 100644 --- a/cmd/snaptel/task.go +++ b/cmd/snaptel/task.go @@ -387,7 +387,7 @@ func createTaskUsingTaskManifest(ctx *cli.Context) error { if r.Err != nil { errors := strings.Split(r.Err.Error(), " -- ") - errString := "Error creating task:" + errString := "Error creating task: " for _, err := range errors { errString += fmt.Sprintf("%v\n", err) } @@ -448,7 +448,7 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { r := pClient.CreateTask(t.Schedule, wf, t.Name, t.Deadline, !ctx.IsSet("no-start"), t.MaxFailures) if r.Err != nil { errors := strings.Split(r.Err.Error(), " -- ") - errString := "Error creating task:" + errString := "Error creating task: " for _, err := range errors { errString += fmt.Sprintf("%v\n", err) } diff --git a/core/task.go b/core/task.go index 4dec77eea..5d0629d12 100644 --- a/core/task.go +++ b/core/task.go @@ -308,7 +308,15 @@ func CreateTaskFromContent(body io.ReadCloser, var errMsg string for _, e := range errs.Errors() { errMsg = errMsg + e.Error() + " -- " + + log.WithFields(log.Fields{ + "_file": "core/task.go", + "_function": "CreateTaskFromContent", + "_error": e.Error(), + "_fields": e.Fields(), + }).Error("error creating task") } + return nil, errors.New(errMsg[:len(errMsg)-4]) } return task, nil diff --git a/core/task_medium_test.go b/core/task_medium_test.go index dd577dba7..c48f53ec8 100644 --- a/core/task_medium_test.go +++ b/core/task_medium_test.go @@ -173,6 +173,7 @@ func TestUnmarshalBodyTask(t *testing.T) { Convey("Bad JSON file", t, func() { err := createTaskFile(YAML_FILE, YAML_FILE_CONTENT) So(err, ShouldBeNil) + defer deleteTaskFile(YAML_FILE) var tr TaskReq1 file, err := os.Open(YAML_FILE) @@ -182,14 +183,12 @@ func TestUnmarshalBodyTask(t *testing.T) { So(code, ShouldEqual, 400) So(err, ShouldNotBeNil) So(err.Error(), ShouldEqual, "invalid character '-' in numeric literal") - - err = deleteTaskFile(YAML_FILE) - So(err, ShouldBeNil) }) Convey("Proper JSON file", t, func() { err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT) So(err, ShouldBeNil) + defer deleteTaskFile(JSON_FILE) var tr TaskReq1 file, err := os.Open(JSON_FILE) @@ -198,9 +197,6 @@ func TestUnmarshalBodyTask(t *testing.T) { code, err := UnmarshalBody(&tr, file) So(code, ShouldEqual, 0) So(err, ShouldBeNil) - - err = deleteTaskFile(JSON_FILE) - So(err, ShouldBeNil) }) } @@ -219,6 +215,7 @@ func TestCreateTaskRequest(t *testing.T) { Convey("Bad JSON file", t, func() { err := createTaskFile(YAML_FILE, YAML_FILE_CONTENT) So(err, ShouldBeNil) + defer deleteTaskFile(YAML_FILE) file, err := os.Open(YAML_FILE) So(file, ShouldNotBeNil) @@ -227,14 +224,12 @@ func TestCreateTaskRequest(t *testing.T) { So(task, ShouldBeNil) So(err, ShouldNotBeNil) So(err.Error(), ShouldEqual, "invalid character '-' in numeric literal") - - err = deleteTaskFile(YAML_FILE) - So(err, ShouldBeNil) }) Convey("Proper JSON file", t, func() { err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT) So(err, ShouldBeNil) + defer deleteTaskFile(JSON_FILE) file, err := os.Open(JSON_FILE) So(file, ShouldNotBeNil) @@ -249,9 +244,6 @@ func TestCreateTaskRequest(t *testing.T) { So(task.Schedule.StartTimestamp, ShouldBeNil) So(task.Schedule.StopTimestamp, ShouldBeNil) So(task.Start, ShouldEqual, false) - - err = deleteTaskFile(JSON_FILE) - So(err, ShouldBeNil) }) } @@ -271,6 +263,7 @@ func TestCreateTaskFromContent(t *testing.T) { Convey("Bad JSON file", t, func() { err := createTaskFile(YAML_FILE, YAML_FILE_CONTENT) So(err, ShouldBeNil) + defer deleteTaskFile(YAML_FILE) file, err := os.Open(YAML_FILE) So(file, ShouldNotBeNil) @@ -280,58 +273,36 @@ func TestCreateTaskFromContent(t *testing.T) { So(task, ShouldBeNil) So(err, ShouldNotBeNil) So(err.Error(), ShouldEqual, "invalid character '-' in numeric literal") - - err = deleteTaskFile(YAML_FILE) - So(err, ShouldBeNil) - }) - - Convey("Proper JSON file no workflow routine", t, func() { - err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT) - So(err, ShouldBeNil) - - file, err := os.Open(JSON_FILE) - So(file, ShouldNotBeNil) - So(err, ShouldBeNil) - autoStart := true - task, err := CreateTaskFromContent(file, &autoStart, nil) - So(task, ShouldBeNil) - So(err, ShouldNotBeNil) - So(err.Error(), ShouldEqual, "Missing workflow creation routine") - - err = deleteTaskFile(JSON_FILE) - So(err, ShouldBeNil) - }) - - Convey("Proper JSON file erroring routine", t, func() { - err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT) - So(err, ShouldBeNil) - - file, err := os.Open(JSON_FILE) - So(file, ShouldNotBeNil) - So(err, ShouldBeNil) - autoStart := true - task, err := CreateTaskFromContent(file, &autoStart, koRoutine) - So(task, ShouldBeNil) - So(err, ShouldNotBeNil) - So(err.Error(), ShouldEqual, "Dummy error") - - err = deleteTaskFile(JSON_FILE) - So(err, ShouldBeNil) }) - Convey("Proper JSON file proper routine", t, func() { + Convey("Use proper JSON file", t, func() { err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT) So(err, ShouldBeNil) + defer deleteTaskFile(JSON_FILE) file, err := os.Open(JSON_FILE) So(file, ShouldNotBeNil) So(err, ShouldBeNil) autoStart := true - task, err := CreateTaskFromContent(file, &autoStart, okRoutine) - So(task, ShouldBeNil) - So(err, ShouldBeNil) - err = deleteTaskFile(JSON_FILE) - So(err, ShouldBeNil) + Convey("Proper JSON file - no workflow routine", func() { + task, err := CreateTaskFromContent(file, &autoStart, nil) + So(task, ShouldBeNil) + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Missing workflow creation routine") + }) + + Convey("Proper JSON file - erroring routine", func() { + task, err := CreateTaskFromContent(file, &autoStart, koRoutine) + So(task, ShouldBeNil) + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Dummy error") + }) + + Convey("Proper JSON file - proper routine", func() { + task, err := CreateTaskFromContent(file, &autoStart, okRoutine) + So(task, ShouldBeNil) + So(err, ShouldBeNil) + }) }) }