Skip to content

Commit e6ba4c6

Browse files
authored
docs: improve API rate-limit error handling in table-http-example (#22776)
There are currently three issues with the `table-http-example`: 1. If a user hits the rate limit within this example, an overlay will be displayed mentioning the rate limit issue. This overlay intends to overlap the whole table, except for the paginator. Right now this is broken as there is a minium height throwing off the assumption that the paginator is at the bottom of the example. 2. If a user hits the rate limit, the paginator length is reset to zero. This means that the user is unable to trigger new requests as the previous/next page buttons become disabled. 3. Due to incorrect error handling, the observable will be replaced by a new one that will always return an empty list. The rate limit/API errors should be caught within the switch map to not prevent future API requests on errors. Fixes #11547.
1 parent 5de7a22 commit e6ba4c6

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/components-examples/material/table/table-http/table-http-example.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* Structure */
22
.example-container {
33
position: relative;
4-
min-height: 200px;
54
}
65

76
.example-table-container {
87
position: relative;
8+
min-height: 200px;
99
max-height: 400px;
1010
overflow: auto;
1111
}

src/components-examples/material/table/table-http/table-http-example.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {HttpClient} from '@angular/common/http';
22
import {Component, ViewChild, AfterViewInit} from '@angular/core';
33
import {MatPaginator} from '@angular/material/paginator';
4-
import {MatSort} from '@angular/material/sort';
4+
import {MatSort, SortDirection} from '@angular/material/sort';
55
import {merge, Observable, of as observableOf} from 'rxjs';
66
import {catchError, map, startWith, switchMap} from 'rxjs/operators';
77

@@ -39,21 +39,23 @@ export class TableHttpExample implements AfterViewInit {
3939
switchMap(() => {
4040
this.isLoadingResults = true;
4141
return this.exampleDatabase!.getRepoIssues(
42-
this.sort.active, this.sort.direction, this.paginator.pageIndex);
42+
this.sort.active, this.sort.direction, this.paginator.pageIndex)
43+
.pipe(catchError(() => observableOf(null)));
4344
}),
4445
map(data => {
4546
// Flip flag to show that loading has finished.
4647
this.isLoadingResults = false;
47-
this.isRateLimitReached = false;
48-
this.resultsLength = data.total_count;
48+
this.isRateLimitReached = data === null;
49+
50+
if (data === null) {
51+
return [];
52+
}
4953

54+
// Only refresh the result length if there is new data. In case of rate
55+
// limit errors, we do not want to reset the paginator to zero, as that
56+
// would prevent users from re-triggering requests.
57+
this.resultsLength = data.total_count;
5058
return data.items;
51-
}),
52-
catchError(() => {
53-
this.isLoadingResults = false;
54-
// Catch if the GitHub API has reached its rate limit. Return empty data.
55-
this.isRateLimitReached = true;
56-
return observableOf([]);
5759
})
5860
).subscribe(data => this.data = data);
5961
}
@@ -75,7 +77,7 @@ export interface GithubIssue {
7577
export class ExampleHttpDatabase {
7678
constructor(private _httpClient: HttpClient) {}
7779

78-
getRepoIssues(sort: string, order: string, page: number): Observable<GithubApi> {
80+
getRepoIssues(sort: string, order: SortDirection, page: number): Observable<GithubApi> {
7981
const href = 'https://api.github.com/search/issues';
8082
const requestUrl =
8183
`${href}?q=repo:angular/components&sort=${sort}&order=${order}&page=${page + 1}`;

0 commit comments

Comments
 (0)