@piggly/fastify-chassis
Version:
An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.
67 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationErrorEvent = void 0;
const event_bus_1 = require("@piggly/event-bus");
const ddd_toolkit_1 = require("@piggly/ddd-toolkit");
/**
* @file This event should be published when an error occurs.
* @since 7.0.0
* @copyright Piggly Lab 2025
* @author Caique Araujo <caique@piggly.com.br>
*/
class ApplicationErrorEvent extends event_bus_1.EventPayload {
/**
* Create a new instance of the event.
*
* @param {Error} error The error.
* @param {string} hash The hash.
* @memberof ApplicationErrorEvent
* @since 7.0.0
* @author Caique Araujo <caique@piggly.com.br>
*/
constructor(error, hash) {
super('APPLICATION_ERROR_EVENT', {
error: error.message,
hash,
raw: error,
});
}
/**
* Publish the event.
*
* This event should be completed on a graceful shutdown or interruption.
* But use carrefully, as it will heavily increase memory usage if you have a lot of
* events to wait.
*
* If you don't use the EventBus.interface.cleanup you
* may change flag removing "wait_onclean" to false when
* publishing this event.
*
* It will be logged as error when log is true. with parameters:
* - event: APPLICATION_ERROR_EVENT
* - hash: hash
* - message: error.message
* - stack: error.stack
*
* If you enable FileLogStreamService on LoggerService, the message will be:
* [date-time] [uuid] (error) "APPLICATION_ERROR_EVENT" - "hash" - "error.message" - "error.stack"
*
* @param {Error} error The error.
* @param {string} hash The hash.
* @param {EventOptions} options The options.
* @public
* @static
* @memberof ApplicationErrorEvent
* @since 7.0.0
* @author Caique Araujo <caique@piggly.com.br>
*/
static publish(error, hash, options = { log: true, wait_onclean: true }) {
const event = new ApplicationErrorEvent(error, hash);
if (options.log) {
ddd_toolkit_1.LoggerService.softResolve().error(`APPLICATION_ERROR_EVENT`, hash, error.message, error.stack);
}
event_bus_1.EventBus.instance.send(event, options.wait_onclean);
}
}
exports.ApplicationErrorEvent = ApplicationErrorEvent;
//# sourceMappingURL=ApplicationErrorEvent.js.map