Start proxy during site start and improve error wording - #1089
Conversation
| .on( 'error', ( err ) => { | ||
| console.log( err ); | ||
| console.error( `Error starting proxy server on port 80:`, err ); | ||
| console.error( `Error starting proxy server on port 80` ); |
There was a problem hiding this comment.
Let's clean up the error logging, we are logging this error too many times.
| buttons: [ __( 'OK' ) ], | ||
| } ); | ||
| return false; | ||
| throw new Error( 'Studio failed to start the proxy server' ); |
There was a problem hiding this comment.
Let's move the part responsible for dialog to the renderer.
There was a problem hiding this comment.
It looks a bit odd that we throw a new exception for known error types but just return false for unknown error types. The known error types indicate that port 80 is busy, but other errors also indicate that Studio failed to start the proxy server.
There was a problem hiding this comment.
I've updated that logic to handle error cases only through exceptions and cleaned up the logic around false.
There was a problem hiding this comment.
Code LGTM, and a much better user experience 👍
Just FYI we have this related PR: #1090 there might be some code merging to do, and we should retest, depending on what is merged first.
My only suggestion would be to include unit tests for this scenario.
sejas
left a comment
There was a problem hiding this comment.
It works as expected. I confirm that the alert appears if port 80 is already in use. I also confirm that sites without a custom domain function as expected without any errors, even if port 80 is occupied. After freeing port 80, I was able to start two Studio sites with custom domains.
Great work!
Here are the logs when running two sites with custom domains:
Server stopped
Stopped watching for file changes
SQLite folder already exists. Skipping download.
Domain example-2.wp.local added to hosts file for port 8901
Proxy server started on port 80
Custom domain proxy server started successfully
Starting server for 'example 2'
directory: /Users/macbookpro/Studio/example-2
mode: wordpress
php: 8.2
wp: latest
WordPress latest folder already exists. Skipping download.
SQLite folder already exists. Skipping download.
Server running at http://example-2.wp.local
Server started for 'example 2'
Domain example.wp.local added to hosts file for port 8900
Custom domain proxy server started successfully
Starting server for 'example'
directory: /Users/macbookpro/Studio/example
mode: wordpress
php: 8.2
wp: latest
WordPress latest folder already exists. Skipping download.
SQLite folder already exists. Skipping download.
Server running at http://example.wp.local
Server started for 'example'
| getIpcApi().showErrorMessageBox( { | ||
| title: __( 'Studio failed to initialize custom domains' ), | ||
| message: __( | ||
| 'Studio needs to use port 80, but it’s already in use by another app. Close any local development apps and restart Studio.' |
There was a problem hiding this comment.
Should we mention that the port 80 is only necessary for custom domains?
There was a problem hiding this comment.
The title says it failed to initialize custom domains, but it makes sense to clarify the message. I've added that in c82c902
| try { | ||
| const proxyStarted = await startProxyServer(); | ||
| if ( ! proxyStarted ) { | ||
| console.warn( | ||
| 'Failed to start custom domain proxy server - custom domains will require port numbers' | ||
| ); | ||
| } | ||
| console.log( 'Custom domain proxy server started successfully' ); |
There was a problem hiding this comment.
No need to change anything. Just an observation. I noticed we display Custom domain proxy server started successfully each time we start a server with custom domain.
I think it's innocuous as we also display the Domain example.wp.local added to hosts file for port 8900
# Conflicts: # src/lib/proxy-server.ts
dc5b1ee to
c82c902
Compare
| } ); | ||
| if ( | ||
| error instanceof Error && | ||
| error.message.includes( 'Studio failed to start the proxy server' ) |
There was a problem hiding this comment.
If this error only indicates that port 80 is busy, I would argue we should use a more specific error message.
There was a problem hiding this comment.
Actually, as those are used internally to mitigate IPC handler that flattens our exceptions, we could change descriptive messages to codes e.g. PROXY_ERROR_80 or prepend messages with the prefix e.g. PROXY_ERROR_80: 'Studio failed to [...]. Thoughts?
There was a problem hiding this comment.
I agree we could use a message like PROXY_ERROR_80, since this is only used internally 👍

Related issues
Proposed Changes
A "Custom domain set up failed" error is displayed immediately after user starts Studio, even if the user doesn't use a custom domain. I propose to improve Proxy error wording to be more informative about what happened:
Also, I delay starting the proxy until the user tries to start a site that uses custom domains, so users who have port 80 busy and start Studio won't see error immediately.
Testing Instructions
Pre-merge Checklist