Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/http-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@ export class HttpExceptionFilter implements ExceptionFilter {
const request = ctx.getRequest<Request>();

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;
}

}