Skip to content

Commit 56cebd5

Browse files
committed
feat: create and use InternalServerError in /api/v1/status
1 parent a830b31 commit 56cebd5

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

infra/erros.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export class InternalServerError extends Error {
2+
constructor({ cause }) {
3+
super("Um erro interno não esperado aconteceu.", {
4+
cause,
5+
});
6+
this.name = "InternalServerError";
7+
this.action = "Entre em contato com o suporte.";
8+
this.statusCode = 500;
9+
}
10+
11+
toJSON() {
12+
return {
13+
name: this.name,
14+
message: this.message,
15+
action: this.action,
16+
status_code: this.statusCode,
17+
};
18+
}
19+
}

pages/api/v1/status/index.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
import database from "infra/database.js";
2+
import { InternalServerError } from "infra/erros.js";
23

34
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+
},
3033
},
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+
}
3345
}
3446

3547
export default status;

0 commit comments

Comments
 (0)