Skip to content

Commit 05ed01e

Browse files
authored
Add error message when task fail (#20)
1 parent a7b913a commit 05ed01e

File tree

9 files changed

+107
-89
lines changed

9 files changed

+107
-89
lines changed

APIFiles/APIClient.go

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"io/ioutil"
2424
"net/http"
2525
"os"
26-
"reflect"
2726
"strconv"
2827
"strings"
2928
"sync"
@@ -403,6 +402,7 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
403402

404403
req.Header.Set("Content-Type", "application/json")
405404
req.Header.Set("User-Agent", c.userAgent)
405+
req.Header.Set("Accept", "*/*")
406406

407407
if command != "login" {
408408
req.Header.Set("X-chkp-sid", sid)
@@ -444,60 +444,7 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
444444
}
445445

446446
if !res.Success {
447-
resCode := ""
448-
resMsg := ""
449-
if code := res.GetData()["code"]; code != nil {
450-
resCode = code.(string)
451-
}
452-
if msg := res.GetData()["message"]; msg != nil {
453-
resMsg = msg.(string)
454-
}
455-
456-
fullErrorMsg := "failed to execute API call" +
457-
"\nStatus: " + res.StatusCode +
458-
"\nCode: " + resCode +
459-
"\nMessage: " + resMsg
460-
461-
if errorMsg := res.data["errors"]; errorMsg != nil {
462-
fullErrorMsg += "\nErrors: "
463-
errorMsgType := reflect.TypeOf(errorMsg).Kind()
464-
if errorMsgType == reflect.String {
465-
fullErrorMsg += errorMsg.(string) + "\n"
466-
} else {
467-
errorsList := res.data["errors"].([]interface{})
468-
for i := range errorsList {
469-
fullErrorMsg += "\n" + strconv.Itoa(i+1) + ". " + errorsList[i].(map[string]interface{})["message"].(string)
470-
}
471-
}
472-
}
473-
474-
if warningMsg := res.data["warnings"]; warningMsg != nil {
475-
fullErrorMsg += "\nWarnings: "
476-
warningMsgType := reflect.TypeOf(warningMsg).Kind()
477-
if warningMsgType == reflect.String {
478-
fullErrorMsg += warningMsg.(string) + "\n"
479-
} else {
480-
warningsList := res.data["warnings"].([]interface{})
481-
for i := range warningsList {
482-
fullErrorMsg += "\n" + strconv.Itoa(i+1) + ". " + warningsList[i].(map[string]interface{})["message"].(string)
483-
}
484-
}
485-
}
486-
487-
if blockingError := res.data["blocking-errors"]; blockingError != nil {
488-
fullErrorMsg += "\nBlocking errors: "
489-
warningMsgType := reflect.TypeOf(blockingError).Kind()
490-
if warningMsgType == reflect.String {
491-
fullErrorMsg += blockingError.(string) + "\n"
492-
} else {
493-
blockingErrorsList := res.data["blocking-errors"].([]interface{})
494-
for i := range blockingErrorsList {
495-
fullErrorMsg += "\n" + strconv.Itoa(i+1) + ". " + blockingErrorsList[i].(map[string]interface{})["message"].(string)
496-
}
497-
}
498-
}
499-
500-
res.ErrorMsg = fullErrorMsg
447+
res.ErrorMsg = res.buildGenericErrMsg()
501448
}
502449

503450
if waitForTask == true && res.Success && command != "show-task" {
@@ -814,22 +761,7 @@ func (c *ApiClient) waitForTasks(taskObjects []interface{}) APIResponse {
814761
fmt.Println("Problem showing tasks, try again")
815762

816763
}
817-
818-
if taskRes.Success {
819-
if v, ok := taskRes.GetData()["tasks"]; ok {
820-
tasks := v.([]interface{})
821-
if len(tasks) > 0 {
822-
for _, task := range tasks {
823-
status := task.(map[string]interface{})["status"].(string)
824-
if status == "failed" || status == "partially succeeded" {
825-
taskRes.Success = false
826-
break
827-
}
828-
}
829-
}
830-
}
831-
}
832-
764+
checkTasksStatus(&taskRes)
833765
return taskRes
834766

835767
}
@@ -842,14 +774,16 @@ task_result: api_response returned from "show-task" command
842774
return:
843775
*/
844776
func checkTasksStatus(taskResult *APIResponse) {
845-
846-
for _, task := range taskResult.data["tasks"].([]interface{}) {
847-
if task.(map[string]interface{})["status"] == "failed" || task.(map[string]interface{})["status"] == "partially succeeded" {
848-
taskResult.setSuccessStatus(false)
849-
break
777+
if v := taskResult.data["tasks"]; v != nil {
778+
for _, task := range taskResult.data["tasks"].([]interface{}) {
779+
if task.(map[string]interface{})["status"] == "failed" || task.(map[string]interface{})["status"] == "partially succeeded" {
780+
taskResult.setSuccessStatus(false)
781+
taskResult.StatusCode = ""
782+
taskResult.setErrMsg(taskResult.buildGenericErrMsg())
783+
break
784+
}
850785
}
851786
}
852-
853787
}
854788

855789
/*

APIFiles/APIResponse.go

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package api_go_sdk
33
import (
44
"encoding/json"
55
"net/http"
6+
"reflect"
7+
"strconv"
68
)
79

810
const OkResponseCode string = "200 OK"
@@ -44,7 +46,6 @@ func (r *APIResponse) GetResTmp() map[string]interface{} {
4446
return r.resObj
4547
}
4648

47-
4849
// Convert API Response to a map
4950
func (r *APIResponse) asGoMap() map[string]interface{} {
5051
dict := map[string]interface{}{
@@ -63,7 +64,91 @@ func (r *APIResponse) asGoMap() map[string]interface{} {
6364
/*
6465
Set the response success status
6566
status: input status
66-
*/
67+
*/
6768
func (r *APIResponse) setSuccessStatus(status bool) {
6869
r.Success = status
6970
}
71+
72+
func (r *APIResponse) buildGenericErrMsg() string {
73+
response := r.GetData()
74+
errMsg := "Failed to execute API call"
75+
76+
if tasks := response["tasks"]; tasks != nil {
77+
tasksList := tasks.([]interface{})
78+
if len(tasksList) > 0 {
79+
for i := range tasksList {
80+
task := tasksList[i].(map[string]interface{})
81+
errMsg += "\nTask: " + task["task-name"].(string) + "\nMessage: "
82+
if taskDetails := task["task-details"]; taskDetails != nil {
83+
taskDetailsList := taskDetails.([]interface{})
84+
if len(taskDetailsList) > 0 {
85+
for j := range taskDetailsList {
86+
if v := taskDetailsList[j].(map[string]interface{})["fault-message"]; v != nil {
87+
errMsg += v.(string)
88+
}
89+
}
90+
}
91+
}
92+
}
93+
}
94+
} else {
95+
resCode := ""
96+
resMsg := ""
97+
if code := response["code"]; code != nil {
98+
resCode = code.(string)
99+
}
100+
if msg := response["message"]; msg != nil {
101+
resMsg = msg.(string)
102+
}
103+
104+
errMsg +=
105+
"\nStatus: " + r.StatusCode +
106+
"\nCode: " + resCode +
107+
"\nMessage: " + resMsg
108+
109+
if errorMsg := response["errors"]; errorMsg != nil {
110+
errMsg += "\nErrors: "
111+
errorMsgType := reflect.TypeOf(errorMsg).Kind()
112+
if errorMsgType == reflect.String {
113+
errMsg += errorMsg.(string) + "\n"
114+
} else {
115+
errorsList := response["errors"].([]interface{})
116+
for i := range errorsList {
117+
errMsg += "\n" + strconv.Itoa(i+1) + ". " + errorsList[i].(map[string]interface{})["message"].(string)
118+
}
119+
}
120+
}
121+
122+
if warningMsg := response["warnings"]; warningMsg != nil {
123+
errMsg += "\nWarnings: "
124+
warningMsgType := reflect.TypeOf(warningMsg).Kind()
125+
if warningMsgType == reflect.String {
126+
errMsg += warningMsg.(string) + "\n"
127+
} else {
128+
warningsList := response["warnings"].([]interface{})
129+
for i := range warningsList {
130+
errMsg += "\n" + strconv.Itoa(i+1) + ". " + warningsList[i].(map[string]interface{})["message"].(string)
131+
}
132+
}
133+
}
134+
135+
if blockingError := response["blocking-errors"]; blockingError != nil {
136+
errMsg += "\nBlocking errors: "
137+
warningMsgType := reflect.TypeOf(blockingError).Kind()
138+
if warningMsgType == reflect.String {
139+
errMsg += blockingError.(string) + "\n"
140+
} else {
141+
blockingErrorsList := response["blocking-errors"].([]interface{})
142+
for i := range blockingErrorsList {
143+
errMsg += "\n" + strconv.Itoa(i+1) + ". " + blockingErrorsList[i].(map[string]interface{})["message"].(string)
144+
}
145+
}
146+
}
147+
}
148+
149+
return errMsg
150+
}
151+
152+
func (r *APIResponse) setErrMsg(message string) {
153+
r.ErrorMsg = message
154+
}

Examples/add_access_rule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Examples
22

33
import (
44
"fmt"
5-
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
5+
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/add_host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Examples
22

33
import (
44
"fmt"
5-
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
5+
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/auto_publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Examples
22

33
import (
44
"fmt"
5-
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
5+
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
"strconv"
88
"time"

Examples/discard_sessions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Examples
22

33
import (
44
"fmt"
5-
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
5+
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/find_dup_ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Examples
22

33
import (
44
"fmt"
5-
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
5+
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/show_hosts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Examples
22

33
import (
44
"fmt"
5-
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
5+
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package main
22

33
import (
44
"fmt"
5-
//"github.com/cp-mgmt-api-go-sdk/Examples"
6-
"./Examples"
5+
"github.com/CheckPointSW/cp-mgmt-api-go-sdk/Examples"
76
"os"
87
)
98

109
func main() {
1110
if len(os.Args) < 2 {
12-
fmt.Println("cp-mgmt-api-go-sdk main : Operation not found. Optional operations are rule/discard/add_host/show_hosts/dup_ip/auto_publish")
11+
fmt.Println("cp-mgmt-api-go-sdk main: Operation not found. Optional operations are rule/discard/add_host/show_hosts/dup_ip/auto_publish")
1312
os.Exit(0)
1413
}
1514

@@ -27,6 +26,6 @@ func main() {
2726
case "auto_publish":
2827
Examples.AutoPublish()
2928
default:
30-
fmt.Println("Operation not supported. Optional operations are rule/discard/add_host/show_hosts/dup_ip/auto_publish")
29+
fmt.Println("cp-mgmt-api-go-sdk main: Operation not supported. Optional operations are rule/discard/add_host/show_hosts/dup_ip/auto_publish")
3130
}
3231
}

0 commit comments

Comments
 (0)