Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add error handling when fetching calendar events #11

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export class WeekPlannerCard extends LitElement {
return {
_days: { type: Array },
_config: { type: Object },
_isLoading: { type: Boolean }
_isLoading: { type: Boolean },
_error: { type: String }
}
}

Expand Down Expand Up @@ -79,6 +80,10 @@ export class WeekPlannerCard extends LitElement {
return html`
<ha-card class="${this._noCardBackground ? 'nobackground' : ''}" style="--event-background-color: ${this._eventBackground}">
<div class="card-content">
${this._error ?
html`<ha-alert alert-type="error">${this._error}</ha-alert>` :
''
}
<div class="container">
${this._renderDays()}
</div>
Expand Down Expand Up @@ -157,6 +162,7 @@ export class WeekPlannerCard extends LitElement {

this._loading++;
this._isLoading = true;
this._error = '';
this._events = {};

let startDate = moment().startOf('day');
Expand All @@ -181,13 +187,19 @@ export class WeekPlannerCard extends LitElement {
});

this._loading--;
}).catch(error => {
this._error = 'Error while fetching calendar: ' + error.error;
this._loading = 0;
throw new Error(this._error);
});
});

let checkLoading = window.setInterval(() => {
if (this._loading === 0) {
clearInterval(checkLoading);
this._updateCard();
if (!this._error) {
this._updateCard();
}
this._isLoading = false;

window.setTimeout(() => {
Expand Down
Loading