Skip to content

Commit

Permalink
fix uncaught error for blank controller, improve logging coloring, ve…
Browse files Browse the repository at this point in the history
…rsion bump
  • Loading branch information
realtux committed Jan 1, 2025
1 parent 9b889bb commit a77a104
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
17 changes: 17 additions & 0 deletions lib/core/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import chalk from 'chalk';

export default new class {

error(...msg) {
console.log(`${chalk.red('!')}`, ...msg);
}

warning(...msg) {
console.log(`${chalk.yellow('*')}`, ...msg);
}

info(...msg) {
console.log(`${chalk.cyan('*')}`, ...msg);
}

}
20 changes: 16 additions & 4 deletions lib/http/express.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from '#exa/core/config.js';
import logger from '#exa/core/logger.js';
import chalk from 'chalk';
import cors from 'cors';
import Express from 'express';
Expand Down Expand Up @@ -35,9 +36,18 @@ export const init = async () => {
// mounted http controller routes
let files = await glob(`${config.paths.root}${config.paths.http}/**/*.js`);

let route_count = 0;

for (const file of files) {
let controller_path = file.replace(config.paths.root + '/', '');

let { default: controller } = await import(file);

if (!controller?.routes) {
logger.error(`routes object missing from controller (${controller_path}), skipping`);
continue;
}

for (const [route, handler] of Object.entries(controller.routes)) {
let [method, path] = route.split(' ');

Expand All @@ -50,19 +60,21 @@ export const init = async () => {
}

if (!controller[handler]) {
console.log(`${chalk.red('*')} cannot bind to ${file}::${handler}, skipping`);
logger.error(`cannot bind to ${file}::${handler}, skipping`);
continue;
}

app[method](path, ...middleware.concat(controller[handler].bind(controller)));

++route_count;
}
}

// console.log(`mounted ${chalk.green(controllers.length)} routes`);
logger.info(`mounted ${chalk.green(route_count)} routes`);

// start server
const server = app.listen(config.http.port, () => {
console.log(`server running on ${config.http.host}:${config.http.port}`);
logger.info(`server running on ${config.http.host}:${config.http.port}`);
});

if (config.http.websocket) {
Expand Down Expand Up @@ -91,7 +103,7 @@ export const init = async () => {
path_mapper['/'].connection(socket, req);
}
} catch {
console.log('no matching websocket path or fallback file');
logger.error('no matching websocket path or fallback file');
socket.close();
}
});
Expand Down
26 changes: 14 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import { fileURLToPath } from 'node:url';
import { DateTime } from 'luxon';
import { dirname } from 'node:path';
import fs from 'node:fs/promises';
import logger from '#exa/core/logger.js';

process.on('uncaughtException', err => {
console.log(chalk.red('uncaught last resort'), chalk.yellow(DateTime.now().toISO()));
logger.error(`exa uncaught last resort:`);
logger.error(chalk.red('time'), DateTime.now().toISO())

if (err instanceof Error) {
console.error(chalk.cyan('error:'), err.message);
console.error(chalk.cyan('stack:'), err.stack.toString());
logger.error(chalk.red('error:'), err.message);
logger.error(chalk.red('stack:'), err.stack.toString());
} else if (typeof err === 'string') {
console.error(err);
logger.error(err);
} else if (typeof err === 'object') {
console.error(err.toString());
logger.error(err.toString());
}
});

Expand Down Expand Up @@ -62,9 +64,9 @@ export default new class {

if (!quiet) {
console.log();
console.log(`${chalk.yellow('exa.js')} by tux - v${version}`);
console.log(`${chalk.green('exa.js')} by tux - v${version}`);
console.log('-----------------------------');
console.log(`${chalk.green(DateTime.now().toISO())}`);
console.log(`${chalk.gray(DateTime.now().toISO())}`);
console.log('-----------------------------');
console.log(`mode: ${config.environment.development ? 'development' : 'production'}`);
}
Expand All @@ -75,7 +77,7 @@ export default new class {
*/
if (config.database.use) {
if (!quiet) {
console.log('initializing database...');
logger.info('initializing database...');
}

await init_db();
Expand All @@ -100,7 +102,7 @@ export default new class {
if (scripts[command]) {
await scripts[command].run(...process.argv.slice(4))
} else {
console.log(`${chalk.red('*')} no script called ${command} found`);
logger.error(`no script called ${command} found`);
}
} else {
/**
Expand All @@ -111,9 +113,9 @@ export default new class {
await init_express();
}
} catch (e) {
console.log('exa uncaught:');
console.log(e.message);
console.log(e.stack);
logger.error('exa uncaught:');
logger.error(e.message);
logger.error(e.stack);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@exajs/core",
"type": "module",
"author": "Brian Seymour <@realtux>",
"version": "0.0.18",
"version": "0.0.19",
"description": "modern opinionated node.js framework",
"license": "MIT",
"homepage": "https://github.com/realtux/exa",
Expand Down

0 comments on commit a77a104

Please sign in to comment.