-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create and use
InternalServerError
in /api/v1/status
- Loading branch information
1 parent
c00b737
commit b8a7dfd
Showing
2 changed files
with
56 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |