-
Notifications
You must be signed in to change notification settings - Fork 529
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
Toast Notifications: Handle pydantic spec errors #9706
Conversation
WalkthroughThe pull request introduces enhanced error handling for Pydantic validation errors in the Changes
Sequence DiagramsequenceDiagram
participant Client
participant ErrorHandler
participant ToastNotification
Client->>ErrorHandler: Trigger HTTP error handling
ErrorHandler->>ErrorHandler: Check if errors are Pydantic errors
alt Pydantic Errors
ErrorHandler->>ErrorHandler: Process each Pydantic error
ErrorHandler->>ToastNotification: Display detailed error messages
else Standard Errors
ErrorHandler->>ToastNotification: Display generic bad request notification
end
Possibly related PRs
Suggested labels
Suggested reviewers
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
CodeRabbit Configuration File (
|
✅ Deploy Preview for care-ohc ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Deploying care-fe with Cloudflare Pages
|
Deploying care-fe-staging with Cloudflare Pages
|
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 (1)
src/Utils/request/errorHandler.ts (1)
88-99
: Handle potential empty or unknown locations.When building
'${loc.join(".")}'
, consider a fallback for empty or undefinedloc
arrays. This ensures more robust error reporting:- toast.error(`${title}: '${loc.join(".")}'`, { + const locDisplay = loc?.length ? loc.join(".") : "unknown.location"; + toast.error(`${title}: '${locDisplay}'`, { description: msg, duration: 8000, });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/Utils/request/errorHandler.ts
(2 hunks)
🔇 Additional comments (2)
src/Utils/request/errorHandler.ts (2)
34-39
: Good separation of Pydantic vs. generic bad requests.By checking
isPydanticError(errs)
and handling it distinctly, you provide clearer error messages for validation issues while preserving the existing flow for non-Pydantic errors. This is a strong approach to error handling. Ensure that alerts or logs remain consistent whenerrs
is undefined or does not match the expected structure.
70-77
: Validate correctness of theurl
property and align with Pydantic error structure.While this type definition accurately captures the relevant fields, confirm that all fields—especially
url
—are consistently returned by the API. Ifurl
is optional, consider making it optional in the type:- url: string; + url?: string;✅ Verification successful
Let me gather information about the Pydantic error structure usage in the codebase.
Based on the search results, I can now provide a conclusive response about the Pydantic error structure.
The
url
property should indeed be required as it's consistently used across the codebaseLooking at the codebase:
- The
DetailedValidationError
interface insrc/types/questionnaire/batch.ts
also defines theurl
property as required- The error handling implementation in
src/Utils/request/errorHandler.ts
expects and uses theurl
property as a required field- The type guard
isPydanticError
validates the structure, and the error handling code assumes the presence of this fieldThe current type definition is correct and aligns with both the Pydantic error structure used in the API and the rest of the codebase's error handling implementation.
🏁 Scripts executed
The following scripts were executed for the analysis:
Error: No shell scripts found
Script:
#!/bin/bash # Search for Pydantic error handling and usage patterns rg -U "PydanticError" -A 5 -B 5 # Search for actual error handling implementations ast-grep --pattern 'type PydanticError = { $$$ }' # Look for actual error handling code rg "ValidationError|pydantic.*Error" -A 5Length of output: 14211
function isPydanticError(errors: unknown): errors is PydanticError[] { | ||
return ( | ||
Array.isArray(errors) && | ||
errors.every( | ||
(error) => typeof error === "object" && error !== null && "type" in error, | ||
) | ||
); | ||
} |
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.
🛠️ Refactor suggestion
Include additional checks for required PydanticError fields.
Currently, isPydanticError
only confirms that each element is a non-null object with a type
field. Pydantic errors often require other fields like msg
and loc
. Consider strengthening the conditions:
-return (
- Array.isArray(errors) &&
- errors.every(
- (error) => typeof error === "object" && error !== null && "type" in error
- )
-);
+return (
+ Array.isArray(errors) &&
+ errors.every(
+ (error) =>
+ typeof error === "object" &&
+ error !== null &&
+ "type" in error &&
+ "loc" in error &&
+ "msg" in 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.
function isPydanticError(errors: unknown): errors is PydanticError[] { | |
return ( | |
Array.isArray(errors) && | |
errors.every( | |
(error) => typeof error === "object" && error !== null && "type" in error, | |
) | |
); | |
} | |
function isPydanticError(errors: unknown): errors is PydanticError[] { | |
return ( | |
Array.isArray(errors) && | |
errors.every( | |
(error) => | |
typeof error === "object" && | |
error !== null && | |
"type" in error && | |
"loc" in error && | |
"msg" in error | |
) | |
); | |
} |
CARE Run #4160
Run Properties:
|
Project |
CARE
|
Branch Review |
rithviknishad/fix/handle-pydantic-errors
|
Run status |
Passed #4160
|
Run duration | 01m 14s |
Commit |
403467a765: Toast Notifications: Handle pydantic spec errors
|
Committer | Rithvik Nishad |
View all properties for this run ↗︎ |
Test results | |
---|---|
Failures |
0
|
Flaky |
0
|
Pending |
0
|
Skipped |
0
|
Passing |
4
|
View all changes introduced in this branch ↗︎ |
@rithviknishad Your efforts have helped advance digital healthcare and TeleICU systems. 🚀 Thank you for taking the time out to make CARE better. We hope you continue to innovate and contribute; your impact is immense! 🙌 |
Proposed Changes
@ohcnetwork/care-fe-code-reviewers
Merge Checklist
Summary by CodeRabbit