From 8376734ee3a58edc960cb41663de5cc52c760f5b Mon Sep 17 00:00:00 2001 From: Ronan Date: Mon, 16 Oct 2017 11:43:40 +0200 Subject: Handle express-validator error on the client side and fix #96 (#98) * Handle express-validator error on the client side * More meaningfull error for not supported format --- client/src/app/shared/rest/rest-extractor.service.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'client/src/app/shared') diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts index aafc9723e..b560e2fe4 100644 --- a/client/src/app/shared/rest/rest-extractor.service.ts +++ b/client/src/app/shared/rest/rest-extractor.service.ts @@ -45,8 +45,19 @@ export class RestExtractor { errorMessage = err.error.message console.error('An error occurred:', errorMessage) } else if (err.status !== undefined) { - const body = err.error - errorMessage = body ? body.error : 'Unknown error.' + // A server-side error occurred. + // TODO: remove when angular/angular#19477 (comment) is fixed + let body = JSON.parse(err.error) + if (body) { + if (body.errors) { + const errors = body.errors + const error = errors[Object.keys(errors)[0]] + errorMessage = error.msg // Take the message of the first error + } else if (body.error) { + errorMessage = body.error + } + } + errorMessage = errorMessage ? errorMessage : 'Unknown error.' console.error(`Backend returned code ${err.status}, body was: ${errorMessage}`) } else { errorMessage = err -- cgit v1.2.3