|
@@ -1,15 +1,15 @@
|
|
|
-import { Injectable } from "@angular/core";
|
|
|
|
|
-import { HttpClient } from "@angular/common/http";
|
|
|
|
|
|
|
+import { Injectable } from '@angular/core';
|
|
|
|
|
+import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
|
|
|
|
|
|
|
-import { environment } from "@environments/environment";
|
|
|
|
|
-import { User } from "@app/models";
|
|
|
|
|
-import { Observable } from "rxjs/internal/Observable";
|
|
|
|
|
-import { throwError } from "rxjs/internal/observable/throwError";
|
|
|
|
|
-import { map, catchError } from "rxjs/operators";
|
|
|
|
|
|
|
+import { environment } from '@environments/environment';
|
|
|
|
|
+import { User } from '@app/models';
|
|
|
|
|
+import { Observable } from 'rxjs/internal/Observable';
|
|
|
|
|
+import { throwError } from 'rxjs/internal/observable/throwError';
|
|
|
|
|
+import { map, catchError, timeout } from 'rxjs/operators';
|
|
|
|
|
|
|
|
-@Injectable({ providedIn: "root" })
|
|
|
|
|
|
|
+@Injectable({ providedIn: 'root' })
|
|
|
export class UserService {
|
|
export class UserService {
|
|
|
- constructor(private http: HttpClient) {}
|
|
|
|
|
|
|
+ constructor(private http: HttpClient) { }
|
|
|
|
|
|
|
|
getAllUsers() {
|
|
getAllUsers() {
|
|
|
return this.http.get<User[]>(`${environment.productionApiUrl}/users`);
|
|
return this.http.get<User[]>(`${environment.productionApiUrl}/users`);
|
|
@@ -19,12 +19,15 @@ export class UserService {
|
|
|
return this.http.get<User>(`${environment.apiUrl}/users/${id}`);
|
|
return this.http.get<User>(`${environment.apiUrl}/users/${id}`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- getUserById(id: string) {
|
|
|
|
|
|
|
+ getUserById(id: string): Observable<User> {
|
|
|
return this.http
|
|
return this.http
|
|
|
.get<any>(`${environment.productionApiUrl}/customer/${id}`)
|
|
.get<any>(`${environment.productionApiUrl}/customer/${id}`)
|
|
|
.pipe(
|
|
.pipe(
|
|
|
|
|
+ timeout(3000),
|
|
|
map((response) => {
|
|
map((response) => {
|
|
|
- return response;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const user = Object.assign(new User(), response.data.user);
|
|
|
|
|
+ return user;
|
|
|
}),
|
|
}),
|
|
|
catchError(this.errorHandl)
|
|
catchError(this.errorHandl)
|
|
|
);
|
|
);
|
|
@@ -187,15 +190,16 @@ export class UserService {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- errorHandl(error) {
|
|
|
|
|
- let errorMessage = "";
|
|
|
|
|
- if (error.error) {
|
|
|
|
|
- // Get client-side error
|
|
|
|
|
- errorMessage = error.error;
|
|
|
|
|
- } else {
|
|
|
|
|
- // Get server-side error
|
|
|
|
|
- errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
|
|
|
|
|
- }
|
|
|
|
|
- return throwError(errorMessage);
|
|
|
|
|
|
|
+ errorHandl(error: HttpErrorResponse) {
|
|
|
|
|
+ console.log(error.message);
|
|
|
|
|
+ // if (error.error) {
|
|
|
|
|
+ // // Get client-side error
|
|
|
|
|
+ // errorMessage = error.error;
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // // Get server-side error
|
|
|
|
|
+ // errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
|
|
|
|
|
+ // }
|
|
|
|
|
+ // return throwError(errorMessage);
|
|
|
|
|
+ return throwError(error.message || 'Error en el Servidor');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|