Skip to content

Commit ba81f0a

Browse files
committed
Fail hard on invalid axis
1 parent 6b301de commit ba81f0a

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/urdf/UrdfJoint.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ export default class UrdfJoint {
5858
const axis = xml.getElementsByTagName(UrdfAttrs.Axis);
5959
if (axis.length > 0) {
6060
const xyzValue = axis[0].getAttribute(UrdfAttrs.Xyz)?.split(" ");
61-
if (xyzValue && xyzValue.length === 3) {
62-
const [x, y, z] = xyzValue.map(parseFloat);
63-
this.axis = new Vector3({
64-
x,
65-
y,
66-
z,
67-
});
61+
if (!xyzValue || xyzValue.length !== 3) {
62+
throw new Error(
63+
"If specified, axis must have an xyz value composed of three numbers",
64+
);
6865
}
66+
const [x, y, z] = xyzValue.map(parseFloat);
67+
this.axis = new Vector3({
68+
x,
69+
y,
70+
z,
71+
});
6972
}
7073
}
7174
}

test/urdfjoint.test.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ describe("UrdfJoint", () => {
1515
jointWithAxisUrdf,
1616
"text/xml",
1717
).documentElement;
18+
if (!xml) {
19+
throw new Error("Failed to parse XML");
20+
}
1821
const joint = new UrdfJoint({ xml });
1922
expect(joint.axis.x).toBe(0);
2023
expect(joint.axis.y).toBe(1);
@@ -33,13 +36,16 @@ describe("UrdfJoint", () => {
3336
jointNoAxisUrdf,
3437
"text/xml",
3538
).documentElement;
39+
if (!xml) {
40+
throw new Error("Failed to parse XML");
41+
}
3642
const joint = new UrdfJoint({ xml });
3743
expect(joint.axis.x).toBe(1);
3844
expect(joint.axis.y).toBe(0);
3945
expect(joint.axis.z).toBe(0);
4046
});
4147

42-
it("should default axis to (1,0,0) if axis xyz is malformed", () => {
48+
it("should throw if axis xyz is malformed", () => {
4349
const jointMalformedAxisUrdf = `
4450
<joint name="test_joint" type="revolute">
4551
<parent link="link1"/>
@@ -52,9 +58,11 @@ describe("UrdfJoint", () => {
5258
jointMalformedAxisUrdf,
5359
"text/xml",
5460
).documentElement;
55-
const joint = new UrdfJoint({ xml });
56-
expect(joint.axis.x).toBe(1);
57-
expect(joint.axis.y).toBe(0);
58-
expect(joint.axis.z).toBe(0);
61+
if (!xml) {
62+
throw new Error("Failed to parse XML");
63+
}
64+
expect(() => new UrdfJoint({ xml })).toThrowError(
65+
"If specified, axis must have an xyz value composed of three numbers",
66+
);
5967
});
6068
});

0 commit comments

Comments
 (0)