Skip to content

feat: handle database errors - #11645

Open
aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/handle-database-errors
Open

aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/handle-database-errors

Conversation

@aaqilniz

@aaqilniz aaqilniz commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Standardizes database error handling across @loopback/repository and @loopback/rest by translating connector-level driver errors into domain-specific, protocol-neutral errors, which are then mapped to REST HTTP statuses.

Key Changes

  • @loopback/repository:
    • Introduced base DatabaseError and domain-specific sub-classes (UniqueConstraintError, ForeignKeyConstraintError, NotNullConstraintError, LockConflictError, DatabaseConnectionError, etc.).
    • Added handleRepositoryError() utility to intercept low-level driver error codes and normalize them into protocol-neutral DatabaseError instances while retaining context/details.
  • @loopback/rest:
    • Added mapDatabaseErrorToHttpError() to map protocol-neutral DatabaseError codes to appropriate HTTP status codes (400, 409, 422, 503, 504).
    • Updated RejectProvider to run the mapping automatically before serializing the error response.
  • Documentation & Unit Tests: Added reference documentation for all error mappings and unit tests covering repository error normalization and REST error serialization.

Architectural Notes

Connector-specific error translation is supposed to be handled at the connector level. Here are the changes to mysql-connector. @loopback/repository purely consumes standardized code strings, maintaining complete separation between storage layer implementations and transport protocols.

Dependent on PR@loopback-connector-mysql

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@aaqilniz
aaqilniz force-pushed the feat/handle-database-errors branch 2 times, most recently from 5240504 to 0a2b09c Compare July 5, 2026 05:37
@aaqilniz
aaqilniz marked this pull request as ready for review July 5, 2026 11:56
@aaqilniz
aaqilniz marked this pull request as draft July 5, 2026 12:46
@aaqilniz
aaqilniz force-pushed the feat/handle-database-errors branch 2 times, most recently from dc6c5f5 to b873faf Compare July 5, 2026 16:06
@aaqilniz
aaqilniz marked this pull request as ready for review July 5, 2026 17:04
@aaqilniz
aaqilniz force-pushed the feat/handle-database-errors branch from b873faf to cf69ebd Compare July 12, 2026 18:29

@achrinza achrinza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree it's a good feature to have.

Though I'd push back on two points:

  1. Database-specific code should be in each database's NPM package. @loopback/rest can then parse the generalised Error object and map it
    • This would improve future extensibility and ensure that first-party and third-party LB4 connectors have the same features accessible.
    • This allows for non-REST applications to do their own mapping (i.e. don't add REST-specific code in @loopback/repository)
  2. Opt-out mechanism
    Arguably this is security through obscurity, but this feature does necessarily expose some of the inner-workings of the application. IIRC 5xx errors are sanitised, but 4xx are not. So perhaps we should build an extensible "mapping" mechanism in @loopback/rest which users can customise?

@aaqilniz
aaqilniz marked this pull request as draft September 23, 2026 13:06
@aaqilniz
aaqilniz force-pushed the feat/handle-database-errors branch 4 times, most recently from 2c13ca2 to 0c265e3 Compare September 25, 2026 11:42
@aaqilniz
aaqilniz marked this pull request as ready for review September 25, 2026 11:53
@aaqilniz
aaqilniz force-pushed the feat/handle-database-errors branch from febca87 to 5051210 Compare September 27, 2026 04:41
@aaqilniz

Copy link
Copy Markdown
Contributor Author

Hi, @achrinza. Thanks for the feedback. I have just made the changes based on your feedback.

The database-specific codes are mapped in the respective connector (Here is the PR in loopback-connector-mysql). Ultimately, the error is mapped at the REST level.
I have added a binding to disable this error mapping. It's enabled by default.

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
@aaqilniz
aaqilniz force-pushed the feat/handle-database-errors branch from 65bb81f to ea38fd1 Compare September 27, 2026 05:27

This branch has not been deployed

No deployments
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.

2 participants