Skip to content

Commit

Permalink
fix: catch errors when linked tm is unavailable
Browse files Browse the repository at this point in the history
Signed-off-by: fatadel <[email protected]>
  • Loading branch information
fatadel committed Feb 16, 2023
1 parent 9d9fdf6 commit aae0c67
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/td-tools/src/thing-model-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export class ThingModelHelpers {
case "http": {
return new Promise((resolve, reject) => {
http.get(uri, (res) => {
if (!res.statusCode || res.statusCode !== 200) {
reject(new Error(`http status code not 200 but ${res.statusCode}`));
}

res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
Expand All @@ -264,6 +268,10 @@ export class ThingModelHelpers {
return new Promise((resolve, reject) => {
https
.get(uri, (res) => {
if (!res.statusCode || res.statusCode !== 200) {
reject(new Error(`https status code not 200 but ${res.statusCode}`));
}

res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
Expand Down
44 changes: 44 additions & 0 deletions packages/td-tools/test/ThingModelHelperTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,48 @@ class ThingModelHelperTest {
expect(validated.valid).to.be.false;
expect(validated.errors).to.be.equal(`Missing required fields in map for model ${thing.title}`);
}

@test async "should fail on unavailable linked ThingModel - http"() {
const thing = {
"@context": ["https://www.w3.org/2022/wot/td/v1.1"],
"@type": "tm:ThingModel",
title: "Dimmable Colored Lamp",
version: {
model: "1.0.0",
},
links: [
{
rel: "tm:extends",
href: "http://example.com/models/colored-lamp-1.0.0.tm.jsonld",
type: "application/tm+json",
},
],
} as unknown as ThingModel;

expect(this.thingModelHelpers.getPartialTDs(thing)).to.be.rejectedWith(
new Error("http status code not 200 but 404")
);
}

@test async "should fail on unavailable linked ThingModel - https"() {
const thing = {
"@context": ["https://www.w3.org/2022/wot/td/v1.1"],
"@type": "tm:ThingModel",
title: "Dimmable Colored Lamp",
version: {
model: "1.0.0",
},
links: [
{
rel: "tm:extends",
href: "https://example.com/models/colored-lamp-1.0.0.tm.jsonld",
type: "application/tm+json",
},
],
} as unknown as ThingModel;

expect(this.thingModelHelpers.getPartialTDs(thing)).to.be.rejectedWith(
new Error("https status code not 200 but 404")
);
}
}

0 comments on commit aae0c67

Please sign in to comment.