Skip to content
Snippets Groups Projects
Commit 40a1281b authored by David Noah Donges's avatar David Noah Donges
Browse files

Merge branch '78-httpservice-error-handling' into 'master'

Resolve "HttpService - Error handling"

Closes #78

See merge request swtp-block-ws17/arsnova-angular-frontend!52
parents dccede73 c6c94cf7
1 merge request!52Resolve "HttpService - Error handling"
Pipeline #13072 passed with stage
in 31 seconds
import { TestBed, inject } from '@angular/core/testing';
import { ErrorHandlingService } from './error-handling.service';
describe('ErrorHandlingService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ErrorHandlingService]
});
});
it('should be created', inject([ErrorHandlingService], (service: ErrorHandlingService) => {
expect(service).toBeTruthy();
}));
});
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
@Injectable()
export class ErrorHandlingService {
constructor() {
}
public handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error);
return of(result as T);
};
}
}
......@@ -2,28 +2,40 @@ import { Injectable } from '@angular/core';
import { Room } from './room';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { catchError, tap } from 'rxjs/operators';
import { ErrorHandlingService } from './error-handling.service';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class RoomService {
export class RoomService extends ErrorHandlingService {
private roomsUrl = 'api/rooms';
constructor(private http: HttpClient) {
super();
}
getRooms(): Observable<Room[]> {
return this.http.get<Room[]>(this.roomsUrl);
return this.http.get<Room[]>(this.roomsUrl).pipe(
tap (_ => ''),
catchError(this.handleError('getRooms', []))
);
}
addRoom(room: Room): Observable<Room> {
return this.http.post<Room>(this.roomsUrl, room, httpOptions);
return this.http.post<Room>(this.roomsUrl, room, httpOptions).pipe(
tap (_ => ''),
catchError(this.handleError<Room>('addRoom'))
);
}
getRoom(id: string): Observable<Room> {
const url = `${this.roomsUrl}/${id}`;
return this.http.get<Room>(url);
return this.http.get<Room>(url).pipe(
tap (_ => ''),
catchError(this.handleError<Room>(`getRoom id=${id}`))
);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment