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(server): error early on incompatible config (apiHost and apiUrl) #9808

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
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
47 changes: 31 additions & 16 deletions packages/cli/src/commands/serveWebHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import execa from 'execa'

import { getPaths } from '@redwoodjs/project-config'

import { exitWithError } from '../lib/exit'

export const webSsrServerHandler = async () => {
await execa('yarn', ['rw-serve-fe'], {
cwd: getPaths().web.base,
Expand All @@ -11,21 +13,34 @@ export const webSsrServerHandler = async () => {
}

export const webServerHandler = async (argv) => {
await execa(
'yarn',
[
'rw-web-server',
'--port',
argv.port,
'--socket',
argv.socket,
'--api-host',
argv.apiHost,
],
{
cwd: getPaths().base,
stdio: 'inherit',
shell: true,
try {
await execa(
'yarn',
[
'rw-web-server',
'--port',
argv.port,
'--socket',
argv.socket,
'--api-host',
argv.apiHost,
],
{
cwd: getPaths().base,
stdio: 'inherit',
shell: true,
}
)
} catch (e) {
// `@redwoodjs/web-server` uses a custom error exit code to tell this handler that an error has already been handled.
// While any other exit code than `0` is considered an error, there seems to be some conventions around some of them
// like `127`, etc. We chose 64 because it's in the range where there deliberately aren't any previous conventions.
// See https://tldp.org/LDP/abs/html/exitcodes.html.
if (e.exitCode === 64) {
process.exitCode = 1
return
}
)

exitWithError(e)
}
}
28 changes: 27 additions & 1 deletion packages/web-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ interface Opts {
apiHost?: string
}

// no help option...
function isFullyQualifiedUrl(url: string) {
try {
// eslint-disable-next-line no-new
new URL(url)
return true
} catch (e) {
return false
}
}

async function serve() {
// Parse server file args
Expand All @@ -39,6 +47,24 @@ async function serve() {
const port = options.port ? parseInt(options.port) : redwoodConfig.web.port
const apiUrl = redwoodConfig.web.apiUrl

if (!options.apiHost && !isFullyQualifiedUrl(apiUrl)) {
console.error(
`${chalk.red('Error')}: If you don't provide ${chalk.magenta(
'apiHost'
Tobbe marked this conversation as resolved.
Show resolved Hide resolved
)}, ${chalk.magenta(
'apiUrl'
)} needs to be a fully-qualified URL. But ${chalk.magenta(
'apiUrl'
)} is ${chalk.yellow(apiUrl)}.`
)
// We're using a custom error exit code here to tell `@redwoodjs/cli` that this error has been handled.
// While any other exit code than `0` is considered an error, there seems to be some conventions around some of them
// like `127`, etc. We chose 64 because it's in the range where there deliberately aren't any previous conventions.
// See https://tldp.org/LDP/abs/html/exitcodes.html.
process.exitCode = 64
return
}

const tsServer = Date.now()

// Load .env files
Expand Down
Loading