Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api-service): remove ApiException in favour of BadRequestException #8066

Merged
merged 4 commits into from
Apr 8, 2025

Conversation

ChmaraX
Copy link
Contributor

@ChmaraX ChmaraX commented Apr 4, 2025

The ApiException is just an empty extension of BadRequestException:

export class ApiException extends BadRequestException {}

The project was using something like 60% BadRequestException and 40% ApiException interchangeably.

The ApiException was also copied to api, worker and ws or imported from @novu/application-generic - not consistent.

I think using BadRequestException everywhere is better and simpler approach since it comes from nestjs/common directly and it doesn't seems like we need this abstraction.

Caution

Could this be potentially a breaking change? I don't think so because exception type/instance is not being passed over HTTP and I think SDK has its own type for errors.
Like if someone would check in their code for error instanceof ApiException or something like that

Copy link

netlify bot commented Apr 4, 2025

Deploy Preview for dashboard-v2-novu-staging ready!

Name Link
🔨 Latest commit 6bf5c33
🔍 Latest deploy log https://app.netlify.com/sites/dashboard-v2-novu-staging/deploys/67f39f6e8e8f7f0008ae9f2e
😎 Deploy Preview https://deploy-preview-8066.dashboard-v2.novu-staging.co
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -70,7 +69,7 @@ export class AuthController {
Logger.verbose('Checking Github Auth');

if (!process.env.GITHUB_OAUTH_CLIENT_ID || !process.env.GITHUB_OAUTH_CLIENT_SECRET) {
throw new ApiException(
throw new BadRequestException(
Copy link
Contributor Author

@ChmaraX ChmaraX Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of BadRequestException is questionable here, it should be probably InternalServerErrorException

but for now this PR doesn't focus on this - it would be a breaking change, at least now we can see that the exceptions are wrong since they're not being hidden behind ApiException

@@ -147,7 +153,7 @@ export class CommunityAuthService implements IAuthService {
);

const dbUser = await this.userRepository.findById(user._id);
if (!dbUser) throw new ApiException('User not found');
if (!dbUser) throw new BadRequestException('User not found');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here should be NotFoundException

@@ -151,7 +151,7 @@ export class GetChanges {
try {
if (process.env.NOVU_ENTERPRISE === 'true' || process.env.CI_EE_TEST === 'true') {
if (!require('@novu/ee-shared-services')?.TranslationsService) {
throw new ApiException('Translation module is not loaded');
throw new BadRequestException('Translation module is not loaded');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, InternalServerErrorException

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 95 out of 95 changed files in this pull request and generated 1 comment.

@@ -147,7 +153,7 @@ export class CommunityAuthService implements IAuthService {
);

const dbUser = await this.userRepository.findById(user._id);
if (!dbUser) throw new ApiException('User not found');
if (!dbUser) throw new BadRequestException('User not found');
Copy link
Preview

Copilot AI Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using NotFoundException here instead of BadRequestException to better indicate that the user was not located in the database.

Suggested change
if (!dbUser) throw new BadRequestException('User not found');
if (!dbUser) throw new NotFoundException('User not found');

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but user might have a code that checks if (error.status === 400) - that would be a breaking change.

That said we can still make it and bump a release version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a safe change, affecting only the V0 built-in auth. It should be a 404.

@@ -147,7 +153,7 @@ export class CommunityAuthService implements IAuthService {
);

const dbUser = await this.userRepository.findById(user._id);
if (!dbUser) throw new ApiException('User not found');
if (!dbUser) throw new BadRequestException('User not found');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a safe change, affecting only the V0 built-in auth. It should be a 404.

@ChmaraX ChmaraX merged commit c85a5ed into next Apr 8, 2025
28 of 29 checks passed
@ChmaraX ChmaraX deleted the refactor/remove-api-exception branch April 8, 2025 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants