Skip to content

Commit

Permalink
Handle fetch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
apexskier committed Nov 17, 2023
1 parent 5735ce6 commit e3b2179
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"description": "Minimum signal strength (0 - 1) to be considered present.",
"minimum": 0,
"maximum": 1,
"default": 0.7,
"default": 0.25,
"required": false
},
"pollTime": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Eero Presence",
"name": "@apexskier/homebridge-eero-presence",
"version": "1.0.1",
"version": "1.1.0",
"description": "Use your Eero mesh as an occupancy sensor",
"license": "Apache-2.0",
"repository": {
Expand Down
24 changes: 16 additions & 8 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,22 @@ export class EeroPresenceHomebridgePlatform implements DynamicPlatformPlugin {
}

async fetch(input: RequestInfo | URL, init?: RequestInit) {
const response = await fetch(input, {
...init,
headers: {
...init?.headers,
"content-type": "application/json",
cookie: `s=${this.config.userToken}`,
},
});
let response;
try {
response = await fetch(input, {
...init,
headers: {
...init?.headers,
"content-type": "application/json",
cookie: `s=${this.config.userToken}`,
},
});
} catch (error) {
this.log.error("failed to fetch");
throw new this.api.hap.HapStatusError(
this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE,
);
}

if (!response.ok) {
throw new this.api.hap.HapStatusError(
Expand Down
24 changes: 16 additions & 8 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,22 @@ export class EeroPresensePlatformAccessory {
}

async fetch(input: RequestInfo | URL, init?: RequestInit) {
const response = await fetch(input, {
...init,
headers: {
...init?.headers,
"content-type": "application/json",
cookie: `s=${this.accessory.context.config.userToken}`,
},
});
let response;
try {
response = await fetch(input, {
...init,
headers: {
...init?.headers,
"content-type": "application/json",
cookie: `s=${this.accessory.context.config.userToken}`,
},
});
} catch (error) {
this.platform.log.error("failed to fetch");
throw new this.platform.api.hap.HapStatusError(
this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE,
);
}

if (!response.ok) {
throw new this.platform.api.hap.HapStatusError(
Expand Down

0 comments on commit e3b2179

Please sign in to comment.