bcgov / invasivesbc

Inventory and database repository for the creation, treatment, and report on BC Invasive plants and animals.
Apache License 2.0
13 stars 4 forks source link

[INV-3663] Create Common Database Class #3704

Closed LocalNewsTV closed 11 hours ago

LocalNewsTV commented 1 day ago

Overview

This PR includes the following proposed change(s):

First pass

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 image

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 image

sonarcloud[bot] commented 1 day ago

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud