-
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 #32 from anthonyleier/custom-errors
Padronização dos Controllers
- Loading branch information
Showing
8 changed files
with
188 additions
and
84 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,24 @@ | ||
import { InternalServerError, MethodNotAllowedError } from "infra/errors"; | ||
|
||
function onNoMatchHandler(request, response) { | ||
const publicErrorObject = new MethodNotAllowedError(); | ||
response.status(publicErrorObject.statusCode).json(publicErrorObject); | ||
} | ||
|
||
function onErrorHandler(error, request, response) { | ||
const publicErrorObject = new InternalServerError({ | ||
statusCode: error.statusCode, | ||
cause: error, | ||
}); | ||
console.error(publicErrorObject); | ||
response.status(publicErrorObject.statusCode).json(publicErrorObject); | ||
} | ||
|
||
const controller = { | ||
errorHandlers: { | ||
onNoMatch: onNoMatchHandler, | ||
onError: onErrorHandler, | ||
}, | ||
}; | ||
|
||
export default controller; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,52 +1,44 @@ | ||
import { resolve } from "node:path"; | ||
import migrationRunner from "node-pg-migrate"; | ||
import database from "infra/database.js"; | ||
import { createRouter } from "next-connect"; | ||
import controller from "infra/controller.js"; | ||
|
||
const router = createRouter(); | ||
router.get(getHandler); | ||
router.post(postHandler); | ||
export default router.handler(controller.errorHandlers); | ||
|
||
const defaultMigrationsOptions = { | ||
dryRun: true, | ||
dir: resolve("infra", "migrations"), | ||
direction: "up", | ||
verbose: true, | ||
migrationsTable: "pgmigrations", | ||
}; | ||
|
||
async function getHandler(request, response) { | ||
const dbClient = await database.getNewClient(); | ||
const pendingMigrations = await migrationRunner({ | ||
...defaultMigrationsOptions, | ||
dbClient, | ||
}); | ||
await dbClient.end(); | ||
return response.status(200).json(pendingMigrations); | ||
} | ||
|
||
export default async function migrations(request, response) { | ||
let dbClient; | ||
const allowedMethods = ["GET", "POST"]; | ||
|
||
if (!allowedMethods.includes(request.method)) { | ||
return response.status(405).json({ | ||
error: `Method "${request.method}" not allowed`, | ||
}); | ||
async function postHandler(request, response) { | ||
const dbClient = await database.getNewClient(); | ||
const migratedMigrations = await migrationRunner({ | ||
...defaultMigrationsOptions, | ||
dbClient, | ||
dryRun: false, | ||
}); | ||
await dbClient.end(); | ||
|
||
if (migratedMigrations.length > 0) { | ||
return response.status(201).json(migratedMigrations); | ||
} | ||
|
||
try { | ||
dbClient = await database.getNewClient(); | ||
|
||
const defaultMigrationsOptions = { | ||
dbClient: dbClient, | ||
dryRun: true, | ||
dir: resolve("infra", "migrations"), | ||
direction: "up", | ||
verbose: true, | ||
migrationsTable: "pgmigrations", | ||
}; | ||
|
||
if (request.method === "GET") { | ||
const pendingMigrations = await migrationRunner(defaultMigrationsOptions); | ||
return response.status(200).json(pendingMigrations); | ||
} | ||
|
||
if (request.method === "POST") { | ||
const migratedMigrations = await migrationRunner({ | ||
...defaultMigrationsOptions, | ||
dryRun: false, | ||
}); | ||
|
||
if (migratedMigrations.length > 0) { | ||
return response.status(201).json(migratedMigrations); | ||
} | ||
|
||
return response.status(200).json(migratedMigrations); | ||
} | ||
|
||
return response.status(405).end(); | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} finally { | ||
await dbClient.end(); | ||
} | ||
return response.status(200).json(migratedMigrations); | ||
} |
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,43 +1,35 @@ | ||
import { createRouter } from "next-connect"; | ||
import database from "infra/database.js"; | ||
import { InternalServerError } from "infra/errors"; | ||
async function status(request, response) { | ||
try { | ||
const updatedAt = new Date().toISOString(); | ||
import controller from "infra/controller"; | ||
|
||
const postgresVersionResult = await database.query("SHOW server_version;"); | ||
const postgresVersion = postgresVersionResult.rows[0].server_version; | ||
const router = createRouter(); | ||
router.get(getHandler); | ||
export default router.handler(controller.errorHandlers); | ||
|
||
const maxConnectionsResult = await database.query("SHOW max_connections;"); | ||
const maxConnections = parseInt( | ||
maxConnectionsResult.rows[0].max_connections, | ||
); | ||
async function getHandler(request, response) { | ||
const updatedAt = new Date().toISOString(); | ||
|
||
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 postgresVersionResult = await database.query("SHOW server_version;"); | ||
const postgresVersion = postgresVersionResult.rows[0].server_version; | ||
|
||
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 }); | ||
const maxConnectionsResult = await database.query("SHOW max_connections;"); | ||
const maxConnections = parseInt(maxConnectionsResult.rows[0].max_connections); | ||
|
||
console.log("\n Erro dentro do catch do controller:"); | ||
console.error(publicErrorObject); | ||
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(500).json(publicErrorObject); | ||
} | ||
response.status(200).json({ | ||
updated_at: updatedAt, | ||
dependencies: { | ||
database: { | ||
version: postgresVersion, | ||
max_connections: maxConnections, | ||
opened_connections: openedConnections, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export default status; |
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,25 @@ | ||
import orchestrator from "tests/orchestrator.js"; | ||
|
||
beforeAll(async () => { | ||
await orchestrator.waitForAllServices(); | ||
}); | ||
|
||
describe("POST /api/v1/status", () => { | ||
describe("Anonymous user", () => { | ||
test("Retrieving current system status", async () => { | ||
const response = await fetch("http://localhost:3000/api/v1/status", { | ||
method: "POST", | ||
}); | ||
expect(response.status).toBe(405); | ||
|
||
const responseBody = await response.json(); | ||
expect(responseBody).toEqual({ | ||
name: "MethodNotAllowedError", | ||
message: "Método não permitido para este endpoint.", | ||
action: | ||
"Verifique se o método HTTP enviado é válido para este endpoint", | ||
status_code: 405, | ||
}); | ||
}); | ||
}); | ||
}); |