Skip to content

Commit

Permalink
Custom exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwas1 committed Jan 15, 2023
1 parent e0b23fb commit bd6959b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/app-auth/controllers/app-auth/app-auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Controller, Get, ValidationPipe, Post, UsePipes, Body, Req, Res, HttpStatus } from '@nestjs/common';
import { Controller, Get, ValidationPipe, Post, UsePipes, Body, Req, Res, HttpStatus, Param} from '@nestjs/common';
import { CreateAppDto } from 'src/app-auth/dtos/CreateApp.dto';
import { AppAuthService } from 'src/app-auth/services/app-auth/app-auth.service';
import { ApiBadRequestResponse, ApiCreatedResponse, ApiResponse, ApiTags } from '@nestjs/swagger';
import { IApp } from '../../types/App.types';
import { AppSchema } from '../../schemas/App.schema';
import { AppNotFoundException } from 'src/app-auth/exceptions/AppNotFound.exception';

@ApiTags('App')
@Controller('app')
Expand All @@ -15,10 +16,17 @@ export class AppAuthController {
description: 'List of apps',
type: [AppSchema]
})
getAppAuth() {
getApps() {
return this.appAuthService.getAllApps();
}

@Get(':appId')
getAppById(@Param('appId') appId: string): IApp {
const app = this.appAuthService.getAppById(appId);
if(app) return app;
else throw new AppNotFoundException(); // Custom Exception handling
}

@Post('/register')
@ApiCreatedResponse({
description: 'Newly created app',
Expand Down
7 changes: 7 additions & 0 deletions src/app-auth/exceptions/AppNotFound.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from "@nestjs/common";

export class AppNotFoundException extends HttpException {
constructor(msg?: string, status?: HttpStatus){
super(msg || 'Application Not found', status || HttpStatus.NOT_FOUND)
}
}
4 changes: 4 additions & 0 deletions src/app-auth/services/app-auth/app-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ export class AppAuthService {
getAllApps () {
return this.apps
}

getAppById (appId: string) {
return this.apps.find(app => app.appId === appId)
}
}

0 comments on commit bd6959b

Please sign in to comment.