You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{applyDecorators,ArgumentsHost,Catch,ConflictException,UseFilters}from'@nestjs/common';import{ApiConflictResponse}from'@nestjs/swagger';import{mongo}from'mongoose';import{BaseExceptionFilter}from'@nestjs/core';// TODO move everything to @mean-stream/nestx/** * Converts a MongoServerError with code 11000 into a ConflictException with a configurable message. */
@Catch(mongo.MongoServerError)classConflictExceptionFilterextendsBaseExceptionFilter{constructor(privatedescription: Partial<Record<string,string>>,){super();}catch(exception: mongo.MongoServerError,host: ArgumentsHost): any{if(exception.code!==11000){returnsuper.catch(exception,host);}constkeyPattern=exception.keyPattern;// e.g. { username: 1 }constvalues=exception.keyValue;// e.g. { username: 'test' }constindexKey=Object.keys(keyPattern).join('_');consthttp=newConflictException(this.description[indexKey]??`Unique constraint violation on '${indexKey}' with value ${JSON.stringify(values)}.`);returnsuper.catch(http,host);}}exporttypeIndexKey<T>=keyofT| `${keyofT&string}_${keyofT&string|string}`;/** * Decorator to convert `MongoServerError`s with code 11000 (duplicate key) into a `ConflictException` with a * configurable message. * This adds the following decorators: * - {@link ApiConflictResponse} with a description listing all the possible error messages. * To override the conflict repsponse description, use the {@link ApiConflictResponse} decorator *before* this as * shown in the example. * - {@link UseFilters}({@link ConflictExceptionFilter}) decorators. * * @param descriptions A map of index keys to error messages. * If the index is a compound index, the index key must be a concatenation of the keys in the order they appear in the * index definition, separated by an underscore. * * @example simple unique constraints * // user.schema.ts: * UserSchema.index({ name: 1 }, { unique: true }); * // user.controller.ts * @@UniqueConflict<User>({ name: 'Username is already taken.'}) * * @example compound unique constraints * // member.schema.ts: * MemberSchema.index({ game: 1, user: 1 }, { unique: true }); * // member.controller.ts * @@UniqueConflict<Member>({ game_user: 'User is already a member of this game.' }) * * @example custom API description * // note that the order of decorators is important here * @@ApiConflictResponse({description: 'Game already started or user already joined.'}) * @@UniqueConflict<Member>({ game_user: 'User is already a member of this game.' }) */exportfunctionUniqueConflict<T>(descriptions: Partial<Record<IndexKey<T>,string>>): MethodDecorator{constkeys=Object.keys(descriptions);constdescription=keys.length===1 ? keys[0] : '- '+keys.join('\n- ');returnapplyDecorators(ApiConflictResponse({description}),UseFilters(newConflictExceptionFilter(descriptions)),)}
The text was updated successfully, but these errors were encountered:
const values = exception.keyValue; // e.g. { username: 'test' }
https://github.com/sekassel-research/stp-24-server/blob/e8af372209017213e4cf4ee3baab54d26d59365f/src/util/unique-conflict.decorator.ts#L6
The text was updated successfully, but these errors were encountered: