Skip to content

Commit 425ddcf

Browse files
authored
Merge pull request #5 from alisaduncan/update-error-handling
Update error handling
2 parents 45d458f + ef21d8a commit 425ddcf

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

Diff for: src/app/app.component.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inject, TestBed, async } from '@angular/core/testing';
1+
import { TestBed, async } from '@angular/core/testing';
22
import { NO_ERRORS_SCHEMA } from '@angular/core';
33

44
import { AppComponent } from './app.component';

Diff for: src/app/user.service.spec.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { TestBed, getTestBed, inject } from '@angular/core/testing';
2-
import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
2+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
33
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
4-
import { Observable, of } from 'rxjs';
54

65
import { UserService } from './user.service';
76
import { User } from './user';
@@ -90,13 +89,13 @@ describe('UserService', () => {
9089
it('should throw with an error message when API returns an error',
9190
inject([HttpClient, HttpTestingController], (http: HttpClient, httpMock: HttpTestingController) => {
9291
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();
92+
userService.getUsers().subscribe(
93+
response => fail('should have failed with 500 status'),
94+
error => {
95+
expect(error).toBeTruthy();
96+
expect(error.status).toEqual(500);
9897
}
99-
});
98+
);
10099

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

152151
const userService = getTestBed().get(UserService);
153152
userService.addUser(expectedResult)
154-
.subscribe({
155-
error(actualError) {
156-
expect(of(actualError)).toBeTruthy();
157-
expect(actualError).toBeTruthy();
153+
.subscribe(
154+
response => fail('should fail with status 500 error'),
155+
(error: HttpErrorResponse) => {
156+
expect(error).toBeTruthy();
157+
expect(error.status).toEqual(500);
158158
}
159-
});
159+
);
160160

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

Diff for: src/app/user.service.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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';
6-
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
6+
import { HttpClient, HttpHeaders } from '@angular/common/http';
77

88

99
import { User } from './user';
@@ -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
}

Diff for: src/app/users/dialog/dialog.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component, Inject } from '@angular/core';
2-
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
1+
import { Component } from '@angular/core';
2+
import { MatDialogRef } from '@angular/material';
33

44
@Component({
55
selector: 'app-dialog',

0 commit comments

Comments
 (0)