Sorry, my fault, I misunderstood you! Please note that throw in the top level of main.swift is meaningless because the only process who will catch that error is the OS. Please try the code below:
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
if UserController.setup() {
let server = HTTPServer()
let serverRoutes = ServerRoutes()
serverRoutes.addRoutes(server: server)
server.serverPort = 8181
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
} catch (let panic) {
print("panic: \(panic)")
} //end server starting
} else {
// here is the code that prompting user to finish the setup
// before running the server. The program will display this
// message and automatically exit the process (end of program)
print("Setup is not completed.")
}//end if
Alternatively, you can also use the exit() function to preform a quit from the main.swift, with importing Foundation (the standard library), as below:
//
// main.swift
//
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import Foundation
if !UserController.setup() {
// This is the error I'm throwing and concerned about.
print("ServerStartupError.FailedUserControllerSetup")
exit(-1)
}//end if
let server = HTTPServer()
let serverRoutes = ServerRoutes()
serverRoutes.addRoutes(server: server)
server.serverPort = 8181
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
} catch (let panic) {
print("Panic: \(panic)")
}
exit(0)
Both methods have pros and cons. The first one doesn't require anything else while the second one needs Foundation Library; Also I would say that the second one is more traditional and could provide an exit code, which allows you catch the error by wrapping it up in a customize boot loader or something. Unix / Linux uses exit code to check out the application process information and in some cases you can see these exit codes in system log, if available.