Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(contexts): add permission error message change #540

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions packages/context/src/lib/context-manager/shared/context.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, Optional } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';

import { BehaviorSubject, Observable, of } from 'rxjs';
import { map, tap, catchError, debounceTime, flatMap } from 'rxjs/operators';
Expand Down Expand Up @@ -93,7 +93,9 @@ export class ContextService {
const url = `${this.baseUrl}/contexts/${id}/details`;
return this.http
.get<DetailedContext>(url)
.pipe(catchError(res => this.handleError(res, id)));
.pipe(catchError(res => {
return this.handleError(res, id)
}));
}

getDefault(): Observable<DetailedContext> {
Expand Down Expand Up @@ -181,16 +183,17 @@ export class ContextService {
contextId: string,
profil: string,
type: TypePermission
): Observable<ContextPermission[]> {
): Observable<ContextPermission[] | Message[]> {
const url = `${this.baseUrl}/contexts/${contextId}/permissions`;
const association = {
profil,
typePermission: type
};
return this.http.post<ContextPermission[]>(
url,
JSON.stringify(association)
);

return this.http.post<ContextPermission[]>(url, JSON.stringify(association))
.pipe(catchError(res => {
return [this.handleError(res, undefined, true)]
}));
}

deletePermissionAssociation(
Expand Down Expand Up @@ -469,19 +472,30 @@ export class ContextService {
return `${basePath}/${file}`;
}

private handleError(res: Response, uri: string): Message[] {
private handleError(error: HttpErrorResponse, uri: string, permissionError?: boolean): Message[] {
const context = this.contexts$.value.ours.find(obj => obj.uri === uri);
const titleContext = context ? context.title : uri;
const titleError = this.languageService.translate.instant(
error.error.title = this.languageService.translate.instant(
'igo.context.contextManager.invalid.title'
);

const textError = this.languageService.translate.instant(
error.error.message = this.languageService.translate.instant(
'igo.context.contextManager.invalid.text',
{ value: titleContext }
);

throw [{ title: titleError, text: textError }];
error.error.toDisplay = true;

if (permissionError) {
error.error.title = this.languageService.translate.instant(
'igo.context.contextManager.errors.addPermissionTitle'
);
error.error.message = this.languageService.translate.instant(
'igo.context.contextManager.errors.addPermission'
);
}

throw error;
}

private handleContextsChange(
Expand Down
4 changes: 3 additions & 1 deletion packages/context/src/locale/en.context.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"uncaught": {
"message": "Sorry, service is currently not available. Please try again later",
"title": "Error server"
}
},
"addPermission": "The permission you are trying to add is already in the context.",
"addPermissionTitle": "Add permission error"
},
"favorite": "Favorite",
"form": {
Expand Down
4 changes: 3 additions & 1 deletion packages/context/src/locale/fr.context.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"uncaught": {
"message": "Désolé! Le service que vous voulez utiliser n’est malheureusement pas disponible pour le moment. Essayez plus tard!",
"title": "Erreur de serveur"
}
},
"addPermission": "La permission que vous tentez d'ajouter est déjà présente dans le contexte.",
"addPermissionTitle": "Erreur d'ajout de permission"
},
"favorite": "Favori",
"form": {
Expand Down