diff --git a/src/http-exception.filter.ts b/src/http-exception.filter.ts index b04f5a1a..dc9f953d 100644 --- a/src/http-exception.filter.ts +++ b/src/http-exception.filter.ts @@ -9,20 +9,35 @@ export class HttpExceptionFilter implements ExceptionFilter { const request = ctx.getRequest(); let status: number; + const isUniqueConstaintException = this.isUniqueConstraintException(exception); try { - status = exception.getStatus(); + 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); response.status(status).json({ path: request.url, name: exception.name, - message: exception.message, + message: message, exception: exception, 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; + } + }