Skip to content

Commit

Permalink
Merge pull request #31 from anthonyleier/custom-errors
Browse files Browse the repository at this point in the history
Implementa `InternalServerError`
  • Loading branch information
anthonyleier authored Feb 17, 2025
2 parents 57d0f18 + 1db2c8c commit a2f43be
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion infra/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function query(queryObject) {
console.error(error);
throw error;
} finally {
await client.end();
await client?.end();
}
}

Expand Down
17 changes: 17 additions & 0 deletions infra/errors.js
Original file line number Diff line number Diff line change
@@ -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,
};
}
}
54 changes: 33 additions & 21 deletions pages/api/v1/status/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
import database from "infra/database.js";

import { InternalServerError } from "infra/errors";
async function status(request, response) {
const updatedAt = new Date().toISOString();
try {
const updatedAt = new Date().toISOString();

const postgresVersionResult = await database.query("SHOW server_version;");
const postgresVersion = postgresVersionResult.rows[0].server_version;
const postgresVersionResult = await database.query("SHOW server_version;");
const postgresVersion = postgresVersionResult.rows[0].server_version;

const maxConnectionsResult = await database.query("SHOW max_connections;");
const maxConnections = parseInt(maxConnectionsResult.rows[0].max_connections);
const maxConnectionsResult = await database.query("SHOW max_connections;");
const maxConnections = parseInt(
maxConnectionsResult.rows[0].max_connections,
);

const databaseName = process.env.POSTGRES_DB;
const openedConnectionsResult = await database.query({
text: `SELECT COUNT(*)::INTEGER AS opened_connections FROM pg_stat_activity WHERE datname = $1;`,
values: [databaseName],
});
const openedConnections = openedConnectionsResult.rows[0].opened_connections;
const databaseName = process.env.POSTGRES_DB;
const openedConnectionsResult = await database.query({
text: `SELECT COUNT(*)::INTEGER AS opened_connections FROM pg_stat_activity WHERE datname = $1;`,
values: [databaseName],
});
const openedConnections =
openedConnectionsResult.rows[0].opened_connections;

response.status(200).json({
updated_at: updatedAt,
dependencies: {
database: {
version: postgresVersion,
max_connections: maxConnections,
opened_connections: openedConnections,
response.status(200).json({
updated_at: updatedAt,
dependencies: {
database: {
version: postgresVersion,
max_connections: maxConnections,
opened_connections: openedConnections,
},
},
},
});
});
} catch (error) {
const publicErrorObject = new InternalServerError({ cause: error });

console.log("\n Erro dentro do catch do controller:");
console.error(publicErrorObject);

response.status(500).json(publicErrorObject);
}
}

export default status;

0 comments on commit a2f43be

Please sign in to comment.