Skip to content

WEB-826: Improve null-safety in ErrorHandlerInterceptor#3340

Open
tkshsbcue wants to merge 2 commits intoopenMF:devfrom
tkshsbcue:WEB-826/improve-error-handler-null-safety
Open

WEB-826: Improve null-safety in ErrorHandlerInterceptor#3340
tkshsbcue wants to merge 2 commits intoopenMF:devfrom
tkshsbcue:WEB-826/improve-error-handler-null-safety

Conversation

@tkshsbcue
Copy link
Contributor

@tkshsbcue tkshsbcue commented Mar 9, 2026

Description:
During network failures, CORS issues, or non-JSON server responses, response.error can be null or undefined. The interceptor directly accesses properties like developerMessage and errors, which can cause runtime errors.

Fix:
Use optional chaining and safe fallbacks when accessing response.error fields so the interceptor handles all error shapes gracefully.

ticket -> https://mifosforge.jira.com/browse/WEB-826?atlOrigin=eyJpIjoiMmRlZWE0NzNjNDkyNDQ0ZmJlMmRjMWYzMGE3NmJlZDkiLCJwIjoiaiJ9

Summary by CodeRabbit

  • Bug Fixes
    • Improved error extraction to handle missing or incomplete error data more safely.
    • Enhanced fallback logic so alerts display a meaningful message when expected error details are absent.
    • Adjusted logged message selection in non-production to prefer available response message with preserved status-based handling and alert behavior.

@coderabbitai
Copy link

coderabbitai bot commented Mar 9, 2026

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'pre_merge_checks'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • 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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 07967656-6069-4d6c-9c5e-e2e5eca9fb23

📥 Commits

Reviewing files that changed from the base of the PR and between 2cf5cf2 and 81ea935.

📒 Files selected for processing (1)
  • src/app/core/http/error-handler.interceptor.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/app/core/http/error-handler.interceptor.ts

Walkthrough

Replaces unsafe direct access to nested error array properties with optional chaining and a prioritized fallback chain for deriving error messages; logging selection adjusted to prefer response?.message when present. No public signatures or status-based control flow were changed.

Changes

Cohort / File(s) Summary
Error Handler Refactor
src/app/core/http/error-handler.interceptor.ts
Use const error = response?.error and optional chaining (error?.errors?.[0]). Derive errorMessage via error?.defaultUserMessageerror?.developerMessageresponse?.message'Unknown error'. Adjust logging to use `response?.message

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • IOhacker
  • alberto-art3ch
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: improving null-safety in the ErrorHandlerInterceptor through optional chaining, which is exactly what the changeset implements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@IOhacker
Copy link
Contributor

IOhacker commented Mar 9, 2026

Run npx prettier

Copy link
Contributor

@IOhacker IOhacker left a comment

Choose a reason for hiding this comment

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

Run npx prettier

Copy link
Contributor

@IOhacker IOhacker left a comment

Choose a reason for hiding this comment

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

Run npx prettier

Safely access response.error with optional chaining; honor root
error.defaultUserMessage before developerMessage. Handles network
errors and non-JSON responses without crashing.

Made-with: Cursor
@tkshsbcue tkshsbcue force-pushed the WEB-826/improve-error-handler-null-safety branch from 458bf45 to 2cf5cf2 Compare March 10, 2026 02:09
@tkshsbcue
Copy link
Contributor Author

@IOhacker fixed!

}
const error = response?.error;
let errorMessage =
error?.defaultUserMessage || error?.developerMessage || response?.message || 'Unknown error';
Copy link
Contributor

@shubhamkumar9199 shubhamkumar9199 Mar 10, 2026

Choose a reason for hiding this comment

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

'Unknown error'
is hard-coded. If the application supports translations (which seems likely given other alert messages), it might be better to route this through the translation service or a shared constant to keep error messaging consistent.

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/app/core/http/error-handler.interceptor.ts`:
- Around line 48-51: The current user-facing fallback chain incorrectly includes
the Angular transport string (response?.message) via the errorMessage
initialization; remove response?.message from the user-facing chain and only use
backend fields (error?.defaultUserMessage, error?.developerMessage and any
error.errors[0] values) to build errorMessage so the later status-specific
branches (400/403/404 handling) can still override when the backend didn't
return structured errors; keep the original transport string (response?.message
or the HttpErrorResponse.message) for logging only (e.g., logMessage or when
calling logger.error) while ensuring variables referenced are response, error,
errorMessage and error.errors[0] so you can locate the change in
error-handler.interceptor.ts.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9467a213-d6d1-4516-b807-a192b1bcb2e9

📥 Commits

Reviewing files that changed from the base of the PR and between 458bf45 and 2cf5cf2.

📒 Files selected for processing (1)
  • src/app/core/http/error-handler.interceptor.ts

errorMessage = response.error.errors[0].defaultUserMessage || response.error.errors[0].developerMessage;
}
const error = response?.error;
let errorMessage =
Copy link
Contributor

Choose a reason for hiding this comment

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

In the previous implementation, only the defaultUserMessage from response.error.errors[0] was used.
In the new implementation, error?.defaultUserMessage is used first.

That changes the order of which message will be used to display to the user.
If this change was intentional, it might be worth mentioning this in the PR description so reviewers understand the reasoning.

Remove response?.message from user-facing chain; keep transport string
for logging only (logMessage).

Made-with: Cursor
if (response.error.errors[0]) {
errorMessage = response.error.errors[0].defaultUserMessage || response.error.errors[0].developerMessage;
}
const error = response?.error;
Copy link
Contributor

Choose a reason for hiding this comment

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

response is typed as HttpErrorResponse and should never be null or undefined, so optional chaining here may not be necessary.
This could be simplified to const error = response.error;.

@shubhamkumar9199
Copy link
Contributor

shubhamkumar9199 commented Mar 10, 2026

@tkshsbcue Could you please squash the commits into a single commit

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.

3 participants