Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxy): handle error when proxy itself errors #13929

Merged
merged 2 commits into from Jul 24, 2023

Conversation

chaejunlee
Copy link
Contributor

@chaejunlee chaejunlee commented Jul 23, 2023

Description

closes #13703

Add error handling to the point when the proxy itself errors.

Right now, the proxy expects that the res is never undefined or other falsy value but there are some cases when the res is.

When the res is falsy, the thrown error becomes the error of accessing undefined value, not the original error.

This makes it hard for developers to trace the original error.

Therefore, I added a logic to handle error case when the res is falsy.

Additional context

My particular case of the problem was passing server.proxy's target as falsy value (null or undefined).

import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    proxy: {
      '^/undefined': {
        target: undefined, // this one
      },
      '^/null': {
        target: null, // and this one
      },
    },
  },
});

AS-IS

Screenshot 2023-07-03 at 11 55 38

Just seeing the error above, it is hard to track where the error started from.

TO-BE

Screenshot 2023-07-23 at 17 21 46

The above error can give developers more specific information about where the error started from.

I believe that my fix can cover so much more errors. Due to the fact that it is caused by an incorrect expectation of the type of res.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Jul 23, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@@ -51,6 +51,10 @@ export function proxyMiddleware(
}

proxy.on('error', (err, req, originalRes) => {
// originalRes can be falsy if the proxy itself errored
if (!originalRes) {
httpServer?.emit('error', err)
Copy link
Member

Choose a reason for hiding this comment

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

Since httpServer can be null (For example, if Vite is running in middleware mode), I think it would be better to output the error here directly (like we do below).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing it out!

I will change it right away.

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label Jul 24, 2023
@chaejunlee
Copy link
Contributor Author

chaejunlee commented Jul 24, 2023

I have changed it to config.logger.error to output the error, and removed the httpServer error handling.

Now, the error console looks like this.

Screenshot 2023-07-24 at 16 33 31

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

@patak-dev patak-dev merged commit 4848e41 into vitejs:main Jul 24, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error handling with proxyMiddleware needs improvement
3 participants