-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Loading either the Sample or Default database doesn't create a default Agenda Item #2482 #2794
Conversation
WalkthroughThe changes introduce a new asynchronous function Changes
Sequence DiagramsequenceDiagram
participant Middleware
participant Trace
participant getNanoid
participant nanoid
Middleware->>+getNanoid: Request nanoid function
getNanoid->>+nanoid: Dynamically import
nanoid-->>-getNanoid: Return customAlphabet
getNanoid-->>-Middleware: Provide nanoid generator
Middleware->>Middleware: Generate tracing ID
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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 using PR comments)
Other keywords and placeholders
Documentation and Community
|
Our Pull Request Approval ProcessThanks for contributing! Testing Your CodeRemember, your PRs won't be reviewed until these criteria are met:
Our policies make our code better. ReviewersDo not assign reviewers. Our Queue Monitors will review your PR and assign them.
Reviewing Your CodeYour reviewer(s) will have the following roles:
CONTRIBUTING.mdRead our CONTRIBUTING.md file. Most importantly:
Other
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Congratulations on making your first PR! 🎊 If you haven't already, check out our Contributing Guidelines and PR Reporting Guidelines to ensure that you are following our guidelines for contributing and creating PR.
There was a problem hiding this 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
🧹 Nitpick comments (2)
src/libraries/requestTracing.ts (2)
33-36
: Add an explicit return type forgetNanoid
to satisfy linting rules.The static analysis tools are indicating that a return type should be explicitly declared. Since this function ultimately returns a
() => string
wrapped in aPromise
, consider specifyingPromise<() => string>
as follows:-const getNanoid = async () => { +const getNanoid = async (): Promise<() => string> => { const { customAlphabet } = await import("nanoid"); return customAlphabet(alphabets, 10); };🧰 Tools
🪛 eslint
[error] 33-33: Missing return type on function.
(@typescript-eslint/explicit-function-return-type)
🪛 GitHub Check: Check for linting, formatting, and type errors
[failure] 33-33:
Missing return type on function
88-96
: Consider removing or refining the try/catch block.The catch block simply rethrows the same error without additional handling or logging, which is flagged as unnecessary. If you do not need custom logging or cleanup, removing the try/catch entirely simplifies the flow. Otherwise, add meaningful actions or logs inside the catch block to justify its presence.
-export const trace = async <T>( - tracingId: string, - method: () => T, -): Promise<void> => { - try { - const nanoid = await getNanoid(); - await requestTracingNamespace.runAndReturn<T>(() => { - setTracingId(tracingId || nanoid()); - return method(); - }); - } catch (error) { - throw error; - } +export const trace = async <T>(tracingId: string, method: () => T): Promise<void> => { + const nanoid = await getNanoid(); + await requestTracingNamespace.runAndReturn<T>(() => { + setTracingId(tracingId || nanoid()); + return method(); + }); }🧰 Tools
🪛 Biome (1.9.4)
[error] 95-95: The catch clause that only rethrows the original error is useless.
An unnecessary catch clause can be confusing.
Unsafe fix: Remove the try/catch clause.(lint/complexity/noUselessCatch)
🪛 eslint
[error] 88-96: Unnecessary try/catch wrapper.
(no-useless-catch)
🪛 GitHub Check: Check for linting, formatting, and type errors
[failure] 88-88:
Unnecessary try/catch wrapper
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/libraries/requestTracing.ts
(3 hunks)
🧰 Additional context used
🪛 eslint
src/libraries/requestTracing.ts
[error] 33-33: Missing return type on function.
(@typescript-eslint/explicit-function-return-type)
[error] 88-96: Unnecessary try/catch wrapper.
(no-useless-catch)
🪛 GitHub Check: Check for linting, formatting, and type errors
src/libraries/requestTracing.ts
[failure] 33-33:
Missing return type on function
[failure] 88-88:
Unnecessary try/catch wrapper
🪛 Biome (1.9.4)
src/libraries/requestTracing.ts
[error] 95-95: The catch clause that only rethrows the original error is useless.
An unnecessary catch clause can be confusing.
Unsafe fix: Remove the try/catch clause.
(lint/complexity/noUselessCatch)
hey @palisadoes here this got a issue which is in separate file a test case is failed so should I address it here or raise a pull request for it?? |
Fix the issue in this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
src/libraries/requestTracing.ts (1)
Line range hint
92-96
: Fix return type and enhance error handling.There are two issues to address:
- The function's return type is
Promise<void>
but it actually returnsPromise<T>
- Error handling for
getNanoid()
should be explicitexport const trace = async <T>( tracingId: string, method: () => T, -): Promise<void> => { +): Promise<T> => { const nanoid = await getNanoid(); - await requestTracingNamespace.runAndReturn<T>(() => { + return requestTracingNamespace.runAndReturn<T>(() => { setTracingId(tracingId || nanoid()); return method(); }); };
🧹 Nitpick comments (1)
src/libraries/requestTracing.ts (1)
33-36
: Consider caching the nanoid function.The current implementation dynamically imports
customAlphabet
on every call. Consider caching the result to avoid unnecessary imports and improve performance.+let cachedNanoid: (() => string) | null = null; const getNanoid: () => Promise<() => string> = async () => { + if (cachedNanoid) return cachedNanoid; const { customAlphabet } = await import("nanoid"); - return customAlphabet(alphabets, 10); + cachedNanoid = customAlphabet(alphabets, 10); + return cachedNanoid; };
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/libraries/requestTracing.ts
(3 hunks)
🔇 Additional comments (2)
src/libraries/requestTracing.ts (2)
64-77
: LGTM! Error handling has been properly implemented.
The changes properly handle asynchronous nanoid generation and include error handling as suggested in the previous review.
Line range hint 33-96
: Verify the relationship with PR objectives.
The PR objectives mention fixing an issue with "Loading either the Sample or Default database doesn't create a default Agenda Item", but this file implements request tracing functionality. Could you clarify how these changes relate to the database loading and agenda item creation issue?
@palisadoes I am trying to fix the error but I'm unable to do it. could u please see it and suggest it to me what could I do. |
There was a problem hiding this 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
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libraries/requestTracing.ts
(3 hunks)tests/libraries/requestTracing.spec.ts
(1 hunks)
🔇 Additional comments (3)
tests/libraries/requestTracing.spec.ts (1)
30-37
: Test coverage looks good!
Your test for the requestContext
middleware properly verifies that nextFunction
is called once. This is a straightforward yet critical test to ensure middleware functionality.
src/libraries/requestTracing.ts (2)
33-36
: Asynchronous import approach is valid.
Using import("nanoid")
on-demand is a good optimization for only loading this dependency when necessary.
92-92
: Ensure upstream error handling for getNanoid
If getNanoid
fails here, it will throw asynchronously. Consider wrapping trace
in a try/catch or letting it propagate to a higher-level error handler to avoid unhandled exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
src/libraries/requestTracing.ts (1)
Line range hint
92-96
: Fix return type and add error handling.The trace function has two issues:
- The return type is
Promise<void>
but the function actually returnsPromise<T>
.- Missing error handling for potential failures in
getNanoid()
ormethod()
.-export const trace = async <T>( +export const trace = async <T = void>( tracingId: string, method: () => T, -): Promise<void> => { +): Promise<T> => { const nanoid = await getNanoid(); - await requestTracingNamespace.runAndReturn<T>(() => { + return requestTracingNamespace.runAndReturn<T>(() => { setTracingId(tracingId || nanoid()); return method(); }); };Also consider adding error handling:
export const trace = async <T = void>( tracingId: string, method: () => T, ): Promise<T> => { try { const nanoid = await getNanoid(); return requestTracingNamespace.runAndReturn<T>(() => { setTracingId(tracingId || nanoid()); return method(); }); } catch (error) { throw new Error(`Tracing failed: ${error.message}`); } };
♻️ Duplicate comments (1)
src/libraries/requestTracing.ts (1)
63-77
:⚠️ Potential issueRemove the duplicate
next()
call to prevent "Headers already sent" errors.The middleware has a critical issue where
next()
is called twice:
- Inside the promise chain (line 71)
- After the promise chain (line 77)
This will cause "Cannot set headers after they are sent to the client" errors in Express.
getNanoid() .then((nanoid) => { const tracingId = req.header(tracingIdHeaderName) || nanoid(); req.headers[tracingIdHeaderName] = tracingId; res.header(tracingIdHeaderName, tracingId); requestTracingNamespace.run(() => { setTracingId(tracingId); next(); }); }) .catch((error) => { next(error); }); - next();
🧹 Nitpick comments (1)
src/libraries/requestTracing.ts (1)
33-36
: LGTM! Consider adding error handling.The dynamic import implementation is clean and follows best practices. However, consider adding explicit error handling for import failures.
const getNanoid: () => Promise<() => string> = async () => { - const { customAlphabet } = await import("nanoid"); - return customAlphabet(alphabets, 10); + try { + const { customAlphabet } = await import("nanoid"); + return customAlphabet(alphabets, 10); + } catch (error) { + throw new Error(`Failed to load nanoid: ${error.message}`); + } };
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libraries/requestTracing.ts
(3 hunks)tests/libraries/requestTracing.spec.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/libraries/requestTracing.spec.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/libraries/requestTracing.ts (1)
Line range hint
91-95
: Add error handling for dynamic import.While the async/await usage is correct, the function should handle potential failures in the dynamic import.
Consider adding try/catch:
export const trace = async <T>( tracingId: string, method: () => T, ): Promise<void> => { - const nanoid = await getNanoid(); - await requestTracingNamespace.runAndReturn<T>(() => { - setTracingId(tracingId || nanoid()); - return method(); - }); + try { + const nanoid = await getNanoid(); + await requestTracingNamespace.runAndReturn<T>(() => { + setTracingId(tracingId || nanoid()); + return method(); + }); + } catch (error) { + // Log the error or handle it appropriately + throw new Error(`Failed to execute traced method: ${error.message}`); + } };
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/libraries/requestTracing.ts
(3 hunks)
🔇 Additional comments (2)
src/libraries/requestTracing.ts (2)
33-36
: LGTM! Clean implementation of dynamic import.
The implementation of getNanoid
follows best practices for dynamic imports and is properly typed.
63-76
:
Fix critical async/await and context propagation issues.
There are two significant issues in the middleware implementation:
-
The
next()
call on line 76 executes immediately after starting the promise chain, not waiting for the async work to complete. This breaks the middleware chain. -
The
next()
call should be inside the namespace context to ensure proper context propagation to subsequent middleware.
Apply this fix:
getNanoid()
.then((nanoid) => {
const tracingId = req.header(tracingIdHeaderName) || nanoid();
req.headers[tracingIdHeaderName] = tracingId;
res.header(tracingIdHeaderName, tracingId);
requestTracingNamespace.run(() => {
setTracingId(tracingId);
+ next();
});
- })
+ },
(error) => {
next(error);
});
- next();
Alternatively, consider using async/await for better readability:
- return (req: Request, res: Response, next: NextFunction): void => {
+ return async (req: Request, res: Response, next: NextFunction): Promise<void> => {
requestTracingNamespace.bindEmitter(req);
requestTracingNamespace.bindEmitter(res);
+ try {
+ const nanoid = await getNanoid();
+ const tracingId = req.header(tracingIdHeaderName) || nanoid();
+ req.headers[tracingIdHeaderName] = tracingId;
+ res.header(tracingIdHeaderName, tracingId);
+
+ requestTracingNamespace.run(() => {
+ setTracingId(tracingId);
+ next();
+ });
+ } catch (error) {
+ next(error);
+ }
- getNanoid()
- .then((nanoid) => {
- const tracingId = req.header(tracingIdHeaderName) || nanoid();
- req.headers[tracingIdHeaderName] = tracingId;
- res.header(tracingIdHeaderName, tracingId);
-
- requestTracingNamespace.run(() => {
- setTracingId(tracingId);
- });
- })
- .catch((error) => {
- next(error);
- });
- next();
};
Likely invalid or redundant comment.
@Ramneet04 Please fix the first comment so the PR can automatically close. Please fix the failed tests. |
This pull request did not get any activity in the past 10 days and will be closed in 180 days if no update occurs. Please verify it has no conflicts with the develop branch and rebase if needed. Mention it now if you need help or give permission to other people to finish your work. |
Closing. Inactivity |
Loading either the Sample or Default database is now creating a default Agenda Item.
Issue: #2482: #2482
Changes Implemented:
Imported the nanoid() function dynamically.
Add try catch to handle error
Other information
I have read the previous refactor PR and tried to keep things as uniform as possible.
Please suggest any other changes if required.
Summary by CodeRabbit
New Features
Bug Fixes