|
1 | 1 | import database from "infra/database.js";
|
| 2 | +import { InternalServerError } from "infra/erros.js"; |
2 | 3 |
|
3 | 4 | async function status(request, response) {
|
4 |
| - const updatedAt = new Date().toISOString(); |
5 |
| - |
6 |
| - const databaseVersionResult = await database.query("SHOW server_version;"); |
7 |
| - const databaseVersionValue = databaseVersionResult.rows[0].server_version; |
8 |
| - |
9 |
| - const databaseMaxConnectionResult = await database.query( |
10 |
| - "SHOW max_connections;", |
11 |
| - ); |
12 |
| - const databaseMaxConnectionValue = |
13 |
| - databaseMaxConnectionResult.rows[0].max_connections; |
14 |
| - |
15 |
| - const databaseName = process.env.POSTGRES_DB; |
16 |
| - const databaseOpenedConnectionsResult = await database.query({ |
17 |
| - text: "SELECT count(*)::int FROM pg_stat_activity where datname = $1;", |
18 |
| - values: [databaseName], |
19 |
| - }); |
20 |
| - const databaseOpenedConnectionsValue = |
21 |
| - databaseOpenedConnectionsResult.rows[0].count; |
22 |
| - |
23 |
| - response.status(200).json({ |
24 |
| - updated_at: updatedAt, |
25 |
| - dependencies: { |
26 |
| - database: { |
27 |
| - version: databaseVersionValue, |
28 |
| - max_connections: parseInt(databaseMaxConnectionValue), |
29 |
| - opened_connections: databaseOpenedConnectionsValue, |
| 5 | + try { |
| 6 | + const updatedAt = new Date().toISOString(); |
| 7 | + |
| 8 | + const databaseVersionResult = await database.query("SHOW server_version;"); |
| 9 | + const databaseVersionValue = databaseVersionResult.rows[0].server_version; |
| 10 | + |
| 11 | + const databaseMaxConnectionResult = await database.query( |
| 12 | + "SHOW max_connections;", |
| 13 | + ); |
| 14 | + const databaseMaxConnectionValue = |
| 15 | + databaseMaxConnectionResult.rows[0].max_connections; |
| 16 | + |
| 17 | + const databaseName = process.env.POSTGRES_DB; |
| 18 | + const databaseOpenedConnectionsResult = await database.query({ |
| 19 | + text: "SELECT count(*)::int FROM pg_stat_activity where datname = $1;", |
| 20 | + values: [databaseName], |
| 21 | + }); |
| 22 | + const databaseOpenedConnectionsValue = |
| 23 | + databaseOpenedConnectionsResult.rows[0].count; |
| 24 | + |
| 25 | + response.status(200).json({ |
| 26 | + updated_at: updatedAt, |
| 27 | + dependencies: { |
| 28 | + database: { |
| 29 | + version: databaseVersionValue, |
| 30 | + max_connections: parseInt(databaseMaxConnectionValue), |
| 31 | + opened_connections: databaseOpenedConnectionsValue, |
| 32 | + }, |
30 | 33 | },
|
31 |
| - }, |
32 |
| - }); |
| 34 | + }); |
| 35 | + } catch (error) { |
| 36 | + const publicErrorObject = new InternalServerError({ |
| 37 | + cause: error, |
| 38 | + }); |
| 39 | + |
| 40 | + console.log("\n Erro dentro do catch do controller: "); |
| 41 | + console.error(publicErrorObject); |
| 42 | + |
| 43 | + response.status(500).json(publicErrorObject); |
| 44 | + } |
33 | 45 | }
|
34 | 46 |
|
35 | 47 | export default status;
|
0 commit comments