Skip to content

Commit

Permalink
Reject responses that aren't 200s
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnewcomer committed Nov 8, 2024
1 parent 89f6d46 commit 2848e1c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/dialog/Modax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ export class Modax extends RapidElement {
// this.cancelToken = CancelToken.source();
this.fetching = true;
this.body = this.getLoading();
getUrl(this.endpoint, null, this.getHeaders()).then(
(response: WebResponse) => {
getUrl(this.endpoint, null, this.getHeaders())
.then((response: WebResponse) => {
// if it's a full page, breakout of the modal
if (response.body.indexOf('<!DOCTYPE HTML>') == 0) {
this.open = false;
Expand All @@ -294,8 +294,12 @@ export class Modax extends RapidElement {
});
});
}
}
);
})
.catch((error) => {
this.fetching = false;
this.open = false;
this.fireCustomEvent(CustomEventType.Error, { error });
});

Check warning on line 302 in src/dialog/Modax.ts

View check run for this annotation

Codecov / codecov/patch

src/dialog/Modax.ts#L300-L302

Added lines #L300 - L302 were not covered by tests
}

public submit(extra = {}): void {
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,6 @@ export enum CustomEventType {
DragStart = 'temba-drag-start',
DragStop = 'temba-drag-stop',
Resized = 'temba-resized',
DetailsChanged = 'temba-details-changed'
DetailsChanged = 'temba-details-changed',
Error = 'temba-error'
}
5 changes: 5 additions & 0 deletions src/list/ContentMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export class ContentMenu extends RapidElement {
});
})
.catch((error: any) => {
this.fireCustomEvent(CustomEventType.Loaded, {
buttons: this.buttons,
items: this.items
});

console.error(error);
});
}
Expand Down
7 changes: 3 additions & 4 deletions src/list/TembaList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class TembaList extends RapidElement {
this.items = newItems;
}
} catch (error) {
// console.error(error);
this.paused = true;
}
}

Expand All @@ -332,7 +332,6 @@ export class TembaList extends RapidElement {

try {
const page = await fetchResultsPage(endpoint, controller);

// sanitize our options if necessary
if (this.sanitizeOption) {
page.results.forEach(this.sanitizeOption);
Expand All @@ -350,10 +349,10 @@ export class TembaList extends RapidElement {
} catch (error) {
// aborted
this.reset();
console.log(error);
this.paused = true;
this.loading = false;
return;
}

this.nextPage = nextPage;
}
this.pages = pages;
Expand Down
10 changes: 6 additions & 4 deletions src/list/TembaMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,8 @@ export class TembaMenu extends ResizeElement {
private loadItems(item: MenuItem, event: MouseEvent = null) {
if (item && item.endpoint) {
item.loading = true;
this.httpComplete = fetchResults(item.endpoint).then(
(items: MenuItem[]) => {
this.httpComplete = fetchResults(item.endpoint)
.then((items: MenuItem[]) => {
items.forEach((newItem) => {
if (!newItem.items) {
const prevItem = (item.items || []).find(
Expand Down Expand Up @@ -811,8 +811,10 @@ export class TembaMenu extends ResizeElement {

this.requestUpdate('root');
this.scrollSelectedIntoView();
}
);
})
.catch((error) => {
this.fireCustomEvent(CustomEventType.Error, { error });
});
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export const getUrl = (

fetch(url, options)
.then((response) => {
if (response.status < 200 || response.status >= 300) {
reject(response);
return;
}

response.text().then((body: string) => {
let json = {};
try {
Expand Down Expand Up @@ -177,7 +182,9 @@ export const fetchResultsPage = (
next: response.json.next
});
})
.catch((error) => reject(error));
.catch((error) => {
return reject(error);
});
});
};

Expand Down Expand Up @@ -326,7 +333,6 @@ export const postFormData = (
}
})
.catch((err) => {
console.error(err);
reject(err);
});
});
Expand Down

0 comments on commit 2848e1c

Please sign in to comment.