implementation of exception handling

This commit is contained in:
Kallesh B S 2025-05-06 09:53:31 +05:30
parent 0cf1727029
commit 81945f66c4
5 changed files with 57 additions and 12 deletions

View File

@ -0,0 +1,19 @@
import { HttpException, HttpStatus } from '@nestjs/common';
export class InternalServerException extends HttpException {
constructor(
message = 'Internal server error',
errorCode = 'INTERNAL_ERROR',
data: any = null,
) {
super(
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message,
// errorCode,
// data,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}

View File

@ -1,7 +1,7 @@
import { Body, Controller, Get, Patch, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ApiInternalServerErrorResponse, ApiOkResponse, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { RegionService } from './region.service';
import { InsertRegionsDto, UpdateRegionDto } from 'src/oracle/uscib-managed-sp/region/region.dto';
import { InsertRegionsDto, RegionDto, UpdateRegionDto } from 'src/oracle/uscib-managed-sp/region/region.dto';
@Controller('mssql')
export class RegionController {
@ -9,6 +9,12 @@ export class RegionController {
constructor(private readonly regionService: RegionService) { }
@ApiTags('Regions - Mssql')
@ApiOperation({ summary: 'Get all regions for issuing and replacement.' })
@ApiOkResponse({
description: 'User retrieved successfully',
type: RegionDto,
})
@ApiInternalServerErrorResponse({ description: 'Server error' })
@Get('/GetRegions')
getRegions() {
return this.regionService.getRegions();

View File

@ -1,8 +1,9 @@
import { Injectable } from '@nestjs/common';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { Connection, Request } from 'mssql';
import { MssqlDBService } from 'src/db/db.service';
import { InsertRegionsDto, UpdateRegionDto } from 'src/oracle/uscib-managed-sp/region/region.dto';
import * as mssql from 'mssql';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
@Injectable()
export class RegionService {
@ -16,11 +17,8 @@ export class RegionService {
const request = new Request(connection);
const result = await request.execute('carnetsys.GETREGIONS');
return { data: result.recordset };
} catch (error) {
console.log("Error is here ...");
return { error: error.message };
throw new InternalServerException();
}
}
@ -36,7 +34,7 @@ export class RegionService {
return { data: result.recordset };
} catch (error) {
return { error: error.message };
throw new InternalServerException();
}
}
@ -51,7 +49,7 @@ export class RegionService {
return { data: result.recordset };
} catch (error) {
return { error: error.message };
throw new InternalServerException();
}
}
}

View File

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import * as oracledb from 'oracledb';
import { OracleDBService } from 'src/db/db.service';
import {
@ -75,6 +75,9 @@ export class ManageFeeService {
return rows;
} catch (err) {
if (err instanceof Error) {
if(err.message === "NJS-107: invalid cursor"){
throw new BadRequestException("Bad Request")
}
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };

View File

@ -1,5 +1,5 @@
import { IsDefined, IsNumber, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { IsDefined, IsInt, IsNumber, IsOptional, IsString } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class InsertRegionsDto {
@ApiProperty({ required: true })
@ -24,3 +24,22 @@ export class UpdateRegionDto {
@IsDefined({ message: 'Property p_name is required' })
p_name: string;
}
export class RegionDto {
@ApiProperty({ example: 1, description: 'Unique identifier for the region' })
@IsInt()
REGIONID: number;
@ApiProperty({ example: '01', description: 'Region code' })
@IsString()
REGION: string;
@ApiProperty({ example: 'uk', description: 'Human-readable region name' })
@IsString()
REGIONNAME: string;
@ApiPropertyOptional({ example: null, description: 'Error message if applicable' })
@IsOptional()
@IsString()
ERRORMESG?: string | null;
}