Skip to content

Fix pg write error message - #686

Merged
DZakh merged 1 commit into
mainfrom
dz/fix-pg-write-error-message
Aug 18, 2025
Merged

DZakh merged 1 commit into
mainfrom
dz/fix-pg-write-error-message

Conversation

@DZakh

@DZakh DZakh commented Aug 18, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Improved database error messaging; encoding issues now reported with clearer context.
    • Graceful handling of aborted transactions and miscellaneous errors to reduce crashes and avoid unnecessary interruptions.
    • Batch operations now continue safely where possible, improving reliability.
  • Refactor

    • Consolidated and normalized error handling to avoid unnecessary rethrows, resulting in more consistent behavior and stability.

@coderabbitai

coderabbitai Bot commented Aug 18, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Renamed and reshaped a Postgres error schema in PgStorage.res and refactored executeBatch error handling in IO.res to normalize exceptions, parse generic PG error messages, surface specific UTF-8 encoding errors with table context, skip certain transaction-aborted messages, and avoid rethrowing by resolving to an empty array.

Changes

Cohort / File(s) Summary
PG error schema update
codegenerator/cli/npm/envio/src/PgStorage.res
Renamed pgEncodingErrorSchema → pgErrorMessageSchema; schema changed from fixed tagged message to generic object with string field "message".
executeBatch error handling refactor
codegenerator/cli/templates/static/codegen/src/IO.res
Normalizes exceptions in catch; parses PgStorage.pgErrorMessageSchema from JsError; maps UTF-8 encoding error to PgEncodingError with table; ignores "transaction is aborted" message; prettifies others; no rethrow—resolves to []. Added race-condition note.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant IO.executeBatch as IO.executeBatch
  participant DB as Postgres
  participant PgSchema as PgStorage.pgErrorMessageSchema

  Caller->>IO.executeBatch: invoke
  IO.executeBatch->>DB: beginSql / batch ops
  DB-->>IO.executeBatch: result or error (Promise)
  IO.executeBatch->>IO.executeBatch: Promise.catch normalize exn
  IO.executeBatch->>PgSchema: parse JsError(message)
  alt message == "invalid byte sequence for encoding \"UTF8\": 0x00"
    IO.executeBatch->>IO.executeBatch: set specificError = PgEncodingError{table}
  else message == "current transaction is aborted..."
    IO.executeBatch->>IO.executeBatch: no action
  else other JsError / non-JsError
    IO.executeBatch->>IO.executeBatch: prettify or ignore
  end
  IO.executeBatch-->>Caller: Promise.resolve([]) (no rethrow)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • moose-code
  • MJYoung114

Poem

I twitched my whiskers at the byte with glee,
UTF-8 hiccup? I’ll name the table, see!
Aborted transactions, I’ll quietly hop by—
No rethrows today, just clouds in the sky.
With schemas neat and errors tame,
This bunny commits, and ships the game. 🐇✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dz/fix-pg-write-error-message

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (4)
codegenerator/cli/templates/static/codegen/src/IO.res (4)

172-177: Normalization step is good; small improvement: prefer prettifying normalizedExn for consistency.

You normalize to an exn once; downstream, use this normalized value for prettification instead of the outer exn to avoid surprises when reason is already transformed.


200-200: Typo: “Improtant” → “Important”.

Minor spelling fix in the user-facing comment.

-        // Improtant: Don't rethrow here, since it'll result in
+        // Important: Don't rethrow here, since it'll result in

184-197: Optional: make matching resilient to minor message prefix/suffix variations.

Some PG clients prepend severity or codes. If this shows up in your environment, consider relaxing the equality for the transaction-aborted case as well (e.g., startsWith/includes).


172-197: Optional: Preserve original PG error object in PgStorage.setOrThrow to enable reliable parsing here.

Currently, PgStorage.setOrThrow wraps non-schema errors as Persistence.StorageError with a prettified reason string. That erases the original PG error object, making it harder to inspect message here when entities (not history) fail. If feasible, change PgStorage to store the original exn in reason and only prettify at presentation boundaries.

I can draft that refactor if you’d like.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between cef9a67 and 30ab003.

📒 Files selected for processing (2)
  • codegenerator/cli/npm/envio/src/PgStorage.res (1 hunks)
  • codegenerator/cli/templates/static/codegen/src/IO.res (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{res,resi}

📄 CodeRabbit Inference Engine (.cursor/rules/rescript.mdc)

**/*.{res,resi}: Never use [| item |] to create an array. Use [ item ] instead.
Must always use = for setting value to a field. Use := only for ref values created using ref function.
ReScript has record types which require a type definition before hand. You can access record fields by dot like foo.myField.
It's also possible to define an inline object, it'll have quoted fields in this case.
Use records when working with structured data, and objects to conveniently pass payload data between functions.
Never use %raw to access object fields if you know the type.

Files:

  • codegenerator/cli/npm/envio/src/PgStorage.res
  • codegenerator/cli/templates/static/codegen/src/IO.res
codegenerator/cli/npm/envio/**

📄 CodeRabbit Inference Engine (.cursor/rules/navigation.mdc)

Library-fied runtime shared across indexers lives in codegenerator/cli/npm/envio.

Files:

  • codegenerator/cli/npm/envio/src/PgStorage.res
codegenerator/cli/templates/{dynamic/**/*.hbs,static/**}

📄 CodeRabbit Inference Engine (.cursor/rules/navigation.mdc)

Templates live under codegenerator/cli/templates: dynamic/ for Handlebars (.hbs), static/ for raw Rescript files copied verbatim.

Files:

  • codegenerator/cli/templates/static/codegen/src/IO.res
{**/generated/src/**/*.res,codegenerator/cli/templates/static/codegen/src/**/*.res,codegenerator/cli/templates/dynamic/codegen/src/**/*.res}

📄 CodeRabbit Inference Engine (.cursor/rules/navigation.mdc)

Runtime code lives in each project’s generated/src, but template versions (good for editing) are under codegenerator/cli/templates/static/codegen/src or codegenerator/cli/templates/dynamic/codegen/src.

Files:

  • codegenerator/cli/templates/static/codegen/src/IO.res
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build_and_test
🔇 Additional comments (1)
codegenerator/cli/templates/static/codegen/src/IO.res (1)

166-170: Comment explains an important transactional race. LGTM.

The note clarifies why the catch resolves and relies on beginSql to fail. This context is helpful.

Comment thread codegenerator/cli/npm/envio/src/PgStorage.res
Comment thread codegenerator/cli/templates/static/codegen/src/IO.res
@DZakh
DZakh requested a review from JonoPrest August 18, 2025 10:31
Comment on lines +185 to +186
| `current transaction is aborted, commands ignored until end of transaction block` => ()
| `invalid byte sequence for encoding "UTF8": 0x00` =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are these from the lib or our internal errors? Would rather not depend on string matching an external error. Do they have codes instead? Otherwise all good 👍🏼

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't notice anything like this after a quick look.

@DZakh
DZakh merged commit 667e909 into main Aug 18, 2025
2 checks passed
@DZakh
DZakh deleted the dz/fix-pg-write-error-message branch August 18, 2025 14:21
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