diff --git a/core/fetch/src/fetch.ts b/core/fetch/src/fetch.ts index f9131f71f..cf0d5346a 100644 --- a/core/fetch/src/fetch.ts +++ b/core/fetch/src/fetch.ts @@ -37,7 +37,7 @@ export async function serviceRequest, TMeta = Re response = await fetch(options); } catch (err) { - logger.error('serviceRequest', (err as Error).message || 'fetch_failed', (err as Error).stack || err, options); + logger.error('serviceRequest', 'fetch_failed', err, options); throw new Error('fetch_failed'); } @@ -46,7 +46,7 @@ export async function serviceRequest, TMeta = Re responseText = await response.text(); } catch (err) { - logger.error('serviceRequest', 'invalid_response', (err as Error).message || err, { + logger.error('serviceRequest', 'invalid_response', err, { response, }); throw new Error('invalid_response'); @@ -57,7 +57,7 @@ export async function serviceRequest, TMeta = Re responseJson = JSON.parse(responseText); } catch (err) { - logger.error('serviceRequest', 'invalid_json', (err as Error).message || err, {responseText}); + logger.error('serviceRequest', 'invalid_json', err, {responseText}); throw new Error('invalid_json'); } @@ -289,7 +289,7 @@ async function _handleRetryPattern(options: Required): Promise { } } catch (err) { - logger.error('resourceProvider', 'fetch_failed', (err as Error).stack || err, {locale, url}); + logger.error('resourceProvider', 'fetch_failed', err, {locale, url}); // TODO: user error signal. return; } diff --git a/core/logger/README.md b/core/logger/README.md index 8adaa1628..c4f148665 100644 --- a/core/logger/README.md +++ b/core/logger/README.md @@ -148,7 +148,7 @@ try { ... } catch (err) { - logger.error('myMethod', 'error_code', (err as Error).stack || err, {a: 1, b: 2}); + logger.error('myMethod', 'error_code', err, {a: 1, b: 2}); } ``` @@ -175,7 +175,7 @@ Best practices to catch the error and log it: ```ts // Unhandled promise rejection (just log it) failPromiseTest().catch((err) => - logger.error('myMethod', (err as Error).message || 'error_code', (err as Error).stack || err) + logger.error('myMethod', (err as Error).message || 'error_code', err) ); // Handled promise rejection @@ -186,7 +186,7 @@ try { 'myMethod', 'error_code', 'failPromiseTest failed!, ' + (err as Error).message, - (err as Error).stack || err + err ); // do something to handle the error... } diff --git a/core/logger/src/type.ts b/core/logger/src/type.ts index 25f6aac4c..ed6aa7fa7 100644 --- a/core/logger/src/type.ts +++ b/core/logger/src/type.ts @@ -127,11 +127,11 @@ export interface AlwatrLogger { * ... * } * catch (err) { - * logger.error('myMethod', 'error_code', (err as Error).stack || err, {a: 1, b: 2}); + * logger.error('myMethod', 'error_code', err, {a: 1, b: 2}); * } * ``` */ - error(method: string, code: string, errorStack: string | unknown, ...args: unknown[]): void; + error(method: string, code: string, ...args: unknown[]): void; /** * Simple `console.debug` with styled scope. diff --git a/core/nano-server/src/nano-server.ts b/core/nano-server/src/nano-server.ts index bd7602112..e10f9d0ff 100644 --- a/core/nano-server/src/nano-server.ts +++ b/core/nano-server/src/nano-server.ts @@ -247,7 +247,8 @@ export class AlwatrNanoServer { this._notFoundListener(connection); } } - catch (err) { + catch (_err) { + const err = _err as Error; this._logger.error('handleRequest', 'http_server_middleware_error', err, { method: connection.method, route, @@ -256,6 +257,11 @@ export class AlwatrNanoServer { ok: false, statusCode: 500, errorCode: 'http_server_middleware_error', + meta: { + name: err?.name, + message: err?.message, + cause: err?.cause, + }, }); } } diff --git a/core/signal/src/core.ts b/core/signal/src/core.ts index 61d5fabf5..5c436108e 100644 --- a/core/signal/src/core.ts +++ b/core/signal/src/core.ts @@ -68,14 +68,14 @@ function __callListeners(signal: SignalO const ret = listener.callback(signal.value); if (ret instanceof Promise) { ret.catch((err) => - logger.error('_callListeners', 'call_listener_failed', (err as Error).stack || err, { + logger.error('_callListeners', 'call_listener_failed', err, { signalName: signal.name, }), ); } } catch (err) { - logger.error('_callListeners', 'call_listener_failed', (err as Error).stack || err, { + logger.error('_callListeners', 'call_listener_failed', err, { signalName: signal.name, }); } @@ -129,7 +129,7 @@ export function _addSignalListener( listenerCallback(signal.value); } catch (err) { - logger.error('_addSignalListener', 'call_signal_callback_failed', (err as Error).stack || err, { + logger.error('_addSignalListener', 'call_signal_callback_failed', err, { signalName: signal.name, }); } diff --git a/core/storage-client/README.md b/core/storage-client/README.md index 9ba5fc584..91c831756 100644 --- a/core/storage-client/README.md +++ b/core/storage-client/README.md @@ -81,7 +81,7 @@ try { if ((err as Error)?.message === 'document_not_found') { console.log('user_5000 id not found!'); } else { - console.err((err as Error)?.message ?? err); + console.error(err); } } ``` diff --git a/core/storage-client/src/storage-client.ts b/core/storage-client/src/storage-client.ts index e54d64d48..102ef3032 100644 --- a/core/storage-client/src/storage-client.ts +++ b/core/storage-client/src/storage-client.ts @@ -61,7 +61,11 @@ alwatrRegisteredList.push({ * } */ export class AlwatrStorageClient { - protected _logger = createLogger('alwatr-storage-client:' + this.config.name, undefined, this.config.debug); + protected _logger = createLogger( + 'alwatr-storage-client' + (this.config.name == null ? '' : ':' + this.config.name), + undefined, + this.config.debug, + ); /** * Default fetch options. @@ -98,7 +102,7 @@ export class AlwatrStorageClient { - this._logger.logMethodArgs('get', {documentId}); + this._logger.logMethodArgs('get', {storage, documentId}); if (storage == null) throw new Error('storage_not_defined'); const responseJson = await serviceRequest({ @@ -118,10 +122,7 @@ export class AlwatrStorageClient { - this._logger.logMethodArgs('has', {documentId}); + this._logger.logMethodArgs('has', {storage, documentId}); if (storage == null) throw new Error('storage_not_defined'); const responseJson = await serviceRequest<{has: boolean | unknown}>({ @@ -212,7 +213,7 @@ export class AlwatrStorageClient { - this._logger.logMethodArgs('delete', {documentId}); + this._logger.logMethodArgs('delete', {storage, documentId}); if (storage == null) throw new Error('storage_not_defined'); await serviceRequest({ diff --git a/demo/logger/index.ts b/demo/logger/index.ts index cbacb8902..ea2aebe9b 100644 --- a/demo/logger/index.ts +++ b/demo/logger/index.ts @@ -34,5 +34,5 @@ try { throw new Error('my_error_message'); } catch (err) { - logger1.error('myMethod', 'error_code', (err as Error).stack || err, {a: 1, b: 2}); + logger1.error('myMethod', 'error_code', err, {a: 1, b: 2}); } diff --git a/demo/storage-client/benchmark.ts b/demo/storage-client/benchmark.ts index 9d8f9be16..18af3cd8f 100644 --- a/demo/storage-client/benchmark.ts +++ b/demo/storage-client/benchmark.ts @@ -12,10 +12,7 @@ interface User extends AlwatrDocumentObject { token: string; } -const token = process.env.TOKEN; -if (token == null) { - throw new Error('token_not_defined'); -} +const token = process.env.TOKEN ?? 'YOUR_SECRET_TOKEN'; const db = new AlwatrStorageClient({ name: 'junk-data', diff --git a/services/comment/src/lib/storage.ts b/services/comment/src/lib/storage.ts index fb3ae5180..a7fe7c4e9 100644 --- a/services/comment/src/lib/storage.ts +++ b/services/comment/src/lib/storage.ts @@ -3,4 +3,4 @@ import {AlwatrStorageClient} from '@alwatr/storage-client'; import {config} from '../config.js'; import {Message} from './type.js'; -export const storage = new AlwatrStorageClient(config.storage); +export const storageClient = new AlwatrStorageClient(config.storage); diff --git a/services/comment/src/route/patch.ts b/services/comment/src/route/patch.ts index 91377bc32..83fb7137c 100644 --- a/services/comment/src/route/patch.ts +++ b/services/comment/src/route/patch.ts @@ -1,6 +1,6 @@ import {config, logger} from '../config.js'; import {nanoServer} from '../lib/nano-server.js'; -import {storage} from '../lib/storage.js'; +import {storageClient} from '../lib/storage.js'; import {Message} from '../lib/type.js'; import type {AlwatrConnection} from '@alwatr/nano-server'; @@ -27,17 +27,26 @@ async function setComment(connection: AlwatrConnection): Promise { // } try { + const comment = await storageClient.set(bodyJson, params.storage); + logger.logProperty('comment', comment); + connection.reply({ ok: true, - data: await storage.set(bodyJson, params.storage), + data: comment, }); } - catch (err) { - logger.error('setComment', (err as Error).message ?? 'storage_error', (err as Error).stack ?? err); + catch (_err) { + const err = _err as Error; + logger.error('setComment', err.message || 'storage_error', err); connection.reply({ ok: false, statusCode: 500, errorCode: 'storage_error', + meta: { + name: err.name, + message: err.message, + cause: err.cause, + }, }); } } diff --git a/services/comment/src/route/storage.ts b/services/comment/src/route/storage.ts index f133a5542..709988f53 100644 --- a/services/comment/src/route/storage.ts +++ b/services/comment/src/route/storage.ts @@ -1,6 +1,6 @@ import {config, logger} from '../config.js'; import {nanoServer} from '../lib/nano-server.js'; -import {storage} from '../lib/storage.js'; +import {storageClient} from '../lib/storage.js'; import type {AlwatrConnection} from '@alwatr/nano-server'; @@ -16,14 +16,20 @@ async function getStorage(connection: AlwatrConnection): Promise { if (params == null) return; try { - connection.reply(await storage.getStorage(params.name)); + connection.reply(await storageClient.getStorage(params.name)); } - catch (err) { - logger.error('getStorage', (err as Error).message ?? 'storage_error', (err as Error).stack ?? err); + catch (_err) { + const err = _err as Error; + logger.error('getStorage', err.message || 'storage_error', err); connection.reply({ ok: false, statusCode: 500, errorCode: 'storage_error', + meta: { + name: err.name, + message: err.message, + cause: err.cause, + }, }); } } diff --git a/services/flight-crawler/src/crawl.ts b/services/flight-crawler/src/crawl.ts index e02f8a632..00de34750 100644 --- a/services/flight-crawler/src/crawl.ts +++ b/services/flight-crawler/src/crawl.ts @@ -25,7 +25,7 @@ export async function crawlAllJobs(): Promise { await storageClient.set(job); } catch (err) { - logger.error('crawlAllJobs', 's', (err as Error).stack); + logger.error('crawlAllJobs', 'crawling_failed', err); } } } diff --git a/services/storage-server/demo.http b/services/storage-server/demo.http index 579646354..c31e5ef6e 100644 --- a/services/storage-server/demo.http +++ b/services/storage-server/demo.http @@ -17,7 +17,7 @@ Content-Type: application/json { "id": "auto_increment", "from": "Ali Mihandoost", - "message": "Salam" + "message": "سلام" } ### @@ -28,7 +28,7 @@ Content-Type: application/json { "id": "auto_increment", "from": "Fateme Mihandoost", - "message": "Aleyk Salam" + "message": "علیک سلام" } ### Edit document diff --git a/services/telegram-notifier/src/bot/launch.ts b/services/telegram-notifier/src/bot/launch.ts index c12a57c61..666ce781f 100644 --- a/services/telegram-notifier/src/bot/launch.ts +++ b/services/telegram-notifier/src/bot/launch.ts @@ -9,12 +9,12 @@ export async function launchBot(): Promise { logger.logProperty('botInfo', bot.botInfo); bot.launch().catch((err) => { - logger.error('launchBot', 'launch_bot_failed', (err as Error).stack || err); + logger.error('launchBot', 'launch_bot_failed', err); }); await sendMessage(config.telegramBot.debugNotifyToken, '⚡️ Bot launched'); } catch (err) { - logger.error('launchBot', 'launch_bot_failed', (err as Error).stack || err); + logger.error('launchBot', 'launch_bot_failed', err); } } diff --git a/services/telegram-notifier/src/bot/send-message.ts b/services/telegram-notifier/src/bot/send-message.ts index fcdb8a0d0..e94a95fce 100644 --- a/services/telegram-notifier/src/bot/send-message.ts +++ b/services/telegram-notifier/src/bot/send-message.ts @@ -17,7 +17,7 @@ export async function sendMessage(to: string, message: string): Promise { } catch (err) { // TODO: handle blocked user - logger.error('sendMessage', 'error_send_message', (err as Error).stack || err, {chatId}); + logger.error('sendMessage', 'error_send_message', err, {chatId}); } } } diff --git a/ui-kit/icon/src/icon.ts b/ui-kit/icon/src/icon.ts index e118f1d23..118a2f8bf 100644 --- a/ui-kit/icon/src/icon.ts +++ b/ui-kit/icon/src/icon.ts @@ -84,8 +84,8 @@ export class AlwatrIcon extends AlwatrElement { try { this._icon = html`${unsafeSVG(await preloadIcon(this.name, this.urlPrefix))}`; } - catch (error) { - this._logger.error('_fetchIcon', 'fetch_failed', (error as Error).stack || error); + catch (err) { + this._logger.error('_fetchIcon', 'fetch_failed', err); this._icon = AlwatrIcon._fallback; } } diff --git a/uniquely/flight-finder-api/src/route/job/delete.ts b/uniquely/flight-finder-api/src/route/job/delete.ts index c909a5726..4e40ca3dc 100644 --- a/uniquely/flight-finder-api/src/route/job/delete.ts +++ b/uniquely/flight-finder-api/src/route/job/delete.ts @@ -22,19 +22,26 @@ async function deleteJob(connection: AlwatrConnection): Promise { data: {}, }); } - catch (err) { - if ((err as Error).message === 'document_not_found') { - return connection.reply({ + catch (_err) { + const err = _err as Error; + if (err.message === 'document_not_found') { + connection.reply({ ok: false, statusCode: 404, errorCode: 'document_not_found', }); } - // else - connection.reply({ - ok: false, - statusCode: 500, - errorCode: 'storage_error', - }); + else { + connection.reply({ + ok: false, + statusCode: 500, + errorCode: 'storage_error', + meta: { + name: err.name, + message: err.message, + cause: err.cause, + }, + }); + } } } diff --git a/uniquely/flight-finder-api/src/route/job/get.ts b/uniquely/flight-finder-api/src/route/job/get.ts index 2c7618de6..4de183c54 100644 --- a/uniquely/flight-finder-api/src/route/job/get.ts +++ b/uniquely/flight-finder-api/src/route/job/get.ts @@ -15,12 +15,18 @@ async function getJob(connection: AlwatrConnection): Promise { try { connection.reply(await storageClient.getStorage()); } - catch (err) { - logger.error('getJob', (err as Error).message ?? 'storage_error', (err as Error).stack ?? err); + catch (_err) { + const err = _err as Error; + logger.error('getJob', err.message || 'storage_error', err); connection.reply({ ok: false, statusCode: 500, errorCode: 'storage_error', + meta: { + name: err.name, + message: err.message, + cause: err.cause, + }, }); } } diff --git a/uniquely/flight-finder-api/src/route/job/put.ts b/uniquely/flight-finder-api/src/route/job/put.ts index fe6977f9b..82e965548 100644 --- a/uniquely/flight-finder-api/src/route/job/put.ts +++ b/uniquely/flight-finder-api/src/route/job/put.ts @@ -34,12 +34,18 @@ async function newJob(connection: AlwatrConnection): Promise { data: (await storageClient.set(job)) as unknown as Record, }); } - catch (err) { - logger.error('newJob', (err as Error).message ?? 'storage_error', (err as Error).stack ?? err); + catch (_err) { + const err = _err as Error; + logger.error('newJob', err.message || 'storage_error', err); connection.reply({ ok: false, statusCode: 500, errorCode: 'storage_error', + meta: { + name: err.name, + message: err.message, + cause: err.cause, + }, }); } }