Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Jul 14, 2024
1 parent 72b4f06 commit 2aac950
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions packages/signalizejs/src/modules/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ export default async ({ resolve}, options) => {

/** @type {import('../../types/modules/logger').Levels[]} */
const enabledLevels = options?.levels ?? ['error'];

let handlerProcessingRequest = false;
/**
* @param {import('../../types/modules/logger').Log} log
* @returns {void}
* @returns {Promise<void>}
*/
const handler = (log) => {
const body = { log, url: window.location.href };
const handler = async (log) => {
if (handlerProcessingRequest) {
return;
}

handlerProcessingRequest = true;
log.url = window.location.href;

const body = { log };
const logStopped = !dispatch(`logger:${log.type}`, body);

if (!logStopped) {
void ajax(options.url, { body });
await ajax(options.url, { body });
}

handlerProcessingRequest = false;
};

for (const level of enabledLevels) {
Expand All @@ -25,12 +35,12 @@ export default async ({ resolve}, options) => {
* @returns {void}
*/
console[level] = (...args) => {
handler({ type: 'error', message: args.join(',') });
void handler({ type: 'error', message: args.join(',') });
originalCall(...args);
};
}

if ('error' in enabledLevels) {
if (enabledLevels.includes('error')) {
/**
* @param {Event | string} message - The error message or event object.
* @param {string} [file] - The file associated with the error (optional).
Expand All @@ -45,16 +55,14 @@ export default async ({ resolve}, options) => {
return;
}

handler({
void handler({
type: 'error',
message: message instanceof Event ? message.type : message,
file: file ?? null,
lineNumber: lineNumber ?? 0,
columnNumber: columnNumber ?? 0,
stack: error?.stack === undefined ? null : error.stack
});

console.error(message, file, lineNumber, columnNumber, error);
};

/**
Expand All @@ -65,7 +73,8 @@ export default async ({ resolve}, options) => {
* @returns {void}
*/
window.addEventListener('unhandledrejection', (event) => {
handler({ type: 'error', message: event.reason });
console.log(event);
void handler({ type: 'error', message: event.reason });
});
}
};

0 comments on commit 2aac950

Please sign in to comment.