Skip to content

Commit

Permalink
feat(API) Better error messages on invalid ParentID (#343)
Browse files Browse the repository at this point in the history
* feat(API) Better error messages on invalid ParentID

* feat(API) remove forgotten test code

* feat(API) update new error message on `create_object_test`
  • Loading branch information
Barbozafab authored Jan 10, 2024
1 parent 605ddae commit 0d5a794
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion API/models/create_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestCreateGenericWithParentNotRoomReturnsError(t *testing.T) {

_, err := integration.CreateGeneric(rack["id"].(string), "create-object-8-generic")
assert.NotNil(t, err)
assert.ErrorContains(t, err, "ParentID should correspond to Existing Room ID")
assert.ErrorContains(t, err, "ParentID should correspond to existing room ID")
}

func TestCreateGenericWithParentRoomWorks(t *testing.T) {
Expand Down
16 changes: 4 additions & 12 deletions API/models/validateEntity.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,9 @@ func validateParent(ent string, entNum int, t map[string]interface{}) (map[strin
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "ParentID should correspond to Existing Rack or Device ID"}
Message: "ParentID should correspond to existing rack or device ID"}

case u.GROUP:
w, _ := GetObject(req, "device", u.RequestFilters{}, nil)
if w != nil {
parent["parent"] = "device"
parent["domain"] = w["domain"]
parent["id"] = w["id"]
return parent, nil
}

x, _ := GetObject(req, "rack", u.RequestFilters{}, nil)
if x != nil {
parent["parent"] = "rack"
Expand All @@ -125,7 +117,7 @@ func validateParent(ent string, entNum int, t map[string]interface{}) (map[strin
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "ParentID should correspond to Existing ID"}
Message: "ParentID should correspond to existing rack, room or building ID"}

case u.GENERIC:
x, _ := GetObject(req, "room", u.RequestFilters{}, nil)
Expand All @@ -137,7 +129,7 @@ func validateParent(ent string, entNum int, t map[string]interface{}) (map[strin
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "ParentID should correspond to Existing Room ID"}
Message: "ParentID should correspond to existing room ID"}
default:
parentInt := u.GetParentOfEntityByInt(entNum)
parentStr := u.EntityToString(parentInt)
Expand All @@ -152,7 +144,7 @@ func validateParent(ent string, entNum int, t map[string]interface{}) (map[strin
println("ENTITY VALUE: ", ent)
println("We got Parent: ", parent, " with ID:", t["parentId"].(string))
return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "ParentID should correspond to Existing ID"}
Message: fmt.Sprintf("ParentID should correspond to existing %s ID", parentStr)}
}
}
return nil, nil
Expand Down

0 comments on commit 0d5a794

Please sign in to comment.