Skip to content

Commit

Permalink
Implements a custom injector and handles http errors (#1081) (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielInTheWorld committed Jun 8, 2022
1 parent b71a0a4 commit 23991a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
4 changes: 3 additions & 1 deletion client/src/app/gateways/actions/action.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ export class ActionService {
throw new Error(`The action service did not return responses for each request.`);
}
return results[0] as T[];
} else if (response !== null) {
throw new Error(`Unknown return type from action service`);
}
throw new Error(`Unknown return type from action service`);
return null;
}

public create<T>(...requests: ActionRequest[]): Action<T> {
Expand Down
14 changes: 12 additions & 2 deletions client/src/app/gateways/http.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { TranslateService } from '@ngx-translate/core';
import { firstValueFrom, Observable } from 'rxjs';

import {
Expand All @@ -10,6 +12,7 @@ import {
ResponseType
} from '../infrastructure/definitions/http';
import { ProcessError } from '../infrastructure/errors';
import { OpenSlidesInjector } from '../infrastructure/utils/di/openslides-injector';
import { toBase64 } from '../infrastructure/utils/functions';

const defaultHeaders = { [`Content-Type`]: `application/json` };
Expand Down Expand Up @@ -56,7 +59,14 @@ export class HttpService {
const response = await firstValueFrom(this.getObservableFor<HttpResponse<T>>(method, url, options));
return response?.body as T;
} catch (error) {
throw new ProcessError(error);
if (error instanceof HttpErrorResponse) {
const snackBar = OpenSlidesInjector.get(MatSnackBar);
const translate = OpenSlidesInjector.get(TranslateService);
snackBar.open(`${translate.instant(`Error`)}: ${error.error.message}`, `Ok`);
return null;
} else {
throw new ProcessError(error);
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions client/src/app/infrastructure/utils/di/openslides-injector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injector, ProviderToken } from '@angular/core';

import { Deferred } from '../promises';

interface InjectOptions {
defaultValue?: any;
}

export class OpenSlidesInjector {
public static get initialized(): Promise<void> {
return this._initialized;
}

private static _injector: Injector | null = null;
private static _initialized = new Deferred();

public static setInjector(injector: Injector): void {
this._injector = injector;
this._initialized.resolve();
}

public static get<T>(token: ProviderToken<T>, { defaultValue }: InjectOptions = {}): T {
return this._injector.get(token, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, Injector, Type } from '@angular/core';
import { BaseRepository } from 'src/app/gateways/repositories/base-repository';
import { AppConfig } from 'src/app/infrastructure/definitions/app-config';
import { OnAfterAppsLoaded } from 'src/app/infrastructure/definitions/hooks/after-apps-loaded';
import { OpenSlidesInjector } from 'src/app/infrastructure/utils/di/openslides-injector';
import { AgendaAppConfig } from 'src/app/site/pages/meetings/pages/agenda/agenda.config';
import { AssignmentsAppConfig } from 'src/app/site/pages/meetings/pages/assignments/assignments.config';
import { ChatAppConfig } from 'src/app/site/pages/meetings/pages/chat/chat.config';
Expand Down Expand Up @@ -60,7 +61,9 @@ export class AppLoadService {
private modelMapper: CollectionMapperService,
private mainMenuService: MainMenuService,
private fallbackRoutesService: FallbackRoutesService
) {}
) {
OpenSlidesInjector.setInjector(injector);
}

public async loadApps(): Promise<void> {
const repositories: OnAfterAppsLoaded[] = [];
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/site/base/base.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export abstract class BaseComponent implements OnDestroy {
* @param message The message to show or an "real" error, which will be passed to the console.
*/
public raiseError = (message: any): void => {
console.log(`raiseError`, message);
console.debug(`raiseError`, message);
let errorNotification: string;
if (message instanceof Error) {
if (message.message) {
Expand All @@ -168,7 +168,7 @@ export abstract class BaseComponent implements OnDestroy {
* This snack bar will only dismiss if the user clicks the 'OK'-button.
*/
protected raiseWarning = (message: string): void => {
// this.messageSnackBar = this.matSnackBar.open(message, this.translate.instant(`OK`));
this.messageSnackBar = this.matSnackBar.open(message, this.translate.instant(`OK`));
};

/**
Expand Down

0 comments on commit 23991a0

Please sign in to comment.