@@ -4,7 +4,7 @@ import cors from 'cors'
44import { ZenStackMiddleware } from '@zenstackhq/server/express'
55import { ZModelConfig } from './zmodel-parser'
66import { getNodeModulesFolder , getPrismaVersion , getZenStackVersion } from './utils/version-utils'
7- import { blue , grey } from 'colors'
7+ import { blue , grey , red } from 'colors'
88import semver from 'semver'
99import { CliError } from './cli-error'
1010
@@ -168,23 +168,29 @@ async function loadZenStackModules(
168168 enums = prismaModule . $Enums || { }
169169 }
170170
171- let zenstackAbsPath = zenstackPath
171+ const zenstackAbsPath = zenstackPath
172172 ? path . isAbsolute ( zenstackPath )
173173 ? zenstackPath
174174 : path . join ( process . cwd ( ) , zenstackPath )
175175 : undefined
176176
177- if ( zenstackAbsPath ) {
178- const modelMetaPath = path . join ( zenstackAbsPath , 'model-meta' )
177+ let modelMetaPath : string | undefined
178+ try {
179+ if ( zenstackAbsPath ) {
180+ modelMetaPath = path . join ( zenstackAbsPath , 'model-meta' )
181+ } else {
182+ modelMetaPath = '@zenstackhq/runtime/model-meta'
183+ }
179184 modelMeta = require ( modelMetaPath ) . default
180- } else {
181- modelMeta = require ( '@zenstackhq/runtime/model-meta' ) . default
185+ } catch {
186+ throw new CliError (
187+ `Failed to load ZenStack generated model meta from: ${ modelMetaPath } \n` +
188+ `Please run \`zenstack generate\` first or specify the correct output directory of ZenStack generated modules using the \`-z\` option.`
189+ )
182190 }
183191
184192 if ( ! modelMeta . models ) {
185- throw new CliError (
186- 'ZenStack generated modules not found. Please make sure `zenstack generate` has been run or specify the correct path using the `-z` option.'
187- )
193+ throw new CliError ( `Generated model meta not found. Please run \`zenstack generate\` first.` )
188194 }
189195
190196 const zenstackVersion = getZenStackVersion ( )
@@ -247,6 +253,19 @@ export async function startServer(options: ServerOptions) {
247253 console . log ( `ZenStack Studio is running at: ${ blue ( 'https://studio.zenstack.dev' ) } ` )
248254 } )
249255
256+ server . on ( 'error' , ( err : NodeJS . ErrnoException ) => {
257+ if ( err . code === 'EADDRINUSE' ) {
258+ console . error (
259+ red (
260+ `Port ${ options . port } is already in use. Please choose a different port using -p option.`
261+ )
262+ )
263+ } else {
264+ throw new CliError ( `Failed to start the server: ${ err . message } ` )
265+ }
266+ process . exit ( 1 )
267+ } )
268+
250269 // Graceful shutdown
251270 process . on ( 'SIGTERM' , async ( ) => {
252271 server . close ( ( ) => {
0 commit comments