Skip to content

feat: add throwOnError option to generate function#3362

Draft
chu0119 wants to merge 1 commit into
orval-labs:masterfrom
chu0119:feat/throw-on-error-option
Draft

feat: add throwOnError option to generate function#3362
chu0119 wants to merge 1 commit into
orval-labs:masterfrom
chu0119:feat/throw-on-error-option

Conversation

@chu0119
Copy link
Copy Markdown

@chu0119 chu0119 commented May 16, 2026

Summary

Added throwOnError option to the generate function that allows callers to opt into error-throwing behavior.

Why

When using orval programmatically, errors are caught internally and only logged, making it difficult for callers to implement custom error handling.

What changed

  • Added throwOnError?: boolean to GlobalOptions
  • Updated three try/catch blocks in generate.ts to re-throw when enabled

Closes #2290

Summary by CodeRabbit

Release Notes

  • New Features
    • Added optional throwOnError configuration setting to control error handling behavior during generation operations. When enabled, errors are re-thrown for improved error visibility and control.

Review Change Stack

Added `throwOnError` option to `GlobalOptions` that, when set to true,
causes the `generate` function to throw errors instead of just logging
them. This allows programmatic consumers to handle errors with their
own error handling logic.

Closes orval-labs#2290
Copilot AI review requested due to automatic review settings May 16, 2026 03:54
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

📝 Walkthrough

Walkthrough

This PR adds a new throwOnError configuration option to GlobalOptions and implements conditional error re-throwing in the generate() function. When enabled, generation errors are re-thrown after being caught; otherwise, errors are logged as before.

Changes

Conditional error re-throwing in generate

Layer / File(s) Summary
Configuration contract
packages/core/src/types.ts
GlobalOptions interface extended with optional throwOnError?: boolean field.
Generate function error handling
packages/orval/src/generate.ts
Per-project, single-project, and watcher-based error handlers now re-throw caught generateSpec errors when options?.throwOnError is enabled, preserving existing error logging and failOnWarnings behavior.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A gentle flag now makes the errors fly,
Instead of silent logs that pass on by,
Three error paths with care now throw with might,
When throwOnError shines its truthful light!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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: adding a throwOnError option to the generate function, which is the core feature introduced in this PR.
Linked Issues check ✅ Passed The PR implements the core requirement from issue #2290: enabling error throwing in the generate function via a throwOnError option, allowing programmatic callers to catch and handle errors.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing the throwOnError feature: adding the option to GlobalOptions type and implementing conditional re-throws in three error handling locations.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new throwOnError option that, when enabled, causes generate to rethrow errors from generateSpec instead of only logging them.

Changes:

  • Add throwOnError?: boolean to GlobalOptions.
  • Rethrow caught errors in three generateSpec call sites when throwOnError is set.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/core/src/types.ts Adds the new throwOnError field to GlobalOptions.
packages/orval/src/generate.ts Rethrows errors from generateSpec in three locations when throwOnError is true.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +42 to 45
if (options?.throwOnError) {
throw error;
}
logError(error, projectName);
Copy link
Copy Markdown
Contributor

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/orval/src/generate.ts (1)

59-61: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Apply throwOnError in the config-file watch callback too.

Line 59-61 still swallows generation errors in watch mode for config-file flows, so behavior is inconsistent with the new throwOnError contract.

Suggested fix
         await startWatcher(
           options.watch,
           async () => {
             resetWarnings();
             try {
               await generateSpec(workspace, normalizedOptions, projectName);
             } catch (error) {
+              if (options?.throwOnError) {
+                throw error;
+              }
               logError(error, projectName);
             }
             if (options.failOnWarnings && getWarningCount() > 0) {
               throw new Error(
                 `Process failed with ${getWarningCount()} warning(s) due to failOnWarnings option`,
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/orval/src/generate.ts` around lines 59 - 61, In the catch block
inside the config-file watch callback (currently calling logError(error,
projectName)), respect the new throwOnError contract by checking the
watcher/config option and rethrowing the caught error when enabled: call
logError(error, projectName) as today, then if the config or
options.loaded.throwOnError (or the existing throwOnError variable) is truthy,
rethrow error so the watch flow doesn't swallow it; update the catch to
reference the same throwOnError flag used elsewhere so behavior is consistent
across generation paths.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/orval/src/generate.ts`:
- Around line 59-61: In the catch block inside the config-file watch callback
(currently calling logError(error, projectName)), respect the new throwOnError
contract by checking the watcher/config option and rethrowing the caught error
when enabled: call logError(error, projectName) as today, then if the config or
options.loaded.throwOnError (or the existing throwOnError variable) is truthy,
rethrow error so the watch flow doesn't swallow it; update the catch to
reference the same throwOnError flag used elsewhere so behavior is consistent
across generation paths.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4ea00515-cb79-4f76-aa08-8cd9366795c0

📥 Commits

Reviewing files that changed from the base of the PR and between e3b888e and aac126a.

📒 Files selected for processing (2)
  • packages/core/src/types.ts
  • packages/orval/src/generate.ts

@melloware melloware added the enhancement New feature or request label May 16, 2026
Copy link
Copy Markdown
Collaborator

@melloware melloware left a comment

Choose a reason for hiding this comment

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

Build is failing and documentation needed based on your other ticket

Copy link
Copy Markdown

@ded-furby ded-furby left a comment

Choose a reason for hiding this comment

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

One behavior worth documenting in this PR: when generate() loads a config file with multiple named projects, throwOnError now fails fast on the first generateSpec error and exits the for...of loop before later projects run. That is probably the right tradeoff for programmatic callers, but it is a semantic change from the current "log and continue" behavior, not just a different error surface.

I’d call that out explicitly in docs/content/docs/reference/integration.mdx alongside the new option example so consumers know enabling throwOnError changes multi-project runs from best-effort to fail-fast.

@melloware
Copy link
Copy Markdown
Collaborator

@ded-furby any chance you want to resubmit this PR with the documentation change? It seems the OP has let this branch go dead...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose throwing generate function from orval package

4 participants