1.0.13 • Published 2 years ago
@q5idservices/q5id-common v1.0.13
@q5idservices/q5id-common
Error Handler
Error Handler is taking care of handling all the errors. Below is the instruction on how to use the error handling.
Installation
npm install @q5idservices/q5id-common
Simple example
import { errorHandler, NotFoundError } from '@q5idservices/q5id-common';
// After all the routes and before the errorHandler
app.all('*', () => {
throw new NotFoundError();
});
// Place Error handler right before you start the server
app.use(errorHandler);
// Server Start
app.listen(port);
List of all the Error Handlers
Error Handlers | Function | Description |
---|---|---|
Bad Request Error | BadRequestError | Description coming... |
Custom Error | CustomError | Description coming... |
Database Connection Error | DatabaseConnectionError | Description coming... |
Not Found Error | NotFoundError | Description coming... |
Logger
The logger is logging everything separately to individual files or to the same file. It can be done both ways, log everything and separately. See example below.
Files will be created automatically. declaring
Logging Everything to ONE file
Import Logging and LoggingErrors to the index file. Its crucial to add Logging below you declare any routes and LoggingErrors right before you add a listener with the port. See example below.
List of all the Loggers
Log Handlers | Import | Usage | Description |
---|---|---|---|
Logging All | {LoggingAll} | LoggingAll(app) | his in used to log all the routes with message and status. It has to be injected inside an index file, before all routes are declared. |
Logging Errors | {LoggingErrors} | LoggingErrors(app) | This in used to catch all errors (throw new Error ). It has to be injected inside an index file, after all routes are declared. |
Custom Logging | {Log} | Log.Info("Message") Log.Warning("Message") Log.Error("Message") | This is made to log Error, Info, or Warning with custom message. |
- Logging - Catching everything besides Errors and logging it to logsAll.log
- LoggingErrors - Catching all the Errors. Specifically when it's
throw new Error(...)
. These Errors cannot be caught by Logging function.
// import express -> import express, { Express } from 'express';
import { LoggingAll, LoggingErrors } from '@q5idservices/q5id-common';
// initialize express -> const app: Express = express();
// This need to be declared before all the routes
LoggingAll(app);
// ... declaring all the routes, modules, controller, middleware
// ALL ROUTES HERER
// This need to be declared after all the routes
LoggingErrors(app);
// Server Start
app.listen(port);
Log Custom
Import logger to the file you want to use custom logger
Logging Info
import { Log } = require('@q5idservices/q5id-common');
// Example of use
// level - specify the level of the log (example: "warning", "info", "error", etc). Mainly "info" if it goest to Info log
// message - specify the log message
app.get('/', (req: Request, res: Response) => {
// Logging Custom info
Log.Info('Info message')
// Logging Custom Error
Log.Error('Error message')
// Logging Custom Warning
Log.Warning('Warning message')
res.send(...);
});
Changelog
Version 1.0.2
- Finalizing logger. Adding Error Logger and Logging All
Version 1.0.0 (breaking)
- Initial Publish of the package
- ErrorHandling
- Logger