From 9bcb35e3f5fb0ce2bd6e66c1a31b143682afc672 Mon Sep 17 00:00:00 2001 From: "CORP\\Nigel.Palmer" Date: Tue, 10 Dec 2024 15:42:15 +0000 Subject: [PATCH] gh-441: Provide more useful error message when there is a data specification naming clash --- .../data-specification.service.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/app/data-explorer/data-specification.service.ts b/src/app/data-explorer/data-specification.service.ts index c0ba07be..02c2847a 100644 --- a/src/app/data-explorer/data-specification.service.ts +++ b/src/app/data-explorer/data-specification.service.ts @@ -625,9 +625,13 @@ export class DataSpecificationService { of(dataElements), ]); }), - catchError((error) => { + catchError((error: HttpErrorResponse) => { + const errorMessage = this.isDataSpecificationNameClashError(error) + ? 'A data specification with this name already exists. Please choose a different name.' + : error.message; + this.toastr.error( - `There was a problem creating your data specification. ${error}`, + `There was a problem creating your data specification. ${errorMessage}`, 'Data specification creation error' ); return EMPTY; @@ -863,4 +867,14 @@ export class DataSpecificationService { }) ); } + + private isDataSpecificationNameClashError(error: HttpErrorResponse) { + if (error.status === 422 && error.error && Array.isArray(error.error.errors)) { + // Extract the first error message + return error.error.errors.some((err: { message: string }) => + err.message?.includes('must be unique by branch name') + ); + } + return false; + } }