From b8a7dfd74bc19095eebd4e3c5802f7951990c684 Mon Sep 17 00:00:00 2001 From: braiancalot Date: Sat, 11 Jan 2025 09:54:20 -0300 Subject: [PATCH] feat: create and use `InternalServerError` in `/api/v1/status` --- infra/errors.js | 17 +++++++++ pages/api/v1/status/index.js | 68 +++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 infra/errors.js diff --git a/infra/errors.js b/infra/errors.js new file mode 100644 index 0000000..cdfe39e --- /dev/null +++ b/infra/errors.js @@ -0,0 +1,17 @@ +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 cc09c12..7b6eea9 100644 --- a/pages/api/v1/status/index.js +++ b/pages/api/v1/status/index.js @@ -1,36 +1,46 @@ import database from "infra/database.js"; +import { InternalServerError } from "infra/errors"; async function status(req, res) { - const updatedAt = new Date().toISOString(); - - 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 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; - - res.status(200).json({ - updated_at: updatedAt, - dependencies: { - database: { - version: databaseVersionValue, - max_connections: parseInt(databaseMaxConnectionsValue), - opened_connections: databaseOpenedConnectionsValue, + try { + const updatedAt = new Date().toISOString(); + + 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 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; + + res.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.error(publicErrorObject); + + res.status(500).json({ publicErrorObject }); + } } export default status;