Skip to content

Commit

Permalink
fix(contexts): add permission error message change (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeLafreniere18 authored and mbarbeau committed Jan 6, 2020
1 parent 0ae5342 commit 4710037
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
36 changes: 25 additions & 11 deletions packages/context/src/lib/context-manager/shared/context.service.ts
@@ -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 @@ -471,19 +474,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
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
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

0 comments on commit 4710037

Please sign in to comment.