From dcd73424896feaf4637ff7b0df0aa1c779232c68 Mon Sep 17 00:00:00 2001 From: Surat Das Date: Thu, 19 Aug 2021 17:52:45 -0700 Subject: [PATCH 1/2] Add clearer error message for unauthorized user Fixes part of the issue: https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker/issues/296 --- src/http-exception.filter.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/http-exception.filter.ts b/src/http-exception.filter.ts index dc9f953d..1a62b4cc 100644 --- a/src/http-exception.filter.ts +++ b/src/http-exception.filter.ts @@ -10,12 +10,13 @@ export class HttpExceptionFilter implements ExceptionFilter { let status: number; const isUniqueConstaintException = this.isUniqueConstraintException(exception); + const isUserAuthorized = this.isUserAuthorized(exception); try { status = isUniqueConstaintException ? HttpStatus.BAD_REQUEST : exception.getStatus(); } catch { status = HttpStatus.INTERNAL_SERVER_ERROR; } - const message = isUniqueConstaintException ? this.getCustomMessageForException(exception) : exception.message; + const message = (isUniqueConstaintException || isUserAuthorized) ? this.getCustomMessageForException(exception) : exception.message; Logger.error(exception, exception.stack); @@ -28,6 +29,10 @@ export class HttpExceptionFilter implements ExceptionFilter { }); } + isUserAuthorized(exception: HttpException): boolean { + return exception.message.includes("Forbidden resource"); + } + isUniqueConstraintException(exception: HttpException): boolean { return exception.message.includes("Unique constraint failed on the fields"); } @@ -36,7 +41,8 @@ export class HttpExceptionFilter implements ExceptionFilter { 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; + : (message.includes("user.create()")) ? "This user already exists." + : (message.includes("Forbidden resource")) ? "You do not have permission to perform this operation." : message; return message; } From 75f12e1a68fdb96fc18531b92d4cda8a354d6ee9 Mon Sep 17 00:00:00 2001 From: Surat Das Date: Thu, 19 Aug 2021 18:04:26 -0700 Subject: [PATCH 2/2] Renamed the method to a more relevant one. --- src/http-exception.filter.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/http-exception.filter.ts b/src/http-exception.filter.ts index 1a62b4cc..ca3fb3c6 100644 --- a/src/http-exception.filter.ts +++ b/src/http-exception.filter.ts @@ -10,13 +10,13 @@ export class HttpExceptionFilter implements ExceptionFilter { let status: number; const isUniqueConstaintException = this.isUniqueConstraintException(exception); - const isUserAuthorized = this.isUserAuthorized(exception); + const isForbiddenException = this.isForbiddenException(exception); try { status = isUniqueConstaintException ? HttpStatus.BAD_REQUEST : exception.getStatus(); } catch { status = HttpStatus.INTERNAL_SERVER_ERROR; } - const message = (isUniqueConstaintException || isUserAuthorized) ? this.getCustomMessageForException(exception) : exception.message; + const message = (isUniqueConstaintException || isForbiddenException) ? this.getCustomMessageForException(exception) : exception.message; Logger.error(exception, exception.stack); @@ -29,7 +29,7 @@ export class HttpExceptionFilter implements ExceptionFilter { }); } - isUserAuthorized(exception: HttpException): boolean { + isForbiddenException(exception: HttpException): boolean { return exception.message.includes("Forbidden resource"); }