Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/components/filter/FilterCreationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { MAX_CHAR_DESCRIPTION } from '../../utils/constants/uiConstants';
import { EXPERT_FILTER_QUERY } from './expert/expertFilterConstants';
import { FILTER_EQUIPMENTS_ATTRIBUTES } from './explicitNaming/ExplicitNamingFilterConstants';
import { GsLang } from '../../utils';
import { CustomError } from '../../services';
import { CustomError, formatMessageValues } from '../../services';

const emptyFormData = {
[FieldConstants.NAME]: '',
Expand Down Expand Up @@ -116,6 +116,9 @@ export function FilterCreationDialog({
if (error instanceof CustomError && error.businessErrorCode != null) {
snackError({
messageId: error.businessErrorCode,
messageValues: error.businessErrorValues
? formatMessageValues(error.businessErrorValues)
: undefined,
});
} else {
snackError({
Expand Down
5 changes: 4 additions & 1 deletion src/components/filter/expert/ExpertFilterEditionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { saveExpertFilter } from '../utils/filterApi';
import { expertFilterSchema } from './ExpertFilterForm';
import { importExpertRules } from './expertFilterUtils';
import { HeaderFilterSchema } from '../HeaderFilterForm';
import { catchErrorHandler, CustomError } from '../../../services';
import { catchErrorHandler, CustomError, formatMessageValues } from '../../../services';
import { EXPERT_FILTER_QUERY } from './expertFilterConstants';

const formSchema = yup
Expand Down Expand Up @@ -102,6 +102,9 @@ export function ExpertFilterEditionDialog({
if (error instanceof CustomError && error.businessErrorCode != null) {
snackError({
messageId: error.businessErrorCode,
messageValues: error.businessErrorValues
? formatMessageValues(error.businessErrorValues)
: undefined,
headerId: 'cannotSaveFilter',
});
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/services/businessErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ export class CustomError extends Error {

businessErrorCode?: string;

constructor(message: string, status: number, businessErrorCode?: string) {
businessErrorValues?: Record<string, unknown>;

constructor(message: string, status: number, businessErrorCode?: string, properties?: Record<string, unknown>) {
super(message);
this.status = status;
this.businessErrorCode = businessErrorCode;
this.businessErrorValues = properties;
}
}

export function formatMessageValues(properties: Record<string, unknown>): Record<string, string> {
return Object.fromEntries(
Object.entries(properties).map(([key, value]) => [key, typeof value === 'object' ? JSON.stringify(value) : ''])
);
}
2 changes: 1 addition & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export * from './study-config';
export * from './userAdmin';
export * from './voltage-init';
export * from './security-analysis';
export { CustomError } from './businessErrorCode';
export { CustomError, formatMessageValues } from './businessErrorCode';
8 changes: 8 additions & 0 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ const handleError = (response: Response) => {
const errorName = 'HttpResponseError : ';
const errorJson = parseError(text);
let customError: CustomError;
if (errorJson?.businessErrorCode != null && errorJson?.businessErrorValues != null) {
throw new CustomError(
errorJson.message,
errorJson.status,
errorJson.businessErrorCode,
errorJson.businessErrorValues
);
}
if (errorJson?.businessErrorCode != null) {
throw new CustomError(errorJson.message, errorJson.status, errorJson.businessErrorCode);
}
Expand Down
2 changes: 1 addition & 1 deletion src/translations/en/businessErrorsEn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const businessErrorsEn = {
'useradmin.groupAlreadyExists': 'User group already exists.',
'useradmin.announcementInvalidPeriod': 'The announcement has an invalid time period.',
'useradmin.announcementOverlap': 'The announcement period overlaps with an existing one.',
'filter.filterCycleDetected': 'Filter cycle detected.',
'filter.filterCycleDetected': 'Filter cycle detected between: {filters}',
};
2 changes: 1 addition & 1 deletion src/translations/fr/businessErrorsFr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const businessErrorsFr = {
'useradmin.groupAlreadyExists': "Le groupe d'utilisateurs existe déjà.",
'useradmin.announcementInvalidPeriod': "La période de l'annonce est invalide.",
'useradmin.announcementOverlap': "La période de l'annonce chevauche une autre annonce existante.",
'filter.filterCycleDetected': 'Cycle de filtre détecté.',
'filter.filterCycleDetected': 'Cycle de filtre détecté entre : {filters}',
};
Loading