diff --git a/infra/database.js b/infra/database.js index 9235499..58f999a 100644 --- a/infra/database.js +++ b/infra/database.js @@ -8,10 +8,11 @@ async function query(queryObject) { const result = await client.query(queryObject); return result; } catch (error) { + console.log("\n Erro dentro do catch do database:"); console.error(error); throw error; } finally { - await client.end(); + await client?.end(); } } diff --git a/infra/errors.js b/infra/errors.js new file mode 100644 index 0000000..25a69eb --- /dev/null +++ b/infra/errors.js @@ -0,0 +1,19 @@ +export class InternalServerError extends Error { + constructor({ cause }) { + super("Um erro interno não esperado aconteceu", { + cause, + }); + this.name = "InternalServerError"; + this.action = "Entre em contato com o suporte."; + this.statusCode = 500; + } + + toJSON() { + return { + name: this.name, + message: this.message, + action: this.action, + status_code: this.statusCode, + }; + } +} diff --git a/pages/api/v1/status/index.js b/pages/api/v1/status/index.js index c581f9f..4793887 100644 --- a/pages/api/v1/status/index.js +++ b/pages/api/v1/status/index.js @@ -1,35 +1,44 @@ import database from "infra/database"; - +import { InternalServerError } from "infra/errors"; async function status(request, response) { - const updatedAt = new Date().toISOString(); + try { + const updatedAt = new Date().toISOString(); - const databaseVersionResult = await database.query("SHOW server_version;"); - const databaseVersionValue = databaseVersionResult.rows[0].server_version; + const databaseVersionResult = await database.query("SHOW server_version;"); + const databaseVersionValue = databaseVersionResult.rows[0].server_version; - const databaseMaxConnectionsResult = await database.query( - "SHOW max_connections;", - ); - const databaseMaxConnectionsValue = - databaseMaxConnectionsResult.rows[0].max_connections; + const databaseMaxConnectionsResult = await database.query( + "SHOW max_connections;", + ); + const databaseMaxConnectionsValue = + databaseMaxConnectionsResult.rows[0].max_connections; - const databaseName = process.env.POSTGRES_DB; - const databaseOpenedConnectionsResult = await database.query({ - text: "SELECT count(*)::int FROM pg_stat_activity WHERE datname = $1;", - values: [databaseName], - }); - const databaseOpenedConnectionsValue = - databaseOpenedConnectionsResult.rows[0].count; + const databaseName = process.env.POSTGRES_DB; + const databaseOpenedConnectionsResult = await database.query({ + text: "SELECT count(*)::int FROM pg_stat_activity WHERE datname = $1;", + values: [databaseName], + }); + const databaseOpenedConnectionsValue = + databaseOpenedConnectionsResult.rows[0].count; - response.status(200).json({ - updated_at: updatedAt, - dependencies: { - database: { - version: databaseVersionValue, - max_connections: parseInt(databaseMaxConnectionsValue), - opened_connections: databaseOpenedConnectionsValue, + response.status(200).json({ + updated_at: updatedAt, + dependencies: { + database: { + version: databaseVersionValue, + max_connections: parseInt(databaseMaxConnectionsValue), + opened_connections: databaseOpenedConnectionsValue, + }, }, - }, - }); + }); + } catch (error) { + const publicErrorObject = new InternalServerError({ + cause: error, + }); + console.log("\n Erro dentro do catch do controller:"); + console.log(publicErrorObject); + response.status(500).json(publicErrorObject); + } } export default status;