Skip to content

Commit 9383684

Browse files
committed
WEB-307: Fix error when accessing client without profile picture
1 parent 9697d90 commit 9383684

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ export class ClientsViewComponent implements OnInit {
9292
ngOnInit() {
9393
this.clientsService.getClientProfileImage(this.clientViewData.id).subscribe({
9494
next: (base64Image: any) => {
95-
this.clientImage = this._sanitizer.bypassSecurityTrustResourceUrl(base64Image);
96-
},
97-
error: (error: any) => {
98-
// 404 is expected when client has no profile image - not an error
99-
if (error.status === 404) {
100-
this.clientImage = null;
95+
// If base64Image is null, client has no profile image
96+
if (base64Image) {
97+
this.clientImage = this._sanitizer.bypassSecurityTrustResourceUrl(base64Image);
10198
} else {
102-
// Log other unexpected errors
103-
console.error('Error loading client profile image:', error);
10499
this.clientImage = null;
105100
}
101+
},
102+
error: (error: any) => {
103+
// Handle any unexpected errors
104+
console.error('Error loading client profile image:', error);
105+
this.clientImage = null;
106106
}
107107
});
108108
}

src/app/clients/clients.service.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { Injectable } from '@angular/core';
33
import { HttpClient, HttpParams } from '@angular/common/http';
44

55
/** rxjs Imports */
6-
import { Observable } from 'rxjs';
6+
import { Observable, of, throwError } from 'rxjs';
7+
import { map, catchError } from 'rxjs/operators';
78

89
/**
910
* Clients service.
@@ -171,7 +172,23 @@ export class ClientsService {
171172

172173
getClientProfileImage(clientId: string) {
173174
const httpParams = new HttpParams().set('maxHeight', '150');
174-
return this.http.get(`/clients/${clientId}/images`, { params: httpParams, responseType: 'text' });
175+
// Keep it simple since our interceptor will handle the 404 errors
176+
return this.http
177+
.get(`/clients/${clientId}/images`, {
178+
params: httpParams,
179+
responseType: 'text'
180+
})
181+
.pipe(
182+
// Handle the error here and return null when no image is found (404)
183+
catchError((error) => {
184+
if (error.status === 404) {
185+
// Client has no profile image - return null without propagating error
186+
return of(null);
187+
}
188+
// For other errors, rethrow the error
189+
return throwError(() => error);
190+
})
191+
);
175192
}
176193

177194
uploadClientProfileImage(clientId: string, image: File) {

src/app/core/http/error-handler.interceptor.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { environment } from '../../../environments/environment';
1212
/** Custom Services */
1313
import { Logger } from '../logger/logger.service';
1414
import { AlertService } from '../alert/alert.service';
15+
import { TranslateService } from '@ngx-translate/core'; // Added import for TranslateService
1516

1617
/** Initialize Logger */
1718
const log = new Logger('ErrorHandlerInterceptor');
@@ -23,20 +24,24 @@ const log = new Logger('ErrorHandlerInterceptor');
2324
export class ErrorHandlerInterceptor implements HttpInterceptor {
2425
/**
2526
* @param {AlertService} alertService Alert Service.
27+
* @param {TranslateService} translate Translation Service.
2628
*/
27-
constructor(private alertService: AlertService) {}
29+
constructor(
30+
private alertService: AlertService,
31+
private translate: TranslateService // Added TranslateService
32+
) {}
2833

2934
/**
3035
* Intercepts a Http request and adds a default error handler.
3136
*/
3237
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
33-
return next.handle(request).pipe(catchError((error) => this.handleError(error)));
38+
return next.handle(request).pipe(catchError((error) => this.handleError(error, request)));
3439
}
3540

3641
/**
3742
* Error handler.
3843
*/
39-
private handleError(response: HttpErrorResponse): Observable<HttpEvent<any>> {
44+
private handleError(response: HttpErrorResponse, request: HttpRequest<any>): Observable<HttpEvent<any>> {
4045
const status = response.status;
4146
let errorMessage = response.error.developerMessage || response.message;
4247
if (response.error.errors) {
@@ -64,7 +69,17 @@ export class ErrorHandlerInterceptor implements HttpInterceptor {
6469
message: errorMessage || 'You are not authorized for this request!'
6570
});
6671
} else if (status === 404) {
67-
this.alertService.alert({ type: 'Resource does not exist', message: errorMessage || 'Resource does not exist!' });
72+
// Check if this is an image request that should be silently handled (client profile image)
73+
if (request.url.includes('/clients/') && request.url.includes('/images')) {
74+
// Don't show alerts for missing client images
75+
// This is an expected condition, not an error
76+
return new Observable<HttpEvent<any>>();
77+
} else {
78+
this.alertService.alert({
79+
type: this.translate.instant('error.resource.not.found'),
80+
message: errorMessage || 'Resource does not exist!'
81+
});
82+
}
6883
} else if (status === 500) {
6984
this.alertService.alert({
7085
type: 'Internal Server Error',

0 commit comments

Comments
 (0)