Skip to content

Commit

Permalink
#1 better handling failures in guards
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Aug 12, 2019
1 parent 9e73124 commit 4bb52ad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/app/language.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Observable } from 'rxjs';
import { I18nService } from './shared/i18n.service';
import { EventService } from './shared/event.service';
import { TranslateService } from '@ngx-translate/core';
import { map, switchMap } from 'rxjs/operators';
import { map, switchMap, catchError } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -34,15 +34,15 @@ export class LanguageGuard implements CanActivate {
}

private getForEvent(eventShortName: string): Observable<string[]> {
return this.eventService.getAvailableLanguageForEvent(eventShortName);
return this.eventService.getAvailableLanguageForEvent(eventShortName).pipe(catchError(val => this.getForApp()));
}

private getForApp(): Observable<string[]> {
return this.i18nService.getAvailableLanguages().pipe(map(languages => languages.map(l => l.locale)));
}

private extractLang(availableLanguages: string[], persisted: string, override: string): string {
let lang;
let lang: string;
if (override && availableLanguages.indexOf(override) >= 0) {
lang = override;
} else if (availableLanguages.indexOf(persisted) >= 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LocalizedCountry } from '../model/localized-country';
import { Title } from '@angular/platform-browser';
import { TranslateService, TranslateLoader } from '@ngx-translate/core';
import { Router, NavigationStart } from '@angular/router';
import { map, mergeMap, shareReplay } from 'rxjs/operators';
import { map, mergeMap, shareReplay, catchError } from 'rxjs/operators';
import { EventService } from './event.service';

@Injectable({
Expand Down Expand Up @@ -74,7 +74,7 @@ export class I18nService {
}

useTranslation(eventShortName: string, lang: string): Observable<boolean> {
const overrideBundle = eventShortName ? this.eventService.getEvent(eventShortName).pipe(map(e => e.i18nOverride[lang] || {})) : of({});
const overrideBundle = eventShortName ? this.eventService.getEvent(eventShortName).pipe(catchError(e => of({i18nOverride: {}})), map(e => e.i18nOverride[lang] || {})) : of({});
return zip(this.customLoader.getTranslation(lang), overrideBundle).pipe(mergeMap(([root, override]) => {
this.translateService.setTranslation(lang, root, false);
this.translateService.setTranslation(lang, override, true);
Expand Down

0 comments on commit 4bb52ad

Please sign in to comment.