-
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.
Merge pull request #31 from anthonyleier/custom-errors
Implementa `InternalServerError`
- Loading branch information
Showing
3 changed files
with
51 additions
and
22 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
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,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; |