Skip to content

Commit 74cd22e

Browse files
committed
update error handling
1 parent 45d458f commit 74cd22e

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/app/user.service.spec.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TestBed, getTestBed, inject } from '@angular/core/testing';
2-
import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
2+
import { HttpClient, HTTP_INTERCEPTORS, HttpErrorResponse } from '@angular/common/http';
33
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
44
import { Observable, of } from 'rxjs';
55

@@ -90,13 +90,13 @@ describe('UserService', () => {
9090
it('should throw with an error message when API returns an error',
9191
inject([HttpClient, HttpTestingController], (http: HttpClient, httpMock: HttpTestingController) => {
9292
const userService = getTestBed().get(UserService);
93-
userService.getUsers().subscribe({
94-
error(actualError) {
95-
expect(of(actualError)).toBeTruthy();
96-
expect(actualError).not.toBeNull();
97-
expect(actualError).not.toBeUndefined();
93+
userService.getUsers().subscribe(
94+
response => fail('should have failed with 500 status'),
95+
error => {
96+
expect(error).toBeTruthy();
97+
expect(error.status).toEqual(500);
9898
}
99-
});
99+
);
100100

101101
const req = httpMock.expectOne(userService.apiEndpoint);
102102
expect(req.request.method).toEqual('GET');
@@ -151,12 +151,13 @@ describe('UserService', () => {
151151

152152
const userService = getTestBed().get(UserService);
153153
userService.addUser(expectedResult)
154-
.subscribe({
155-
error(actualError) {
156-
expect(of(actualError)).toBeTruthy();
157-
expect(actualError).toBeTruthy();
154+
.subscribe(
155+
response => fail('should fail with status 500 error'),
156+
(error: HttpErrorResponse) => {
157+
expect(error).toBeTruthy();
158+
expect(error.status).toEqual(500);
158159
}
159-
});
160+
);
160161

161162
const req = httpMock.expectOne(r => r.url === userService.apiEndpoint && r.headers.has('Content-Type'));
162163
expect(req.request.method).toEqual('POST');

src/app/user.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { throwError as observableThrowError, Observable } from 'rxjs';
2+
import { throwError, Observable } from 'rxjs';
33

44
import { catchError, map } from 'rxjs/operators';
55
import { Injectable } from '@angular/core';
@@ -22,7 +22,7 @@ export class UserService {
2222
}).pipe(
2323
map(this.mapUsers),
2424
catchError(error => {
25-
return observableThrowError('An error occurred');
25+
return throwError(error);
2626
}),
2727
);
2828
}
@@ -46,7 +46,7 @@ export class UserService {
4646
};
4747
}),
4848
catchError(error => {
49-
return observableThrowError('An error occurred');
49+
return throwError(error);
5050
}),
5151
);
5252
}

0 commit comments

Comments
 (0)