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(devServer): support custom devServer proxy error handler #7457

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/@vue/cli-service/lib/util/prepareProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function prepareProxy (proxy, appPublicFolder) {
return !(isPublicFileRequest || isWdsEndpointRequest)
}

function createProxyEntry (target, usersOnProxyReq, context) {
function createProxyEntry (target, usersOnProxyReq, usersOnError, context) {
// #2478
// There're a little-known use case that the `target` field is an object rather than a string
// https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/https.md
Expand Down Expand Up @@ -97,7 +97,7 @@ module.exports = function prepareProxy (proxy, appPublicFolder) {
proxyReq.setHeader('origin', target)
}
},
onError: onProxyError(target)
onError: usersOnError ? (err, req, res) => usersOnError(err, req, res, target) : onDefaultProxyError(target)
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ module.exports = function prepareProxy (proxy, appPublicFolder) {
)
process.exit(1)
}
const entry = createProxyEntry(config.target, config.onProxyReq, context)
const entry = createProxyEntry(config.target, config.onProxyReq, config.onError, context)
return Object.assign({}, defaultConfig, config, entry)
})
}
Expand Down Expand Up @@ -163,9 +163,9 @@ function resolveLoopback (proxy) {
return url.format(o)
}

// We need to provide a custom onError function for httpProxyMiddleware.
// We need to provide a default custom onError function for httpProxyMiddleware.
// It allows us to log custom error messages on the console.
function onProxyError (proxy) {
function onDefaultProxyError (proxy) {
return (err, req, res) => {
const host = req.headers && req.headers.host
console.log(
Expand Down