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

[93] Updated response formatting interceptor #142

Merged
merged 6 commits into from
Jul 6, 2024

Conversation

Savio629
Copy link
Collaborator

@Savio629 Savio629 commented Jun 28, 2024

Updated response formatting interceptor

fixes #141

Demo

image

image

Summary by CodeRabbit

  • New Features

    • Enhanced response formatting to include success flag, message, data, and error fields for better clarity.
  • Bug Fixes

    • Improved error handling and response formatting logic to provide more informative error messages.
  • Tests

    • Added comprehensive tests for file upload handling to ensure robust functionality and error management.

Copy link

coderabbitai bot commented Jun 28, 2024

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Recent updates enhance file upload handling and response formatting functionalities. The FastifyFileInterceptor has been slightly modified, and corresponding tests were introduced. The ResponseFormatInterceptor now includes structured error handling and a success flag. Additionally, import paths in sample code have been updated for clarity and consistency.

Changes

File(s) Change Summary
packages/common/src/interceptors/file-upload.interceptor.ts Minor alteration in the return statement of FastifyFileInterceptor function.
packages/common/src/interceptors/response-format.interceptor.ts Updated ResponseFormatInterceptor to include a success flag, a structured response, and enhanced error formatting.
packages/common/test/file-upload.interceptor.spec.ts Introduced new tests for FastifyFileInterceptor, covering file uploads, error scenarios, and missing files.
sample/03-response-formatting/src/main.ts Changed import path for ResponseFormatInterceptor to '@samagra-x/stencil' from a relative path.

Poem

In code forests deep, where functions leap,
Interceptors have grown, with a brand-new tone.
Errors formatted right, with a response that’s bright,
File uploads now tested, in logic well-invested.
Import paths align, making the code fine.
The forest hums, the update drums,
Rabbit leaps with joy—oh, what fun! 🌟🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
packages/common/src/interceptors/file-upload.interceptor.ts (1)

Line range hint 23-24: Decorator usage issue flagged by static analysis.

The static analysis tool indicates an improper usage of decorators. In TypeScript, decorators can only be used on class declarations, class expressions, and class methods. To solve this, ensure that decorators are used correctly or adjust the TypeScript configuration if necessary.

- @Optional()
- @Inject('MULTER_MODULE_OPTIONS')
+ // Decorators removed due to incorrect usage. Consider appropriate alternatives or enable unsafe decorators in TS config.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f8221bb and ba41e72.

Files ignored due to path filters (1)
  • packages/user-service/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (4)
  • packages/common/src/interceptors/file-upload.interceptor.ts (1 hunks)
  • packages/common/src/interceptors/response-format.interceptor.ts (1 hunks)
  • packages/common/test/file-upload.interceptor.spec.ts (1 hunks)
  • sample/03-response-formatting/src/main.ts (1 hunks)
Additional context used
Biome
packages/common/src/interceptors/file-upload.interceptor.ts

[error] 23-24: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods.
You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

(parse)

packages/common/src/interceptors/response-format.interceptor.ts

[error] 41-41: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

Additional comments not posted (3)
sample/03-response-formatting/src/main.ts (1)

3-3: Updated import path for ResponseFormatInterceptor.

The import path update to use the package '@samagra-x/stencil' instead of a local path helps in maintaining modularity and ensures that the interceptor is managed as part of a package, which is good for maintainability.

packages/common/src/interceptors/response-format.interceptor.ts (1)

17-35: Enhanced response structure in interceptor.

The updates to include a success flag, and more detailed message, data, and error fields enhance the clarity and utility of the response structure. These changes are crucial for consistent error handling and response formatting.

packages/common/test/file-upload.interceptor.spec.ts (1)

5-62: Comprehensive test suite for FastifyFileInterceptor.

The tests cover various scenarios including normal operations, error handling, and edge cases like missing files. This is good practice to ensure that the interceptor behaves as expected under different conditions.

Comment on lines 40 to 55
private formatErrorMessage(error: any): string {
if (error.response && error.response.message) {
return error.response.message;
}
return error.message || 'Internal Server Error';
}

private formatError(error: any): any {
// Format your error object here. This can be customized as per requirements.
if (error.response) {
return {
code: error.response.statusCode,
message: error.response.message,
statusCode:
error.response.statusCode || HttpStatus.INTERNAL_SERVER_ERROR,
};
}
return {
code: error.status || HttpStatus.INTERNAL_SERVER_ERROR,
Copy link

Choose a reason for hiding this comment

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

Refactor error handling methods to use optional chaining.

The static analysis tool suggests using optional chaining to simplify the code. This will make the code cleaner and reduce the possibility of runtime errors.

- if (error.response && error.response.message) {
+ return error.response?.message || 'Internal Server Error';
- }
- return error.message || 'Internal Server Error';
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private formatErrorMessage(error: any): string {
if (error.response && error.response.message) {
return error.response.message;
}
return error.message || 'Internal Server Error';
}
private formatError(error: any): any {
// Format your error object here. This can be customized as per requirements.
if (error.response) {
return {
code: error.response.statusCode,
message: error.response.message,
statusCode:
error.response.statusCode || HttpStatus.INTERNAL_SERVER_ERROR,
};
}
return {
code: error.status || HttpStatus.INTERNAL_SERVER_ERROR,
private formatErrorMessage(error: any): string {
return error.response?.message || error.message || 'Internal Server Error';
}
private formatError(error: any): any {
if (error.response) {
return {
code: error.response.statusCode,
message: error.response.message,
};
}
return {
code: error.status || HttpStatus.INTERNAL_SERVER_ERROR,
Tools
Biome

[error] 41-41: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

@Savio629 Savio629 changed the base branch from main to dev June 28, 2024 13:40
@techsavvyash techsavvyash merged commit 56b9c7d into SamagraX-Stencil:dev Jul 6, 2024
1 check passed
@Savio629 Savio629 changed the title Updated response formatting interceptor [93] Updated response formatting interceptor Aug 5, 2024
@Savio629
Copy link
Collaborator Author

Savio629 commented Aug 5, 2024

Parent issue: #93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

update response formatting interceptor
2 participants