From 912520279883431d66fa472d39b8d0526f7eb95e Mon Sep 17 00:00:00 2001 From: Surat Das Date: Mon, 15 Feb 2021 20:54:47 -0800 Subject: [PATCH 1/2] Issue #203 : implemented user friendly error messages. --- src/http-exception.filter.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/http-exception.filter.ts b/src/http-exception.filter.ts index b04f5a1a..8d279132 100644 --- a/src/http-exception.filter.ts +++ b/src/http-exception.filter.ts @@ -9,10 +9,19 @@ export class HttpExceptionFilter implements ExceptionFilter { const request = ctx.getRequest(); let status: number; - try { - status = exception.getStatus(); - } catch { - status = HttpStatus.INTERNAL_SERVER_ERROR; + let message = exception.message; + if (message.includes("Unique constraint failed on the fields")) { + message = (message.includes("build.update()")) ? "There is already a build with this ci build id." + : (message.includes("project.create()")) ? "Project exists with this name." + : (message.includes("user.create()")) ? "This user already exists." : message; + + status = HttpStatus.BAD_REQUEST + } else { + try { + status = exception.getStatus(); + } catch { + status = HttpStatus.INTERNAL_SERVER_ERROR; + } } Logger.error(exception, exception.stack); @@ -20,7 +29,7 @@ export class HttpExceptionFilter implements ExceptionFilter { response.status(status).json({ path: request.url, name: exception.name, - message: exception.message, + message: message, exception: exception, stack: exception.stack, }); From d46a381cfa5044ce79090d518f2b3ca251f246cb Mon Sep 17 00:00:00 2001 From: Surat Das Date: Wed, 17 Feb 2021 23:34:21 -0800 Subject: [PATCH 2/2] Fixed review comments. --- src/http-exception.filter.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/http-exception.filter.ts b/src/http-exception.filter.ts index 8d279132..dc9f953d 100644 --- a/src/http-exception.filter.ts +++ b/src/http-exception.filter.ts @@ -9,20 +9,13 @@ export class HttpExceptionFilter implements ExceptionFilter { const request = ctx.getRequest(); let status: number; - let message = exception.message; - if (message.includes("Unique constraint failed on the fields")) { - message = (message.includes("build.update()")) ? "There is already a build with this ci build id." - : (message.includes("project.create()")) ? "Project exists with this name." - : (message.includes("user.create()")) ? "This user already exists." : message; - - status = HttpStatus.BAD_REQUEST - } else { - try { - status = exception.getStatus(); - } catch { - status = HttpStatus.INTERNAL_SERVER_ERROR; - } + const isUniqueConstaintException = this.isUniqueConstraintException(exception); + try { + status = isUniqueConstaintException ? HttpStatus.BAD_REQUEST : exception.getStatus(); + } catch { + status = HttpStatus.INTERNAL_SERVER_ERROR; } + const message = isUniqueConstaintException ? this.getCustomMessageForException(exception) : exception.message; Logger.error(exception, exception.stack); @@ -34,4 +27,17 @@ export class HttpExceptionFilter implements ExceptionFilter { stack: exception.stack, }); } + + isUniqueConstraintException(exception: HttpException): boolean { + return exception.message.includes("Unique constraint failed on the fields"); + } + + getCustomMessageForException(exception: HttpException): string { + let message = exception.message; + message = (message.includes("build.update()")) ? "There is already a build with this ci build id." + : (message.includes("project.create()")) ? "Project exists with this name." + : (message.includes("user.create()")) ? "This user already exists." : message; + return message; + } + }