Skip to content

Commit

Permalink
feat: 16-error-control
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmartin authored and jorgecasar committed Jan 12, 2024
1 parent f67d786 commit 5784fe0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
45 changes: 45 additions & 0 deletions src/helpers/colours.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* paints a given text with the given color and then resets the color palette
* @param {any} text - the text to paint
* @param {string} color - the desired color to paint the text
* @return {string} - the painted text
*/

function paintText(text, color) {
return color + text + colours.reset;
}

const colours = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
underscore: '\x1b[4m',
blink: '\x1b[5m',
reverse: '\x1b[7m',
hidden: '\x1b[8m',
fg: {
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
gray: '\x1b[90m',
crimson: '\x1b[38m', // Scarlet
},
bg: {
black: '\x1b[40m',
red: '\x1b[41m',
green: '\x1b[42m',
yellow: '\x1b[43m',
blue: '\x1b[44m',
magenta: '\x1b[45m',
cyan: '\x1b[46m',
white: '\x1b[47m',
gray: '\x1b[100m',
crimson: '\x1b[48m',
},
};
export { colours, paintText };
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from 'node:fs';
import { program } from 'commander';
import startMockServer from './services/start-mock-server.js';
import { initWithConfigFile, initWithSchemaPaths, init } from './services/user-flow-steps.js';
import { RC_FILE_NAME } from './services/utils.js';
import { RC_FILE_NAME , MockRunnerError } from './services/utils.js';
import Logger from './utils/logger.js';
import { messages } from './utils/messages.js';

Expand Down Expand Up @@ -56,5 +56,11 @@ const main = async () => {
const config = await init();
return startMockServer(config.selectedSchemas);
};

main();
(async () => {
try {
await main();
} catch (e) {
const err = new MockRunnerError(e.message, 500, 1, this.constructor.name);
err.showError();
}
})();
51 changes: 51 additions & 0 deletions src/services/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { colours, paintText } from '../helpers/colours';
import Logger from '../utils/logger.js';

/**
* The name of the config file
* @constant
Expand Down Expand Up @@ -38,3 +41,51 @@ export default {
TEMP_FOLDER_NAME,
verifyRemoteOrigin,
};

export class MockRunnerError extends Error {
/**
* Error class for the project
* @constructor
* @param {string} message - display message
* @param {number} code - HTTP code for easy identification problem
* @param {number} level - severity of the error [low = 0, medium = 1, critical = 2]
* @param {string} emitter - name of the function origin
* @param {object} opt - options default object on the Error node class
*/
constructor(message, code, level = 0, emitter, opt = {}) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
this.name = this.constructor.name;
this.emitter = emitter;
this.opt = opt;
this.code = code;
this.level = level;
Error.captureStackTrace(this);
}

/**
* Shows the error information on the log on a tidy way
* @async
* @return {Promise<void>}
*/
showError() {
let type = '';
switch (this.level) {
case 2:
type = colours.fg.red;
break;
case 1:
type = colours.fg.crimson;
break;
default:
type = colours.fg.cyan;
}
Logger.error(
`Error of level ${paintText(this.level, type)}, type ${paintText(this.code, colours.fg.gray)} over ${paintText(
this.emitter,
colours.fg.blue
)}`
);
Logger.info(`${this.stack}`);
}
}

0 comments on commit 5784fe0

Please sign in to comment.