Skip to content

Commit

Permalink
refactor: Improve error handling in MlabService findServer method
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Lopez committed Oct 2, 2024
1 parent 172169b commit 125980c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 65 deletions.
58 changes: 35 additions & 23 deletions src/app/services/mlab.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class MlabService {
responseObject: any;

constructor(
private http: HttpClient,
private storageSerivce: StorageService) {
private http: HttpClient,
private storageSerivce: StorageService) {
const headersItem = new HttpHeaders({
'Content-Type': 'application/json'
});
Expand All @@ -37,10 +37,10 @@ export class MlabService {
get(key) {
let settings = this.storageSerivce.get("savedSettings");
let settingsret;
if(settings){
if (settings) {
settings = JSON.parse(settings);
settingsret = key ? settings[key] : settings;
}
}
return settingsret;
}

Expand All @@ -49,32 +49,44 @@ export class MlabService {
* @param metroSelection
* @returns server details
*/
findServer(metroSelection) {
findServer(metroSelection, tries = 0) {
let mlabNsUrlApi = this.mlabNsUrlNoPolicy;
if (metroSelection && metroSelection !== "automatic") {
mlabNsUrlApi = this.mlabNsUrlMetro + metroSelection;
}
this.CACHE.type = metroSelection;
console.log(mlabNsUrlApi);

if (this.CACHE.answer) {
return new Observable(observer => {
observer.next(this.CACHE.answer);
});
}
return this.http.get(mlabNsUrlApi)
.pipe(
map((response: any) => {
if(response) {
if (response) {
if (response.city) {
response.label = response.city.replace('_', ' ');
} else {
response.label = '';
}
response.metro = response.site.slice(0, 3);
this.CACHE.answer = response;
return response;
} else {
throw new Error('This request has failed');
}
}),
tap(responseObject => {
if(responseObject){
if(responseObject.city){
responseObject.label = responseObject.city.replace('_', ' ');
} else {
if (tries < 3) {
console.log('Retrying to get server information');
return new Observable(observer => {
setTimeout(() => {
observer.next(this.findServer(metroSelection, tries + 1));
}, 1000);
}
);
} else {
responseObject.label = '';
}
responseObject.metro = responseObject.site.slice(0, 3);
this.CACHE.answer = responseObject;
// console.log(responseObject);
}
throw new Error('This request has failed');
}
}
}),
catchError(this.handleError)
);
Expand All @@ -96,7 +108,7 @@ export class MlabService {
resolve(responseObject);
}),
tap(data => console.log(JSON.stringify(data))),
catchError( async (error) => reject(error))
catchError(async (error) => reject(error))
);
} else {
resolve(this.CACHE.all);
Expand All @@ -109,10 +121,10 @@ export class MlabService {
* @returns metro and location information
*/
findAllServer(): Observable<any[]> {
this.options = {headers: this.headers};
this.options = { headers: this.headers };
return this.http.get(this.mlabNsUrl)
.pipe(
map((response: any) => response ),
map((response: any) => response),
tap(data => {
this.CACHE.all = [];
data.forEach(responseObject => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/starttest/starttest.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<div class="location">{{mlabInformation.label}}</div>
<div class="bottom-layout">
<div *ngIf="currentState == undefined || !onlineStatus" class="tryAgain">
<ion-button class="dailycheck_btn" (click)="measureReady()" [translate]="'startTest.tryAgain'"></ion-button>
<ion-button class="dailycheck_btn" (click)="startNDT()" [translate]="'startTest.tryAgain'"></ion-button>
<div class="h-line"></div>
</div>
</div>
Expand Down
47 changes: 6 additions & 41 deletions src/app/starttest/starttest.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,47 +194,12 @@ export class StarttestPage implements OnInit {
let loadingMsg =
'<div class="loadContent"><ion-img src="assets/loader/loader.gif" class="loaderGif"></ion-img><p class="white">Fetching Internet Provider Info...</p></div>';
this.loading.present(loadingMsg, 15000, 'pdcaLoaderClass', 'null');
this.mlabService
.findServer(this.settingsService.get('metroSelection'))
.subscribe(
(res) => {
this.mlabInformation = res;

this.connectionStatus = 'success';
if (this.loading.isStillLoading()) {
this.loading.dismiss();
}
/*
this.networkService.getAccessInformation().subscribe(results => {
this.accessInformation = results;
if(this.loading.isStillLoading()){
this.loading.dismiss();
}
},(err) => {
if(this.loading.isStillLoading()){
this.loading.dismiss();
}
//this.presentAlertConfirm();
this.connectionStatus = "error";
this.currentRate = "error";
this.isErrorClosed = false;
// this.presentTestFailModal();
});
*/
},
(err) => {
if (this.loading.isStillLoading()) {
this.loading.dismiss();
}
//this.presentAlertConfirm();
this.connectionStatus = 'error';
this.currentRate = 'error';
this.isErrorClosed = false;
// this.presentTestFailModal();
}
);
this.networkService.getNetInfo().then((res) => {
this.connectionStatus = 'success';
if (this.loading.isStillLoading()) {
this.loading.dismiss();
}
});
}

refreshHistory() {
Expand Down

0 comments on commit 125980c

Please sign in to comment.