From b0ceed0e5b9bb68f671a84787a2d81e09c7e9cf5 Mon Sep 17 00:00:00 2001 From: aloatias Date: Fri, 8 May 2020 20:43:05 +0200 Subject: [PATCH] Feature/handle service errors (#9) * wip * wip * Handle 500 erros * format --- src/app/Shared/BaseUrl.ts | 2 +- src/app/task.service.ts | 55 ++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/app/Shared/BaseUrl.ts b/src/app/Shared/BaseUrl.ts index befd644..cbf8cbb 100644 --- a/src/app/Shared/BaseUrl.ts +++ b/src/app/Shared/BaseUrl.ts @@ -5,5 +5,5 @@ export class Urls { public static TASK_GETALL = environment.url + Global.TASK_ENDPOINT + "GetAll"; public static TASK_ADD = environment.url + Global.TASK_ENDPOINT + "Add"; public static TASK_UPDATESTATUS = environment.url + Global.TASK_ENDPOINT + "UpdateStatus"; - public static TASK_DELETE = environment.url + Global.TASK_ENDPOINT + "Delete?taskId="; + public static TASK_DELETE = environment.url + Global.TASK_ENDPOINT + "Delete"; } \ No newline at end of file diff --git a/src/app/task.service.ts b/src/app/task.service.ts index 5ac76e9..5e9029c 100644 --- a/src/app/task.service.ts +++ b/src/app/task.service.ts @@ -1,13 +1,15 @@ import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs'; +import { Observable, throwError } from 'rxjs'; import { Task } from './Dtos/Task'; -import { HttpClient, HttpHeaders, HttpParams, HttpRequest } from '@angular/common/http'; +import { HttpClient, HttpHeaders, HttpParams, HttpRequest, HttpErrorResponse } from '@angular/common/http'; import { Urls } from './Shared/BaseUrl'; +import { retry, catchError } from 'rxjs/operators'; const httpOptions = { headers: new HttpHeaders({ "Content-Type": "application/json" - }) + }), + params: new HttpParams() }; @Injectable({ @@ -18,21 +20,50 @@ export class TaskService { constructor( private _http: HttpClient) { } - - AddTask(taskDescription: string) : Observable { - return this._http.post(Urls.TASK_ADD, { "description": taskDescription }, httpOptions); + + AddTask(taskDescription: string): Observable { + return this._http + .post(Urls.TASK_ADD, { "description": taskDescription }, httpOptions) + .pipe(catchError(this.handleError)); } DeleteTask(taskId: string) { - let url = Urls.TASK_DELETE + taskId; - return this._http.delete(url, httpOptions); + let url = Urls.TASK_DELETE; + httpOptions.params = httpOptions.params.set("taskId", taskId); + + return this._http + .delete(url, httpOptions) + .pipe(catchError(this.handleError)); } - GetAllTasks() : Observable { - return this._http.get(Urls.TASK_GETALL); + GetAllTasks(): Observable { + return this._http + .get(Urls.TASK_GETALL) + .pipe(catchError(this.handleError)); } - + UpdateTaskStatus(taskId: string): Observable { - return this._http.put(Urls.TASK_UPDATESTATUS, { "id": taskId }, httpOptions); + return this._http + .put(Urls.TASK_UPDATESTATUS, { "id": taskId }, httpOptions) + .pipe(catchError(this.handleError)); + } + + private handleError(error: HttpErrorResponse) { + let errorMessage = ''; + if (error.error instanceof ErrorEvent) { + // client-side error + errorMessage = `Error: ${error.error.message}`; + } else { + // server-side error + if (error.status !== 500) { + errorMessage = error.error; + } else { + errorMessage = "Un error occured. Please try again later"; + } + } + + alert(errorMessage); + + return throwError(errorMessage); } }