Closed LocalNewsTV closed 11 hours ago
This PR includes the following proposed change(s):
First pass
Compartmentalize Database functionality into class
Converted Email Settings/Templates to use new Error Handler and CommonDatabase class
Closes #3663
Examples
Having an Error occur in a response
function getEmailTemplates(): RequestHandler { return async (req, res) => { const response = await new CommonDatabase().query(getEmailTemplatesSQL()); throw new Error('Error Occurring in Function'); return res.status(200).json({ message: 'Email templates retrieved', result: response?.rows, namespace: req.url, req: req.body }); }; }
Error Handling Middleware kicks in
Throwing an Error in the CommonDatabase
public readonly query = async (sqlStatement: SQLStatement): Promise<any> => { let connection: PoolClient; try { connection = await this.getDBConnection(); if (this.logDetails) { this.log(); } throw new Error('Error Occurring in Function'); return await connection.query(sqlStatement.text, sqlStatement.values); } catch (e) { throw new CustomError(e?.message || 'Connection Refused', 503); } finally { connection?.release(); } }; }
The API Handles the error and continues to run, changing the status code to one we defined in CustomError
CustomError
Failed conditions 1 Security Hotspot 0.0% Coverage on New Code (required ≥ 80%)
See analysis details on SonarQube Cloud
Overview
This PR includes the following proposed change(s):
First pass
Compartmentalize Database functionality into class
Converted Email Settings/Templates to use new Error Handler and CommonDatabase class
Closes #3663
Examples
Having an Error occur in a response
Error Handling Middleware kicks in
Throwing an Error in the CommonDatabase
The API Handles the error and continues to run, changing the status code to one we defined in
CustomError