-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mongo error handling and a new field for project
- Loading branch information
1 parent
d0a91e9
commit 300b0be
Showing
7 changed files
with
159 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ArgumentsHost, Catch, HttpStatus, Logger } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
import { Response } from 'express'; | ||
import { MongoError } from 'mongodb'; | ||
|
||
@Catch(MongoError) | ||
export class MongoExceptionFilter extends BaseExceptionFilter { | ||
private readonly logger = new Logger(MongoExceptionFilter.name); | ||
|
||
catch(exception: MongoError, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
this.logger.error(exception.message); | ||
|
||
let status; | ||
switch (exception.name) { | ||
case 'DocumentNotFoundError': { | ||
status = HttpStatus.NOT_FOUND; | ||
response.status(status).json({ | ||
statusCode: HttpStatus.NOT_FOUND, | ||
message: 'Not Found', | ||
}); | ||
|
||
break; | ||
} | ||
case 'MongooseError': | ||
case 'CastError': | ||
case 'DisconnectedError': | ||
case 'DivergentArrayError': | ||
case 'MissingSchemaError': | ||
case 'ValidatorError': | ||
case 'ValidationError': | ||
case 'ObjectExpectedError': | ||
case 'ObjectParameterError': | ||
case 'OverwriteModelError': | ||
case 'ParallelSaveError': | ||
case 'StrictModeError': | ||
case 'VersionError': | ||
case 'MongooseError': | ||
default: { | ||
super.catch(exception, host); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Flag, FlagDocument } from './schemas/flag.schema'; | ||
import { Model, Types } from 'mongoose'; | ||
import FlagDto from './flag.dts'; | ||
|
||
@Injectable() | ||
class FlagsRepository { | ||
private readonly logger = new Logger(FlagsRepository.name); | ||
|
||
constructor( | ||
@InjectModel(Flag.name) | ||
private db: Model<FlagDocument>, | ||
) {} | ||
|
||
private formatFlags(flags: FlagDocument[]): FlagDto[] { | ||
return flags.map( | ||
({ _id, name, type, value, environment, project, isEnabled }) => ({ | ||
id: _id as Types.ObjectId, | ||
name, | ||
type, | ||
value, | ||
environment, | ||
project, | ||
isEnabled, | ||
}), | ||
); | ||
} | ||
|
||
async create(flag: FlagDto): Promise<FlagDto> { | ||
const newFlag = new this.db(flag); | ||
const f = await newFlag.save(); | ||
|
||
return this.formatFlags([f])[0]; | ||
} | ||
|
||
async get(): Promise<FlagDto[]> { | ||
const fs = await this.db.find(); | ||
|
||
return this.formatFlags(fs); | ||
} | ||
|
||
async toggleEnabled(id: string, isEnabled: boolean): Promise<FlagDto> { | ||
this.db.findById(id); | ||
const f = await this.db | ||
.findByIdAndUpdate( | ||
{ _id: new Types.ObjectId(id) }, | ||
{ isEnabled }, | ||
{ new: true }, | ||
) | ||
.orFail(); | ||
|
||
return this.formatFlags([f])[0]; | ||
} | ||
|
||
delete(id: string): void { | ||
this.db.deleteOne({ _id: new Types.ObjectId(id) }); | ||
} | ||
} | ||
|
||
export default FlagsRepository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters