Skip to content

Commit

Permalink
Moved error handling of subscriptions inside the subscription (#509)
Browse files Browse the repository at this point in the history
* Moved error handling of subscriptions inside the subscription

* Moved handleError function to its own class and fixed imports

* Fixed a little issue

---------

Co-authored-by: dylan van dijk <[email protected]>
  • Loading branch information
Banshaan and dylan van dijk authored Jul 17, 2024
1 parent b9d3e0d commit 6e1193e
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 272 deletions.
20 changes: 11 additions & 9 deletions src/app/debug/debug-tree/debug-tree.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, EventEmitter, Input, OnDestroy, Output, ViewChild } from '@angular/core';
import { Report } from '../../shared/interfaces/report';
import { HelperService } from '../../shared/services/helper.service';
import { Observable, Subscription } from 'rxjs';
import { Observable, Subscription, catchError, of } from 'rxjs';
import { HttpService } from '../../shared/services/http.service';
import { SettingsService } from '../../shared/services/settings.service';
import {
Expand All @@ -22,6 +22,7 @@ import {
} from '@ng-bootstrap/ng-bootstrap';
import { ButtonComponent } from '../../shared/components/button/button.component';
import { ReportHierarchyTransformer } from '../../shared/classes/report-hierarchy-transformer';
import { ErrorHandling } from 'src/app/shared/classes/error-handling.service';

@Component({
selector: 'app-debug-tree',
Expand Down Expand Up @@ -59,6 +60,7 @@ export class DebugTreeComponent implements OnDestroy {
private helperService: HelperService,
private httpService: HttpService,
private settingsService: SettingsService,
private errorHandler: ErrorHandling,
) {
this.subscribeToSettingsServiceObservables();
}
Expand Down Expand Up @@ -88,11 +90,10 @@ export class DebugTreeComponent implements OnDestroy {
checkUnmatchedCheckpoints(reports: Report[], currentView: any) {
for (let report of reports) {
if (report.storageName === currentView.storageName) {
this.httpService
.getUnmatchedCheckpoints(report.storageName, report.storageId, currentView.name)
.subscribe((unmatched: any) => {
this.hideCheckpoints(unmatched, this.tree.elements.toArray());
});
this.httpService.getUnmatchedCheckpoints(report.storageName, report.storageId, currentView.name).subscribe({
next: (unmatched: any) => this.hideCheckpoints(unmatched, this.tree.elements.toArray()),
error: () => catchError(this.errorHandler.handleError()),
});
}
}
}
Expand All @@ -108,14 +109,15 @@ export class DebugTreeComponent implements OnDestroy {
}

subscribeToSettingsServiceObservables(): void {
this.showMultipleAtATimeSubscription = this.settingsService.showMultipleAtATimeObservable.subscribe(
(value: boolean) => {
this.showMultipleAtATimeSubscription = this.settingsService.showMultipleAtATimeObservable.subscribe({
next: (value: boolean) => {
this.showMultipleAtATime = value;
if (!this.showMultipleAtATime) {
this.removeAllReportsButOne();
}
},
);
error: () => catchError(this.errorHandler.handleError()),
});
}

hideCheckpoints(unmatched: string[], items: TreeItemComponent[]): void {
Expand Down
31 changes: 20 additions & 11 deletions src/app/debug/debug.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Component, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { Report } from '../shared/interfaces/report';
import { Subscription } from 'rxjs';
import { Subscription, catchError } from 'rxjs';
import { DebugReportService } from './debug-report.service';
import { AngularSplitModule } from 'angular-split';
import { TableComponent } from './table/table.component';
import { ReportComponent } from '../report/report.component';
import { ToastService } from '../shared/services/toast.service';
import { HttpService } from '../shared/services/http.service';
import { View } from '../shared/interfaces/view';
import { ErrorHandling } from '../shared/classes/error-handling.service';

@Component({
selector: 'app-debug',
Expand All @@ -29,6 +30,7 @@ export class DebugComponent implements OnInit, OnDestroy {
private debugReportService: DebugReportService,
private httpService: HttpService,
private toastService: ToastService,
private errorHandler: ErrorHandling,
) {}

ngOnInit(): void {
Expand All @@ -42,16 +44,22 @@ export class DebugComponent implements OnInit, OnDestroy {
}

retrieveViews(): void {
this.httpService.getViews().subscribe((views: View[]): void => {
this.views = views;
if (!this.currentView) {
this.currentView = this.views.find((v: View) => v.defaultView);
}
this.httpService.getViews().subscribe({
next: (views: View[]) => {
this.views = views;
if (!this.currentView) {
this.currentView = this.views.find((v: View) => v.defaultView);
}
},
error: () => catchError(this.errorHandler.handleError()),
});
}

subscribeToServices(): void {
this.viewSubscription = this.debugReportService.changeViewObservable.subscribe((view) => (this.currentView = view));
this.viewSubscription = this.debugReportService.changeViewObservable.subscribe({
next: (view) => (this.currentView = view),
error: () => catchError(this.errorHandler.handleError()),
});
}

unsubscribeAll(): void {
Expand All @@ -71,13 +79,14 @@ export class DebugComponent implements OnInit, OnDestroy {

retrieveErrorsAndWarnings(): void {
if (this.currentView) {
this.httpService
.getWarningsAndErrors(this.currentView.storageName)
.subscribe((value: string | undefined): void => {
this.httpService.getWarningsAndErrors(this.currentView.storageName).subscribe({
next: (value: string | undefined): void => {
if (value) {
this.showErrorsAndWarnings(value);
}
});
},
error: () => catchError(this.errorHandler.handleError()),
});
}
}

Expand Down
39 changes: 21 additions & 18 deletions src/app/debug/filter-side-drawer/filter-side-drawer.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { FilterService } from './filter.service';
import { Subscription } from 'rxjs';
import { Subscription, catchError } from 'rxjs';
import { animate, style, transition, trigger } from '@angular/animations';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { FormsModule } from '@angular/forms';
import { TitleCasePipe } from '@angular/common';
import { View } from '../../shared/interfaces/view';
import { Report } from '../../shared/interfaces/report';
import { HttpService } from '../../shared/services/http.service';
import { ErrorHandling } from 'src/app/shared/classes/error-handling.service';

@Component({
standalone: true,
Expand Down Expand Up @@ -42,6 +43,7 @@ export class FilterSideDrawerComponent implements OnDestroy, OnInit {
constructor(
protected filterService: FilterService,
private httpService: HttpService,
private errorHandler: ErrorHandling,
) {}

ngOnInit(): void {
Expand All @@ -54,22 +56,22 @@ export class FilterSideDrawerComponent implements OnDestroy, OnInit {
}

setSubscriptions(): void {
this.shouldShowFilterSubscription = this.filterService.showFilter$.subscribe((show: boolean): void => {
this.shouldShowFilter = show;
this.shouldShowFilterSubscription = this.filterService.showFilter$.subscribe({
next: (show: boolean) => (this.shouldShowFilter = show),
error: () => catchError(this.errorHandler.handleError()),
});
this.metadataLabelsSubscription = this.filterService.metadataLabels$.subscribe((metadataLabels: string[]): void => {
this.metadataLabels = metadataLabels;
this.metadataLabelsSubscription = this.filterService.metadataLabels$.subscribe({
next: (metadataLabels: string[]) => (this.metadataLabels = metadataLabels),
error: () => catchError(this.errorHandler.handleError()),
});
this.currentRecordsSubscription = this.filterService.currentRecords$.subscribe({
next: (records: Map<string, Array<string>>) => (this.currentRecords = records),
error: () => catchError(this.errorHandler.handleError()),
});
this.metadataTypesSubscription = this.filterService.metadataTypes$.subscribe({
next: (metadataTypes: Map<string, string>) => (this.metadataTypes = metadataTypes),
error: () => catchError(this.errorHandler.handleError()),
});
this.currentRecordsSubscription = this.filterService.currentRecords$.subscribe(
(records: Map<string, Array<string>>): void => {
this.currentRecords = records;
},
);
this.metadataTypesSubscription = this.filterService.metadataTypes$.subscribe(
(metadataTypes: Map<string, string>): void => {
this.metadataTypes = metadataTypes;
},
);
}

unsubscribeAll(): void {
Expand All @@ -80,9 +82,10 @@ export class FilterSideDrawerComponent implements OnDestroy, OnInit {
}

getFilterToolTips(): void {
this.httpService
.getUserHelp(this.currentView.storageName, this.currentView.metadataNames)
.subscribe((response: Report) => (this.toolTipSuggestions = response));
this.httpService.getUserHelp(this.currentView.storageName, this.currentView.metadataNames).subscribe({
next: (response: Report) => (this.toolTipSuggestions = response),
error: () => catchError(this.errorHandler.handleError()),
});
}

closeFilter(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { UntypedFormControl, UntypedFormGroup, ReactiveFormsModule } from '@angu
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { HttpService } from '../../../shared/services/http.service';
import { SettingsService } from '../../../shared/services/settings.service';
import { Subscription } from 'rxjs';
import { Subscription, catchError } from 'rxjs';
import { ToastService } from '../../../shared/services/toast.service';
import { UploadParams } from 'src/app/shared/interfaces/upload-params';
import { ToastComponent } from '../../../shared/components/toast/toast.component';
import { ErrorHandling } from 'src/app/shared/classes/error-handling.service';

@Component({
selector: 'app-table-settings-modal',
Expand Down Expand Up @@ -44,6 +45,7 @@ export class TableSettingsModalComponent implements OnDestroy {
private httpService: HttpService,
private settingsService: SettingsService,
private toastService: ToastService,
private errorHandler: ErrorHandling,
) {
this.subscribeToSettingsServiceObservables();
}
Expand All @@ -53,26 +55,33 @@ export class TableSettingsModalComponent implements OnDestroy {
}

subscribeToSettingsServiceObservables(): void {
this.showMultipleAtATimeSubscription = this.settingsService.showMultipleAtATimeObservable.subscribe(
(value: boolean): void => {
this.showMultipleAtATimeSubscription = this.settingsService.showMultipleAtATimeObservable.subscribe({
next: (value: boolean): void => {
this.showMultipleAtATime = value;
this.settingsForm.get('showMultipleFilesAtATime')?.setValue(this.showMultipleAtATime);
},
);
this.tableSpacingSubscription = this.settingsService.tableSpacingObservable.subscribe((value: number): void => {
this.tableSpacing = value;
this.settingsForm.get('tableSpacing')?.setValue(this.tableSpacing);
error: () => catchError(this.errorHandler.handleError()),
});
this.showSearchWindowOnLoadSubscription = this.settingsService.showSearchWindowOnLoadObservable.subscribe(
(value: boolean): void => {
this.tableSpacingSubscription = this.settingsService.tableSpacingObservable.subscribe({
next: (value: number): void => {
this.tableSpacing = value;
this.settingsForm.get('tableSpacing')?.setValue(this.tableSpacing);
},
error: () => catchError(this.errorHandler.handleError()),
});
this.showSearchWindowOnLoadSubscription = this.settingsService.showSearchWindowOnLoadObservable.subscribe({
next: (value: boolean): void => {
this.showSearchWindowOnLoad = value;
this.settingsForm.get('showSearchWindowOnLoad')?.setValue(this.showSearchWindowOnLoad);
},
);

this.prettifyOnLoadSubscription = this.settingsService.prettifyOnLoadObservable.subscribe((value: boolean) => {
this.prettifyOnLoad = value;
this.settingsForm.get('prettifyOnLoad')?.setValue(this.prettifyOnLoad);
error: () => catchError(this.errorHandler.handleError()),
});
this.prettifyOnLoadSubscription = this.settingsService.prettifyOnLoadObservable.subscribe({
next: (value: boolean) => {
this.prettifyOnLoad = value;
this.settingsForm.get('prettifyOnLoad')?.setValue(this.prettifyOnLoad);
},
error: () => catchError(this.errorHandler.handleError()),
});
}

Expand Down Expand Up @@ -122,13 +131,17 @@ export class TableSettingsModalComponent implements OnDestroy {
const form: any = this.settingsForm.value;
localStorage.setItem('generatorEnabled', form.generatorEnabled);
localStorage.setItem('transformationEnabled', form.transformationEnabled.toString());
this.httpService.postTransformation(form.transformation).subscribe();
this.httpService.postTransformation(form.transformation).subscribe({
error: catchError(this.errorHandler.handleError()),
});
const generatorEnabled: string = String(form.generatorEnabled === 'Enabled');
const data: UploadParams = {
generatorEnabled: generatorEnabled,
regexFilter: form.regexFilter,
};
this.httpService.postSettings(data).subscribe();
this.httpService.postSettings(data).subscribe({
error: catchError(this.errorHandler.handleError()),
});

this.toastService.showWarning('Reopen report to see updated XML');
this.saving = true;
Expand All @@ -141,20 +154,28 @@ export class TableSettingsModalComponent implements OnDestroy {
factoryReset(): void {
this.settingsForm.reset();
this.settingsService.setShowMultipleAtATime();
this.httpService.resetSettings().subscribe((response) => this.saveResponseSetting(response));
this.httpService.getTransformation(true).subscribe((resp) => {
this.settingsForm.get('transformation')?.setValue(resp.transformation);
this.httpService.resetSettings().subscribe({
next: (response) => this.saveResponseSetting(response),
error: () => catchError(this.errorHandler.handleError()),
});
this.httpService.getTransformation(true).subscribe({
next: (res) => this.settingsForm.get('transformation')?.setValue(res.transformation),
error: () => catchError(this.errorHandler.handleError()),
});
}

loadSettings(): void {
this.httpService.getSettings().subscribe((response) => this.saveResponseSetting(response));
this.httpService.getSettings().subscribe({
next: (response) => this.saveResponseSetting(response),
error: () => catchError(this.errorHandler.handleError()),
});
if (localStorage.getItem('transformationEnabled')) {
this.settingsForm.get('transformationEnabled')?.setValue(localStorage.getItem('transformationEnabled') == 'true');
}
this.httpService
.getTransformation(false)
.subscribe((response) => this.settingsForm.get('transformation')?.setValue(response.transformation));
this.httpService.getTransformation(false).subscribe({
next: (response) => this.settingsForm.get('transformation')?.setValue(response.transformation),
error: () => catchError(this.errorHandler.handleError()),
});
}

saveResponseSetting(response: any): void {
Expand Down
Loading

0 comments on commit 6e1193e

Please sign in to comment.