Skip to content

Commit

Permalink
fix(utils/filters/mongo-unique-filter): only throw on duplicate field…
Browse files Browse the repository at this point in the history
…s error
  • Loading branch information
Haltarys authored Oct 4, 2023
1 parent ec43d83 commit 1ce44c1
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/utils/filters/mongo-unique-field.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export class MongoUniqueFieldFilter implements ExceptionFilter {
catch(exception: MongoServerError, host: ArgumentsHost) {
const next = host.switchToHttp().getNext();

if (exception.code !== 11000) next(exception);

const fields = Object.keys(exception.keyPattern).join('", "');
next(new BadRequestException(`Unique fields "${fields}" already in use.`));
if (exception.code === 11000) {
const fields = Object.keys(exception.keyPattern).join('", "');
next(new BadRequestException(`Unique fields "${fields}" already in use.`));
} else {
next(exception);
}
}
}

0 comments on commit 1ce44c1

Please sign in to comment.