Commit 7a3624a5 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[enh] Centralize program exit codes

parent f682ddef
......@@ -83,11 +83,6 @@ export const duniterDocument2str = (type:DuniterDocument) => {
}
}
export const ErrorConstants = {
EXIT_CODE_MINDEX_WRITING_ERROR: 500
}
export const CommonConstants = {
FORMATS: {
......
export enum ExitCodes {
OK = 0,
UNHANDLED_ERROR = 1,
UNCAUGHT_EXCEPTION = 2,
DUNITER_NOT_RUNNING = 2,
SIGINT = 3,
SYNC_FAIL = 50,
FORCE_CLOSE_AFTER_ERROR = 100,
MINDEX_WRITING_ERROR = 500,
}
import {SQLiteDriver} from "../../drivers/SQLiteDriver"
import {MonitorExecutionTime} from "../../../debug/MonitorExecutionTime"
import {ErrorConstants} from "../../../common-libs/constants"
import {NewLogger} from "../../../logger"
import {ExitCodes} from "../../../common-libs/exit-codes"
export class SqliteNodeIOManager<T> {
......@@ -24,7 +24,7 @@ export class SqliteNodeIOManager<T> {
.then(() => this.driver.executeAll(sql, params))
.catch(e => {
NewLogger().error(e)
process.exit(ErrorConstants.EXIT_CODE_MINDEX_WRITING_ERROR)
process.exit(ExitCodes.MINDEX_WRITING_ERROR)
})
}
......
......@@ -30,6 +30,7 @@ import {CommonConstants} from "../../lib/common-libs/constants"
import {DataErrors} from "../../lib/common-libs/errors"
import {NewLogger} from "../../lib/logger"
import {CrawlerConstants} from "./lib/constants"
import {ExitCodes} from "../../lib/common-libs/exit-codes"
export const CrawlerDependency = {
duniter: {
......@@ -144,7 +145,7 @@ export const CrawlerDependency = {
process.on('unhandledRejection', (reason) => {
if (reason.message === DataErrors[DataErrors.NO_NODE_FOUND_TO_DOWNLOAD_CHUNK]) {
NewLogger().error('Synchronization interrupted: no node was found to continue downloading after %s tries.', CrawlerConstants.SYNC_MAX_FAIL_NO_NODE_FOUND)
process.exit(1)
process.exit(ExitCodes.SYNC_FAIL)
}
})
......
......@@ -6,6 +6,7 @@ import {DataErrors} from "../../../../../lib/common-libs/errors"
import {NewLogger} from "../../../../../lib/logger"
import {ISyncDownloader} from "../ISyncDownloader"
import {Watcher} from "../Watcher"
import {ExitCodes} from "../../../../../lib/common-libs/exit-codes"
export class ValidatorStream extends Readable {
......@@ -67,7 +68,7 @@ export class ValidatorStream extends Readable {
await new Promise((res) => setTimeout(res, 3000))
if (failures >= 15) {
NewLogger().error('Could not get a validation from remote blockchain after %s trials. Stopping sync.', failures)
process.exit(1)
process.exit(ExitCodes.SYNC_FAIL)
}
block = null
}
......
......@@ -14,6 +14,7 @@
import {ConfDTO} from "../lib/dto/ConfDTO"
import {Server} from "../../server"
import {Directory, RealFS} from "../lib/system/directory"
import {ExitCodes} from "../lib/common-libs/exit-codes"
const constants = require('../lib/constants');
const Tail = require("tail").Tail
......@@ -80,10 +81,10 @@ module.exports = {
const pid = server.getDaemon().status()
if (pid) {
console.log('Duniter is running using PID %s.', pid)
process.exit(0)
process.exit(ExitCodes.OK)
} else {
console.log('Duniter is not running.')
process.exit(2)
process.exit(ExitCodes.DUNITER_NOT_RUNNING)
}
}
}, {
......
......@@ -18,6 +18,7 @@ import {PowWorker} from "./PowWorker"
import {FileDAL} from "../../../lib/dal/fileDAL"
import {Underscore} from "../../../lib/common-libs/underscore"
import {ProofAsk} from "./blockProver"
import {ExitCodes} from "../../../lib/common-libs/exit-codes"
const nuuid = require('node-uuid');
const cluster = require('cluster')
......@@ -263,7 +264,7 @@ if (cluster.isMaster) {
} else {
process.on("SIGTERM", function() {
process.exit(0)
process.exit(ExitCodes.OK)
});
createPowWorker()
......
......@@ -23,6 +23,7 @@ import {rawer} from "../../../lib/common-libs/index"
import {ProcessCpuProfiler} from "../../../ProcessCpuProfiler"
import {PowDAL} from "../../../lib/dal/fileDALs/PowDAL";
import {Directory} from "../../../lib/system/directory"
import {ExitCodes} from "../../../lib/common-libs/exit-codes"
const querablep = require('querablep');
......@@ -47,7 +48,7 @@ export function createPowWorker() {
});
process.on('unhandledRejection', () => {
process.exit()
process.exit(ExitCodes.OK)
})
process.on('message', async (message) => {
......
#!/usr/bin/env node
"use strict";
const ExitCodes = require("../app/lib/common-libs/exit-codes").ExitCodes;
const logger = require("../app/lib/logger").NewLogger();
// Specific errors handling
......@@ -8,7 +9,7 @@ process.on('uncaughtException', (err) => {
// Dunno why this specific exception is not caught
if (err.code && err.code !== "EADDRNOTAVAIL" && err.code !== "EINVAL" && err.code !== "ENOENT") {
logger.error(err.stack || err.message || err);
process.exit(2);
process.exit(ExitCodes.UNCAUGHT_EXCEPTION);
}
});
......@@ -18,13 +19,13 @@ process.on('uncaughtException', (err) => {
const stack = require('../index').Statics.autoStack();
await stack.executeStack(process.argv);
// Everything went well, close Duniter quietly.
process.exit();
process.exit(ExitCodes.OK);
} catch (e) {
// If an unhandled error occured
logger.error(e.stack || e.message || e);
process.exit(1);
process.exit(ExitCodes.UNHANDLED_ERROR);
} finally {
// If we did not succeed to close before, force close with error.
process.exit(100);
process.exit(ExitCodes.FORCE_CLOSE_AFTER_ERROR);
}
})()
......@@ -28,6 +28,7 @@ import {Directory} from "./app/lib/system/directory"
import {Underscore} from "./app/lib/common-libs/underscore"
import {CliCommand, DuniterDependency, DuniterModule} from "./app/modules/DuniterModule"
import {ProgramOptions} from "./app/lib/common-libs/programOptions"
import {ExitCodes} from "./app/lib/common-libs/exit-codes"
const path = require('path');
const constants = require('./app/lib/constants');
......@@ -319,10 +320,10 @@ export class Stack {
// Save DB
try {
await server.disconnect();
process.exit();
process.exit(ExitCodes.OK);
} catch (e) {
logger.error(e);
process.exit(3);
process.exit(ExitCodes.SIGINT);
}
}
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment