Skip to content

Commit

Permalink
chore: implement new handle error service
Browse files Browse the repository at this point in the history
* Implements the new handle error service.
* Personalizes the error message for the document import.
* Closes rero/rero-ils#3564.

Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr authored and PascalRepond committed Jan 16, 2024
1 parent 2b1206b commit c10e944
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
9 changes: 7 additions & 2 deletions projects/admin/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client';
import { TranslateLoader as BaseTranslateLoader, TranslateModule } from '@ngx-translate/core';
import {
BucketNameService as CoreBucketNameService,
CoreConfigService, LocalStorageService, RecordModule, RemoteTypeaheadService,
CoreConfigService,
RecordHandleErrorService as CoreRecordHandleErrorService,
LocalStorageService,
RecordModule, RemoteTypeaheadService,
TranslateLoader, TranslateService, TruncateTextPipe
} from '@rero/ng-core';
import { ItemHoldingsCallNumberPipe, MainTitlePipe, SharedModule, UserService } from '@rero/shared';
Expand Down Expand Up @@ -198,6 +201,7 @@ import { AppConfigService } from './service/app-config.service';
import { AppInitializerService } from './service/app-initializer.service';
import { BucketNameService } from './service/bucket-name.service';
import { OrganisationService } from './service/organisation.service';
import { RecordHandleErrorService } from './service/record.handle-error.service';
import { TypeaheadFactoryService, typeaheadToken } from './service/typeahead-factory.service';
import { UiRemoteTypeaheadService } from './service/ui-remote-typeahead.service';
import { PreviewEmailModule } from './shared/preview-email/preview-email.module';
Expand Down Expand Up @@ -457,7 +461,8 @@ export function appInitFactory(appInitializerService: AppInitializerService): ()
MainTitlePipe,
ItemHoldingsCallNumberPipe,
CountryCodeTranslatePipe,
{ provide: CoreBucketNameService, useClass: BucketNameService }
{ provide: CoreBucketNameService, useClass: BucketNameService },
{ provide: CoreRecordHandleErrorService, useClass: RecordHandleErrorService }
],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { RecordService } from '@rero/ng-core';
import { Record } from '@rero/ng-core/lib/record/record';
import { Record, RecordService } from '@rero/ng-core';
import { ToastrService } from 'ngx-toastr';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
Expand Down
2 changes: 1 addition & 1 deletion projects/admin/src/app/routes/import-documents-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ImportDocumentsRoute extends BaseRoute implements RouteInterface {
* @returns observable of the string representation of the number of results.
*/
getResultsText(hits: any): Observable<string> {
const total = this._routeToolService.recordService.totalHits(hits.total);
const total = this._routeToolService.recordService.totalHits(hits.total) || 0;
return (total === 0)
? this._translateService.stream('no result')
: this._translateService.stream('{{ total }} results of {{ remoteTotal }}', {
Expand Down
65 changes: 65 additions & 0 deletions projects/admin/src/app/service/record.handle-error.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* RERO ILS UI
* Copyright (C) 2020-2023 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { RecordHandleErrorService as CoreRecordHandleErrorService } from '@rero/ng-core';
import { NgxSpinnerService } from 'ngx-spinner';
import { ToastrService } from 'ngx-toastr';
import { NEVER, Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class RecordHandleErrorService extends CoreRecordHandleErrorService {

/**
* Constructor
* @param translateService - TranslateService
* @param toastrService - ToastrService
* @param spinner - NgxSpinnerService
*/
constructor(
protected translateService: TranslateService,
private toastrService: ToastrService,
private spinner: NgxSpinnerService
) {
super(translateService);
}

/**
* Http handle Error
* @param error - Http Error Response
* @param resourceName - Resource name
* @returns Observable
*/
handleError(error: HttpErrorResponse, resourceName?: string): Observable<never> {
if (resourceName.startsWith('import_')) {
this.toastrService.error(
this.translateService.instant(
'Your request to the external server has failed. Try again later ({{ statusCode }}).', {
statusCode: error.status
}),
this.translateService.instant('Import from the web')
);
this.spinner.hide();
return NEVER;
} else {
return this.standardHandleError(error);
}
}
}

0 comments on commit c10e944

Please sign in to comment.