keyshade-xyz / keyshade

Realtime secret and configuration management tool, with the best in class security and seamless integration support
https://keyshade.xyz
Mozilla Public License 2.0
196 stars 96 forks source link

fix: Error messages fixed in api-key service #418

Closed Nil2000 closed 3 weeks ago

Nil2000 commented 3 weeks ago

User description

Description

Error messages updated in keyshade/apps/api/src/api-key/service/api-key.service.ts file

Fixes #417

Dependencies

Mention any dependencies/packages used

Future Improvements

Mention any improvements to be done in future related to any file/feature

Mentions

Mention and tag the people

Screenshots of relevant screens

Add screenshots of relevant screens

Developer's checklist

If changes are made in the code:

Documentation Update


PR Type

Bug fix


Description


Changes walkthrough 📝

Relevant files
Bug fix
api-key.service.ts
Fix error messages in API key service                                       

apps/api/src/api-key/service/api-key.service.ts
  • Updated error messages for NotFoundException.
  • Changed error message to use apiKeySlug instead of apiKeyId.
  • +3/-3     

    💡 PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    codiumai-pr-agent-free[bot] commented 3 weeks ago

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Inconsistent Error Handling
    The error message in the catch block (line 140) throws a NotFoundException, which may not be appropriate for all types of errors that could occur during the delete operation.
    codiumai-pr-agent-free[bot] commented 3 weeks ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    ✅ Reorder code to prevent potential null reference errors ___ **Consider moving the apiKey check before accessing apiKey.id to avoid potential
    runtime errors if apiKey is null.** [apps/api/src/api-key/service/api-key.service.ts [88-97]](https://github.com/keyshade-xyz/keyshade/pull/418/files#diff-9fefafaa7fbbd90c1f65410a2414bfab311e6faeb1277c532fe372060a33ebefR88-R97) ```diff const apiKey = await this.prisma.apiKey.findUnique({ where: { slug: apiKeySlug } }) -const apiKeyId = apiKey.id if (!apiKey) { throw new NotFoundException(`API key ${apiKeySlug} not found`) } +const apiKeyId = apiKey.id + ``` `[Suggestion has been applied]`
    Suggestion importance[1-10]: 9 Why: This suggestion addresses a potential bug where accessing `apiKey.id` could result in a runtime error if `apiKey` is null. Reordering the code to check for null before accessing properties is a crucial fix.
    9
    Error handling
    Improve error handling to distinguish between different types of errors ___ **Consider adding more specific error handling in the deleteApiKey method to
    distinguish between "not found" errors and other potential issues.** [apps/api/src/api-key/service/api-key.service.ts [132-141]](https://github.com/keyshade-xyz/keyshade/pull/418/files#diff-9fefafaa7fbbd90c1f65410a2414bfab311e6faeb1277c532fe372060a33ebefR132-R141) ```diff try { await this.prisma.apiKey.delete({ where: { slug: apiKeySlug, userId: user.id } }) } catch (error) { - throw new NotFoundException(`API key ${apiKeySlug} not found`) + if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2025') { + throw new NotFoundException(`API key ${apiKeySlug} not found`) + } + throw error } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: This suggestion improves error handling by distinguishing between different types of errors, which enhances the robustness and clarity of the code.
    8
    Consistency
    Standardize error handling approach across different methods ___ **Consider using a consistent approach for error handling across all methods. The
    getApiKeyBySlug method doesn't use a try-catch block, unlike deleteApiKey.** [apps/api/src/api-key/service/api-key.service.ts [155-167]](https://github.com/keyshade-xyz/keyshade/pull/418/files#diff-9fefafaa7fbbd90c1f65410a2414bfab311e6faeb1277c532fe372060a33ebefR155-R167) ```diff -const apiKey = await this.prisma.apiKey.findUnique({ - where: { - slug: apiKeySlug, - userId: user.id - }, - select: this.apiKeySelect -}) +try { + const apiKey = await this.prisma.apiKey.findUnique({ + where: { + slug: apiKeySlug, + userId: user.id + }, + select: this.apiKeySelect + }) -if (!apiKey) { - throw new NotFoundException(`API key ${apiKeySlug} not found`) + if (!apiKey) { + throw new NotFoundException(`API key ${apiKeySlug} not found`) + } + + return apiKey +} catch (error) { + if (error instanceof NotFoundException) { + throw error + } + throw new InternalServerErrorException('An error occurred while fetching the API key') } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Standardizing error handling across methods can improve code consistency and maintainability, though it may not be as critical as addressing potential runtime errors.
    7
    rajdip-b commented 2 weeks ago

    :tada: This PR is included in version 2.5.0 :tada:

    The release is available on GitHub release

    Your semantic-release bot :package::rocket: