forked from svrjs/svrjs
Create the server error handler and temporary startup script.
This commit is contained in:
parent
489ec9741a
commit
45815957d5
4 changed files with 123 additions and 11 deletions
53
src/handlers/serverErrorHandler.js
Normal file
53
src/handlers/serverErrorHandler.js
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
const os = require("os");
|
||||||
|
const cluster = require("../utils/clusterBunShim.js");
|
||||||
|
const serverErrorDescs = require("../res/serverErrorDescriptions.js");
|
||||||
|
let serverconsole = {};
|
||||||
|
let attmts = 5;
|
||||||
|
let attmtsRedir = 5;
|
||||||
|
|
||||||
|
function serverErrorHandler(err, isRedirect, server, start) {
|
||||||
|
if (isRedirect) attmtsRedir--;
|
||||||
|
else attmts--;
|
||||||
|
if (cluster.isPrimary === undefined && (isRedirect ? attmtsRedir : attmts)) {
|
||||||
|
serverconsole.locerrmessage(
|
||||||
|
serverErrorDescs[err.code]
|
||||||
|
? serverErrorDescs[err.code]
|
||||||
|
: serverErrorDescs["UNKNOWN"],
|
||||||
|
);
|
||||||
|
serverconsole.locmessage(
|
||||||
|
(isRedirect ? attmtsRedir : attmts) + " attempts left.",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// TODO: worker message listener
|
||||||
|
/*try {
|
||||||
|
process.send("\x12ERRLIST" + (isRedirect ? attmtsRedir : attmts) + err.code);
|
||||||
|
} catch (err) {
|
||||||
|
// Probably main process exited
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
if ((isRedirect ? attmtsRedir : attmts) > 0) {
|
||||||
|
server.close();
|
||||||
|
setTimeout(start, 900);
|
||||||
|
} else {
|
||||||
|
// TODO: worker message listener
|
||||||
|
/*try {
|
||||||
|
if (cluster.isPrimary !== undefined) process.send("\x12ERRCRASH" + err.code);
|
||||||
|
} catch (err) {
|
||||||
|
// Probably main process exited
|
||||||
|
}*/
|
||||||
|
setTimeout(function () {
|
||||||
|
var errno = os.constants.errno[err.code];
|
||||||
|
process.exit(errno !== undefined ? errno : 1);
|
||||||
|
}, 50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverErrorHandler.resetAttempts = (isRedirect) => {
|
||||||
|
if (isRedirect) attmtsRedir = 5;
|
||||||
|
else attmts = 5;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (serverconsoleO) => {
|
||||||
|
serverconsole = serverconsoleO;
|
||||||
|
return serverErrorHandler;
|
||||||
|
};
|
27
src/index.js
27
src/index.js
|
@ -623,6 +623,10 @@ const clientErrorHandler = require("./handlers/clientErrorHandler.js")(
|
||||||
serverconsole,
|
serverconsole,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const serverErrorHandler = require("./handlers/serverErrorHandler.js")(
|
||||||
|
serverconsole,
|
||||||
|
);
|
||||||
|
|
||||||
let server = {};
|
let server = {};
|
||||||
let server2 = {};
|
let server2 = {};
|
||||||
|
|
||||||
|
@ -643,6 +647,13 @@ server2.on(
|
||||||
"connect",
|
"connect",
|
||||||
process.serverConfig.disableToHTTPSRedirect ? proxyHandler : noproxyHandler,
|
process.serverConfig.disableToHTTPSRedirect ? proxyHandler : noproxyHandler,
|
||||||
);
|
);
|
||||||
|
server2.on("error", (err) => {
|
||||||
|
serverErrorHandler(err, true, server2, start);
|
||||||
|
});
|
||||||
|
server2.on("listening", () => {
|
||||||
|
serverErrorHandler.resetAttempts(true);
|
||||||
|
// TODO: listeningMessage();
|
||||||
|
});
|
||||||
|
|
||||||
// Create HTTP server
|
// Create HTTP server
|
||||||
if (process.serverConfig.enableHTTP2 == true) {
|
if (process.serverConfig.enableHTTP2 == true) {
|
||||||
|
@ -740,6 +751,13 @@ server.on("request", requestHandler);
|
||||||
server.on("checkExpectation", requestHandler);
|
server.on("checkExpectation", requestHandler);
|
||||||
server.on("connect", proxyHandler);
|
server.on("connect", proxyHandler);
|
||||||
server.on("clientError", clientErrorHandler);
|
server.on("clientError", clientErrorHandler);
|
||||||
|
server.on("error", function (err) {
|
||||||
|
serverErrorHandler(err, false, server, start);
|
||||||
|
});
|
||||||
|
server.on("listening", () => {
|
||||||
|
serverErrorHandler.resetAttempts(false);
|
||||||
|
// TODO: listeningMessage();
|
||||||
|
});
|
||||||
|
|
||||||
if (process.serverConfig.secure) {
|
if (process.serverConfig.secure) {
|
||||||
server.prependListener("connection", function (sock) {
|
server.prependListener("connection", function (sock) {
|
||||||
|
@ -856,7 +874,8 @@ middleware.forEach((middlewareO) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: HTTP ports
|
// TODO: HTTP ports and start script
|
||||||
|
function start() {
|
||||||
// Listen HTTP server to port 3000
|
// Listen HTTP server to port 3000
|
||||||
server.listen(3000);
|
server.listen(3000);
|
||||||
|
|
||||||
|
@ -866,6 +885,8 @@ if (configJSONRErr) throw configJSONRErr;
|
||||||
if (configJSONPErr) throw configJSONPErr;
|
if (configJSONPErr) throw configJSONPErr;
|
||||||
if (certificateError) throw certificateError;
|
if (certificateError) throw certificateError;
|
||||||
if (sniReDos) throw new Error("SNI REDOS!!!");
|
if (sniReDos) throw new Error("SNI REDOS!!!");
|
||||||
|
}
|
||||||
|
|
||||||
modLoadingErrors.forEach((modLoadingError) => {
|
modLoadingErrors.forEach((modLoadingError) => {
|
||||||
console.log('Error while loading "' + modLoadingError.modName + '" mod:');
|
console.log('Error while loading "' + modLoadingError.modName + '" mod:');
|
||||||
console.log(modLoadingError.error);
|
console.log(modLoadingError.error);
|
||||||
|
@ -879,7 +900,7 @@ if (SSJSError) {
|
||||||
if (cluster.isPrimary || cluster.isPrimary === undefined) {
|
if (cluster.isPrimary || cluster.isPrimary === undefined) {
|
||||||
// Crash handler
|
// Crash handler
|
||||||
function crashHandlerMaster(err) {
|
function crashHandlerMaster(err) {
|
||||||
serverconsole.locerrmessage("SVR.JS worker just crashed!!!");
|
serverconsole.locerrmessage("SVR.JS main process just crashed!!!");
|
||||||
serverconsole.locerrmessage("Stack:");
|
serverconsole.locerrmessage("Stack:");
|
||||||
serverconsole.locerrmessage(
|
serverconsole.locerrmessage(
|
||||||
err.stack ? generateErrorStack(err) : String(err),
|
err.stack ? generateErrorStack(err) : String(err),
|
||||||
|
@ -976,3 +997,5 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// HTTP error descriptions
|
// HTTP error descriptions
|
||||||
var serverHTTPErrorDescs = {
|
const serverHTTPErrorDescs = {
|
||||||
200: "The request succeeded! :)",
|
200: "The request succeeded! :)",
|
||||||
201: "A new resource has been created.",
|
201: "A new resource has been created.",
|
||||||
202: "The request has been accepted for processing, but the processing has not been completed.",
|
202: "The request has been accepted for processing, but the processing has not been completed.",
|
||||||
|
|
36
src/res/serverErrorDescriptions.js
Normal file
36
src/res/serverErrorDescriptions.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
// Server error descriptions
|
||||||
|
const serverErrorDescs = {
|
||||||
|
EADDRINUSE: "Address is already in use by another process.",
|
||||||
|
EADDRNOTAVAIL: "Address is not available on this machine.",
|
||||||
|
EACCES:
|
||||||
|
"Permission denied. You may not have sufficient privileges to access the requested address.",
|
||||||
|
EAFNOSUPPORT:
|
||||||
|
"Address family not supported. The address family (IPv4 or IPv6) of the requested address is not supported.",
|
||||||
|
EALREADY:
|
||||||
|
"Operation already in progress. The server is already in the process of establishing a connection on the requested address.",
|
||||||
|
ECONNABORTED:
|
||||||
|
"Connection aborted. The connection to the server was terminated abruptly.",
|
||||||
|
ECONNREFUSED:
|
||||||
|
"Connection refused. The server refused the connection attempt.",
|
||||||
|
ECONNRESET:
|
||||||
|
"Connection reset by peer. The connection to the server was reset by the remote host.",
|
||||||
|
EDESTADDRREQ:
|
||||||
|
"Destination address required. The destination address must be specified.",
|
||||||
|
EINVAL: "Invalid argument (invalid IP address?).",
|
||||||
|
ENETDOWN:
|
||||||
|
"Network is down. The network interface used for the connection is not available.",
|
||||||
|
ENETUNREACH:
|
||||||
|
"Network is unreachable. The network destination is not reachable from this host.",
|
||||||
|
ENOBUFS:
|
||||||
|
"No buffer space available. Insufficient buffer space is available for the server to process the request.",
|
||||||
|
ENOTFOUND: "Domain name doesn't exist (invalid IP address?).",
|
||||||
|
ENOTSOCK: "Not a socket. The file descriptor provided is not a valid socket.",
|
||||||
|
EPROTO: "Protocol error. An unspecified protocol error occurred.",
|
||||||
|
EPROTONOSUPPORT:
|
||||||
|
"Protocol not supported. The requested network protocol is not supported.",
|
||||||
|
ETIMEDOUT:
|
||||||
|
"Connection timed out. The server did not respond within the specified timeout period.",
|
||||||
|
UNKNOWN: "There was an unknown error with the server.",
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = serverErrorDescs;
|
Reference in a new issue