Skip to content

Commit

Permalink
feat(nestjs-grpc-errors): add not-found exception-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Amiditin committed Feb 11, 2025
1 parent c4d8e9b commit 0acb3e9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './assertion.exception-factory.js'
export * from './grpc-validation.exception-factory.js'
export * from './internal-server.exception-factory.js'
export * from './not-found.exception-factory.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { NotFoundException } from '@nestjs/common'

import * as grpcErrorStatus from '@atls/grpc-error-status'
import { RpcException } from '@nestjs/microservices'
import { status } from '@grpc/grpc-js'

const { ErrorStatus } = grpcErrorStatus

export const notFoundExceptionFactory = (error: NotFoundException): RpcException => {
const errorStatus = new ErrorStatus(status.NOT_FOUND, error.message)

return new RpcException(errorStatus.toServiceError())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { AssertionError } from 'node:assert'

import { Catch } from '@nestjs/common'
import { InternalServerErrorException } from '@nestjs/common'
import { NotFoundException } from '@nestjs/common'
import { BaseRpcExceptionFilter } from '@nestjs/microservices'
import { Observable } from 'rxjs'

import { assertionExceptionFactory } from '../exception-factories/index.js'
import { internalServerExceptionFactory } from '../exception-factories/index.js'
import { notFoundExceptionFactory } from '../exception-factories/index.js'

@Catch()
export class GrpcExceptionsFilter extends BaseRpcExceptionFilter {
Expand All @@ -22,6 +24,10 @@ export class GrpcExceptionsFilter extends BaseRpcExceptionFilter {
return super.catch(internalServerExceptionFactory(exception), host)
}

if (exception instanceof NotFoundException) {
return super.catch(notFoundExceptionFactory(exception), host)
}

return super.catch(exception, host)
}
}

0 comments on commit 0acb3e9

Please sign in to comment.