Skip to content

Commit

Permalink
try to prevent a crash...
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfonso committed Nov 21, 2023
1 parent 80cc616 commit 32babdb
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1890,34 +1890,38 @@ class WebServer {
}

const user = await this._getUserId(this.config.defaultUser);
const state = await this.adapter.getForeignStateAsync(entity.context.STATE.getId, {user});
if (state && state.val) {
let events = state.val;
if (typeof state.val === 'string') {
try {
events = JSON.parse(state.val);
} catch (e) {
this.log.warn(`Could not process calendar entries. Make sure it is JSON and array: ${e}`);
try {
const state = await this.adapter.getForeignStateAsync(entity.context.STATE.getId, {user});
if (state && state.val) {
let events = state.val;
if (typeof state.val === 'string') {
try {
events = JSON.parse(state.val);
} catch (e) {
this.log.warn(`Could not process calendar entries. Make sure it is JSON and array: ${e}`);
}
}
}
const results = [];
if (events instanceof Array) {
for (const event of events) {
const evStart = new Date(event._date).getTime();
const evEnd = new Date(event._end).getTime();
if (evStart >= start || evEnd <= end || (evStart <= start && evEnd >= end)) { //event in range?
results.push({
start: event._date,
end: event._end,
summary: event.event
});
const results = [];
if (events instanceof Array) {
for (const event of events) {
const evStart = new Date(event._date).getTime();
const evEnd = new Date(event._end).getTime();
if (evStart >= start || evEnd <= end || (evStart <= start && evEnd >= end)) { //event in range?
results.push({
start: event._date,
end: event._end,
summary: event.event
});
}
}
res.json(results);
return;
} else {
this.log.warn(`Could not process calendar entries. Make sure it is JSON and array.`);
}
res.json(results);
return;
} else {
this.log.warn(`Could not process calendar entries. Make sure it is JSON and array.`);
}
} catch (e) {
this.log.error('Could not get state ' + entity.context.STATE.getId + ': ' + e);
}

//fallback: empty list.
Expand Down

0 comments on commit 32babdb

Please sign in to comment.