Skip to content

Commit

Permalink
perf(cli): main entry error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Aug 16, 2021
1 parent 008e444 commit 520cf9c
Show file tree
Hide file tree
Showing 4 changed files with 1,062 additions and 910 deletions.
1 change: 1 addition & 0 deletions example/hoth-quickstart/src/plugin/foo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {FastifyInstance} from 'fastify';

export default async function (fastify: FastifyInstance, opts: typeof autoConfig) {
console.log('foo plugin options', opts);
// throw new Error('test');
}

export const autoConfig = {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
},
"homepage": "https://github.com/cxtom/hoth#readme",
"dependencies": {
"@hoth/app-autoload": "^1.1.9",
"@hoth/logger": "^1.1.7",
"@hoth/app-autoload": "^1.1.15",
"@hoth/logger": "^1.1.8",
"@hoth/utils": "^1.1.4",
"chalk": "^4.1.0",
"close-with-grace": "^1.1.0",
Expand Down
27 changes: 17 additions & 10 deletions packages/cli/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import closeWithGrace from 'close-with-grace';

const listenAddressDocker = '0.0.0.0';

// eslint-disable-next-line @typescript-eslint/init-declarations
let fastify: typeof Fastify;

// 初始化全局变量
Expand Down Expand Up @@ -74,11 +75,6 @@ async function runFastify(opts: Args) {
pluginTimeout: 60 * 1000,
});

// eslint-disable-next-line
require('make-promises-safe').logError = function (error: Error | string) {
fastifyInstance.log.fatal(error instanceof Error ? {err: error} : error);
};

await fastifyInstance.register(appAutoload, {
apps,
});
Expand All @@ -99,6 +95,12 @@ async function runFastify(opts: Args) {
await fastifyInstance.close();
} as closeWithGrace.CloseWithGraceAsyncCallback);

// eslint-disable-next-line
require('make-promises-safe').logError = async function (error: Error | string) {
finalLogger(error instanceof Error ? error : null, error);
await fastifyInstance.close();
};

fastifyInstance.addHook('onClose', async () => {
closeListeners.uninstall();
});
Expand All @@ -107,10 +109,15 @@ async function runFastify(opts: Args) {
if (existsSync(rootEntryPath)) {
const entryMod = await loadModule(rootEntryPath);
entryMod[Symbol.for('skip-override')] = true;
await fastifyInstance.register(entryMod, {
apps,
rootPath
});
try {
await fastifyInstance.register(entryMod, {
apps,
rootPath
});
}
catch (err) {
finalLogger(err);
}
}

// warmup
Expand All @@ -123,7 +130,7 @@ async function runFastify(opts: Args) {
process.exit(-1);
}

let address;
let address = '';
if (opts.address) {
address = await fastifyInstance.listen(opts.port, opts.address);
}
Expand Down
Loading

0 comments on commit 520cf9c

Please sign in to comment.