Skip to content

Commit

Permalink
Fix error screen not showing up for failing API requests
Browse files Browse the repository at this point in the history
The ErrorHandlingInterceptor was not matching the Raiden API URL correctly
  • Loading branch information
manuelwedler committed Apr 15, 2021
1 parent ffe812d commit d2322ee
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/app/interceptors/error-handling.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class MockRequestingService {
getData(): Observable<any> {
return this.http.get(this.raidenConfig.api);
}

makeRequest(url: string): Observable<any> {
return this.http.get(url);
}
}

describe('ErrorHandlingInterceptor', () => {
Expand Down Expand Up @@ -254,4 +258,55 @@ describe('ErrorHandlingInterceptor', () => {
expect(refreshSpy).toHaveBeenCalledTimes(1);
expect(notificationService.apiError).toBeFalsy();
});

it('should handle non-response Raiden API errors for path url requests', () => {
raidenConfig.api = '/api/v1';
// Unlike the HttpClientModule, the HttpClientTestingModule does not prepend the url
// property of a HttpResponse with the host if the request was made by a URL path.
const requestUrl = window.location.origin + raidenConfig.api;
const errorSpy = jasmine.createSpy();

service.makeRequest(requestUrl).subscribe(() => {
fail('On next should not be called');
}, errorSpy);

const request = httpMock.expectOne(requestUrl);
request.flush(
{},
{
status: 504,
statusText: '',
}
);

expect(notificationService.addErrorNotification).toHaveBeenCalledTimes(
1
);
expect(errorSpy).toHaveBeenCalledTimes(1);
});

it('should not misinterpret non-api errors', () => {
raidenConfig.api = '/api/v1';
const requestUrl = 'http://some.url/api/v1';
const errorSpy = jasmine.createSpy();

service.makeRequest(requestUrl).subscribe(() => {
fail('On next should not be called');
}, errorSpy);

const request = httpMock.expectOne(requestUrl);
request.flush(
{},
{
status: 504,
statusText: '',
}
);

expect(notificationService.addErrorNotification).toHaveBeenCalledTimes(
0
);
expect(notificationService.apiError).toBeFalsy();
expect(errorSpy).toHaveBeenCalledTimes(1);
});
});
11 changes: 9 additions & 2 deletions src/app/interceptors/error-handling.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ErrorHandlingInterceptor implements HttpInterceptor {
tap((event) => {
if (
event instanceof HttpResponse &&
event.url.startsWith(this.raidenConfig.api) &&
this.isRaidenApiUrl(event.url) &&
this.notificationService.apiError
) {
this.raidenService.attemptRpcConnection();
Expand All @@ -47,7 +47,7 @@ export class ErrorHandlingInterceptor implements HttpInterceptor {

if (
error instanceof HttpErrorResponse &&
error.url.startsWith(this.raidenConfig.api) &&
this.isRaidenApiUrl(error.url) &&
(error.status === 504 || error.status === 0)
) {
errMsg = 'Could not connect to the Raiden API';
Expand Down Expand Up @@ -94,4 +94,11 @@ export class ErrorHandlingInterceptor implements HttpInterceptor {
console.error(errMsg);
return throwError(errMsg);
}

private isRaidenApiUrl(url: string): boolean {
return (
url.startsWith(this.raidenConfig.api) ||
url.startsWith(window.location.origin + this.raidenConfig.api)
);
}
}

0 comments on commit d2322ee

Please sign in to comment.