Skip to content

Commit 7aefc3e

Browse files
committed
Fix error handling: #36
1 parent 1d2aa1a commit 7aefc3e

File tree

6 files changed

+55
-78
lines changed

6 files changed

+55
-78
lines changed

ClientApp/package-lock.json

Lines changed: 14 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ClientApp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@angular/platform-browser": "7.0.0",
2424
"@angular/platform-browser-dynamic": "7.0.0",
2525
"@angular/router": "7.0.0",
26-
"angular-oauth2-oidc": "4.0.3",
26+
"angular-oauth2-oidc": "5.0.2",
2727
"core-js": "2.5.7",
2828
"hammerjs": "2.0.8",
2929
"rxjs": "6.3.3",

ClientApp/src/app/account/signin.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Router } from '@angular/router';
2+
import { HttpErrorResponse } from '@angular/common/http';
23

34
import { OAuthService } from 'angular-oauth2-oidc';
45

@@ -35,22 +36,17 @@ export class Signin {
3536
// Redirects the user.
3637
this.router.navigate([redirect]);
3738
})
38-
.catch((error: any) => {
39+
.catch((errorResponse: HttpErrorResponse) => {
3940
// Checks for error in response (error from the Token endpoint).
40-
if (error.body !== '') {
41-
const body: any = error.json();
42-
43-
switch (body.error) {
41+
if (errorResponse.error !== '') {
42+
switch (errorResponse.error.error) {
4443
case 'invalid_grant':
4544
this.errorMessages.push({ description: 'Invalid email or password.' });
4645
break;
4746
default:
4847
this.errorMessages.push({ description: 'Unexpected error. Try again.' });
4948
}
5049
} else {
51-
const errMsg = (error.message) ? error.message :
52-
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
53-
console.log(errMsg);
5450
this.errorMessages.push({ description: 'Server error. Try later.' });
5551
}
5652
});

ClientApp/src/app/account/signup/signup.component.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,18 @@ export class SignupComponent extends Signin {
2323
signup(): void {
2424
this.identityService.create(this.model)
2525
.subscribe(
26-
(res: any) => {
27-
// IdentityResult.
28-
if (res.succeeded) {
29-
// Signs in the user.
30-
this.signin();
31-
} else {
32-
this.errorMessages = res.errors;
33-
}
34-
},
35-
(error: any) => {
36-
const errMsg = (error.message) ? error.message :
37-
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
38-
console.log(errMsg);
39-
this.errorMessages.push({ description: 'Server error. Try later.' });
40-
});
26+
(res: any) => {
27+
// IdentityResult.
28+
if (res.succeeded) {
29+
// Signs in the user.
30+
this.signin();
31+
} else {
32+
this.errorMessages = res.errors;
33+
}
34+
},
35+
(error: any) => {
36+
this.errorMessages.push({ description: 'Server error. Try later.' });
37+
});
4138
}
4239

4340
}

ClientApp/src/app/resources/resources.component.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ export class ResourcesComponent implements OnInit {
2323
.get('/api/values', {
2424
headers: this.authenticationService.getAuthorizationHeader()
2525
})
26-
.subscribe((data: any) => {
27-
this.values = data;
28-
},
29-
(error: HttpErrorResponse) => {
30-
if (error.error instanceof Error) {
31-
console.log('An error occurred:', error.error.message);
32-
} else {
33-
console.log(`Backend returned code ${error.status}, body was: ${error.error}`);
34-
}
35-
});
26+
.subscribe(
27+
(data: any) => {
28+
this.values = data;
29+
},
30+
(error: HttpErrorResponse) => {
31+
if (error.error instanceof Error) {
32+
console.log('An error occurred:', error.error.message);
33+
} else {
34+
console.log(`Backend returned code ${error.status}, body was: ${error.error}`);
35+
}
36+
});
3637
}
3738

3839
}

ClientApp/src/app/services/identity.service.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
3-
import { BehaviorSubject, Observable, throwError as _throw } from 'rxjs';
3+
import { BehaviorSubject, Observable, throwError } from 'rxjs';
44
import { map, catchError } from 'rxjs/operators';
55

66
import { AuthenticationService } from './authentication.service';
@@ -26,11 +26,12 @@ import { AuthenticationService } from './authentication.service';
2626
.get('/api/identity/GetAll', {
2727
headers: this.authenticationService.getAuthorizationHeader()
2828
})
29-
.subscribe((data: any) => {
30-
this.users.next(data);
31-
},
29+
.subscribe(
30+
(data: any) => {
31+
this.users.next(data);
32+
},
3233
(error: HttpErrorResponse) => {
33-
if (error.error instanceof Error) {
34+
if (error.error instanceof ErrorEvent) {
3435
console.log('An error occurred:', error.error.message);
3536
} else {
3637
console.log(`Backend returned code ${error.status}, body was: ${error.error}`);
@@ -54,7 +55,7 @@ import { AuthenticationService } from './authentication.service';
5455
return response;
5556
}),
5657
catchError((error: any) => {
57-
return _throw(error);
58+
return throwError(error);
5859
}));
5960
}
6061

@@ -71,12 +72,13 @@ import { AuthenticationService } from './authentication.service';
7172
.post('/api/identity/Delete', body, {
7273
headers: this.authenticationService.getAuthorizationHeader()
7374
})
74-
.subscribe(() => {
75-
// Refreshes the users.
76-
this.getAll();
77-
},
75+
.subscribe(
76+
() => {
77+
// Refreshes the users.
78+
this.getAll();
79+
},
7880
(error: HttpErrorResponse) => {
79-
if (error.error instanceof Error) {
81+
if (error.error instanceof ErrorEvent) {
8082
console.log('An error occurred:', error.error.message);
8183
} else {
8284
console.log(`Backend returned code ${error.status}, body was: ${error.error}`);

0 commit comments

Comments
 (0)