parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

feat: Add information about, db collection and index name on duplicate value error

Open RahulLanjewar93 opened this issue 1 month ago โ€ข 5 comments

Pull Request

Issue

Closes: #9891

Approach

Tasks

  • [X] Add tests
  • [ ] Add changes to documentation (guides, repository pages, code comments)
  • [ ] Add security check
  • [ ] Add new Parse Error codes to Parse JS SDK

Summary by CodeRabbit

  • Bug Fixes

    • Duplicate-value error messages now include database, collection, and index names across create/update operations, giving clearer context for unique-constraint failures.
  • Tests

    • Added test coverage that asserts the exact duplicate-value error message to ensure accurate, consistent reporting.

RahulLanjewar93 avatar Nov 08 '25 20:11 RahulLanjewar93

I will reformat the title to use the proper commit message syntax.

๐Ÿš€ Thanks for opening this pull request!

๐Ÿ“ Walkthrough

Walkthrough

Adds server-side logging of MongoDB E11000 duplicate-key error messages in two adapter paths and extends a test to spy on logger output while keeping the client-facing duplicate-value error unchanged.

Changes

Cohort / File(s) Summary
Mongo storage adapter (logging)
src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Logs duplicate-key errors: when Mongo returns code 11000 in createObject and findOneAndUpdate, the adapter now calls logger.error("Duplicate key error:", err.message) before returning/throwing the existing Parse duplicate-value error. No change to emitted Parse.Error text or control flow.
Test update (logger spy + assertions)
spec/schemas.spec.js
Adds a spy/reset on logger.error and extends assertions to ensure the client-facing error remains the generic duplicate-value message while server logs contain the full MongoDB duplicate-key details (E11000, index name, conflicting value).

Estimated code review effort

๐ŸŽฏ 2 (Simple) | โฑ๏ธ ~10 minutes

  • Focus areas:
    • Verify correct logger import/usage and proper spy/reset in tests.
    • Ensure tests are robust across environments and restore logger state.
    • Review logging content for potential sensitive data exposure in persisted logs.

Pre-merge checks and finishing touches

โœ… Passed checks (5 passed)
Check name Status Explanation
Description check โœ… Passed The description includes the required template structure with the issue link (#9891) filled in and test completion marked, though the Approach section lacks implementation details.
Linked Issues check โœ… Passed The PR successfully addresses issue #9891 by adding server-side logging of the full MongoDB error context for duplicate key errors [#9891], providing better debugging information.
Out of Scope Changes check โœ… Passed All changes are directly related to the objectives: adding logging for duplicate key errors in the MongoDB adapter and corresponding test updates, with no unrelated modifications.
Docstring Coverage โœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check โœ… Passed The title accurately describes the main change: adding debug logging for duplicate key errors. It directly reflects the core modification in both error-handling code paths.
โœจ Finishing touches
  • [ ] ๐Ÿ“ Generate docstrings
๐Ÿงช 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.

coderabbitai[bot] avatar Nov 08 '25 20:11 coderabbitai[bot]

:white_check_mark: Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
:white_check_mark: Open Source Security 0 0 0 0 0 issues

:computer: Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

parseplatformorg avatar Nov 08 '25 20:11 parseplatformorg

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 92.53%. Comparing base (3074eb7) to head (98050b4). :warning: Report is 3 commits behind head on alpha.

Additional details and impacted files
@@           Coverage Diff           @@
##            alpha    #9919   +/-   ##
=======================================
  Coverage   92.53%   92.53%           
=======================================
  Files         190      190           
  Lines       15469    15471    +2     
  Branches      176      176           
=======================================
+ Hits        14314    14316    +2     
  Misses       1143     1143           
  Partials       12       12           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Nov 08 '25 20:11 codecov[bot]

Fixed: Security concern - removed schema information from client error responses

Changes:

  • Removed mongoUniqueIndexErrorFormatter() function (fragile regex parsing)
  • Server logs: Now log the complete MongoDB error message via logger.error('Duplicate key error:', error.message)
  • Client response: Returns only generic message: "A duplicate value for a field with unique values was provided" (no database/collection/index names exposed)
  • Test updated: Verifies generic client message + checks server logs contain full MongoDB error details

This approach is more robust (no regex parsing to break), secure (no schema info leakage), and provides complete debugging information server-side.

mtrezza avatar Dec 14 '25 15:12 mtrezza

๐ŸŽ‰ This change has been released in version 9.1.0-alpha.4

parseplatformorg avatar Dec 14 '25 15:12 parseplatformorg

๐ŸŽ‰ This change has been released in version 9.1.0

parseplatformorg avatar Dec 14 '25 15:12 parseplatformorg