yekonga-server
Version:
Yekonga Server
133 lines (114 loc) • 3.7 kB
JavaScript
// @ts-nocheck
;
const customError = {}
function logFunction(arg1, arg2, defaultTitle = 'Error', color = 'red') {
try {
var title = defaultTitle;
var _title = arg1;
var _text = arg2;
if (arg1 && !arg2) {
_title = null;
_text = arg1;
} else if (!arg1 && arg2) {
_title = null;
_text = arg2;
}
var customErrors = ['GraphQLError', 'AuthenticationError', 'ReadError', 'ValidationError', 'InternalServiceError', 'ForbiddenError', 'MongoError'];
var error = (typeof _text == 'string') ? _text.split("\n").shift() : "";
var errorObj = error.split(":");
if (_title) {
title = _title;
} else {
title = (errorObj.length > 1) ? (errorObj.shift() ? errorObj.shift().trim() : errorObj.shift()) : defaultTitle;
}
var message = error.substr((`${title}`.length + 2));
if (typeof _text == 'object' && _text.message) {
_text = _text.message;
}
if (customErrors.includes(title)) {
Yekonga.Helper.log(`${title}`, message, color);
} else if (!_title) {
message = (typeof _text == 'object') ? JSON.stringify(_text) : _text;
Yekonga.Helper.log(defaultTitle, message, color);
if (arg1) {
console.log(arg1)
} else if (arg2) {
console.log(arg2)
}
} else {
message = (typeof _text == 'object') ? JSON.stringify(_text) : _text;
Yekonga.Helper.log(title, message, color);
}
} catch (error) {
console.log(error);
}
}
console.error = (arg1, arg2) => {
try {
if (Yekonga.Config.debug) {
logFunction(arg1, arg2, 'Error', 'red');
}
} catch (error) {
console.log(error);
}
}
console.warn = (arg1, arg2) => {
logFunction(arg1, arg2, 'Warning', 'yellow');
}
console.info = (arg1, arg2) => {
logFunction(arg1, arg2, 'Info', 'lightBlue');
}
console.debug = (arg1, arg2) => {
logFunction(arg1, arg2, 'Debug', 'green');
}
process.on('uncaughtException', (error) => {
console.log(error)
if (error.code !== 'EPIPE') {
console.error(error);
} else {
console.warn(error);
}
});
customError.Read = class ReadError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = 'ReadError';
}
}
customError.Authentication = class AuthenticationError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = 'AuthenticationError';
}
}
customError.Validation = class ValidationError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = 'ValidationError';
}
}
customError.InternalService = class InternalServiceError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = 'InternalServiceError';
}
}
customError.Forbidden = class ForbiddenError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = 'ForbiddenError';
}
}
customError.Database = class DatabaseError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = 'DatabaseError';
}
}
module.exports = {...customError };