Skip to content

Commit 9a3c681

Browse files
committed
Improved request error handling
1 parent f869ac4 commit 9a3c681

5 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/app/api/model/api-response.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class ApiResponse<T> {
2+
3+
constructor(cont: T) {
4+
this.content = cont;
5+
}
6+
7+
content: T;
8+
}

src/app/api/model/response.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/app/authentication/service/authentication.service.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { HttpClient } from '@angular/common/http';
1+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3-
import { Response } from '@app/api/model/response';
3+
import { ApiResponse } from '@app/api/model/api-response';
44
import { environment } from '@environments/environment';
5-
import { Observable } from 'rxjs';
6-
import { map } from 'rxjs/operators';
5+
import { Observable, throwError } from 'rxjs';
6+
import { catchError, map } from 'rxjs/operators';
77
import { LoginStatus } from '../model/login-status';
88
import { User } from '../model/user';
99

@@ -23,7 +23,8 @@ export class AuthenticationService {
2323
public login(username: string, password: string): Observable<User> {
2424
const toUser = (status: LoginStatus) => this.loadUser(username, password, status);
2525

26-
return this.http.post<Response<LoginStatus>>(this.loginUrl, { username, password })
26+
return this.http.post<ApiResponse<LoginStatus>>(this.loginUrl, { username, password })
27+
.pipe(catchError(this.handleError))
2728
.pipe(map(response => response.content))
2829
.pipe(map(toUser));
2930
}
@@ -52,4 +53,8 @@ export class AuthenticationService {
5253
return this.user;
5354
}
5455

56+
private handleError(error: HttpErrorResponse) {
57+
return throwError(() => new Error(error.message));
58+
}
59+
5560
}

src/app/data/service/data.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3-
import { Response } from '@app/api/model/response';
3+
import { ApiResponse } from '@app/api/model/api-response';
44
import { environment } from '@environments/environment';
55
import { Observable } from 'rxjs';
66
import { map } from 'rxjs/operators';
@@ -14,7 +14,7 @@ export class DataService {
1414
) { }
1515

1616
getData(): Observable<Data[]> {
17-
return this.http.get<Response<Data[]>>(`${environment.apiUrl}/rest/entity/`)
17+
return this.http.get<ApiResponse<Data[]>>(`${environment.apiUrl}/rest/entity/`)
1818
.pipe(map(response => response.content));
1919
}
2020

src/app/login/view/login-view/login-view.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HttpErrorResponse } from '@angular/common/http';
12
import { Component, OnInit } from '@angular/core';
23
import { ActivatedRoute, Router } from '@angular/router';
34
import { AuthenticationService } from '@app/authentication/service/authentication.service';

0 commit comments

Comments
 (0)