-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨feat(main): implementa middleware customizado para log e tratamento …
…de erro | Parte 36 - Implementa Classes Específicas para Erros HTTP - Implementa e Configura o "Middleware" para "Log" de Erro - Implementa e Configura o "Middleware" para Responder Erros - Refatora o "Controller" Recuperar Categoria Por Id para Interceptar Erros e Usar Erros HTTP Apropriados - Refatorando o "Middleware" para Verificação de "Content-Types"para Usar Classe que Representa Erro HTTP Relacionado - Implementa e Configura o "Middleware" para Lidar com Rotas Inexistentes
- Loading branch information
1 parent
1452933
commit 8325c8c
Showing
8 changed files
with
95 additions
and
2 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
20 changes: 20 additions & 0 deletions
20
src/main/presentation/http/middlewares/error-logger.middleware.ts
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,20 @@ | ||
import { logger } from "@shared/helpers/logger.winston"; | ||
import { HttpError } from "@shared/presentation/http/http.error"; | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
const errorLoggerMiddleware = (error: HttpError, request: Request, response: Response, next: NextFunction) => { | ||
let statusCode = error.statusCode || 500; | ||
|
||
const logErro = JSON.stringify({ | ||
name: error.name, | ||
statusCode: statusCode, | ||
message: error.message, | ||
stack: process.env.NODE_ENV === 'development' ? error.stack : {} | ||
}, null, 2); | ||
|
||
logger.error(logErro); | ||
|
||
next(error); | ||
} | ||
|
||
export { errorLoggerMiddleware as errorLogger } |
14 changes: 14 additions & 0 deletions
14
src/main/presentation/http/middlewares/error-responser.middleware.ts
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,14 @@ | ||
import { HttpError } from "@shared/presentation/http/http.error"; | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
const errorResponderMiddleware = (error: HttpError, request: Request, response: Response, next: NextFunction) => { | ||
let statusCode = error.statusCode || 500; | ||
response.status(statusCode).json({ | ||
name: error.name, | ||
statusCode: statusCode, | ||
message: error.message, | ||
stack: process.env.NODE_ENV === 'development' ? error.stack : {} | ||
}); | ||
} | ||
|
||
export { errorResponderMiddleware as errorResponder } |
10 changes: 10 additions & 0 deletions
10
src/main/presentation/http/middlewares/invalid-path.middleware.ts
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,10 @@ | ||
import { HttpErrors } from "@shared/presentation/http/http.error"; | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
//Lança um erro 404 para caminhos indefinidos que vai ser tratado pelos middlewares de erros (log de erro e o responder de erro) | ||
const invalidPathMiddleware = (request: Request, response: Response, next: NextFunction) => { | ||
const error = new HttpErrors.NotFoundError(); | ||
next(error); | ||
} | ||
|
||
export { invalidPathMiddleware as invalidPath } |
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
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,36 @@ | ||
class HttpError extends Error { | ||
statusCode: number; | ||
|
||
constructor(statusCode:number, message: string = '⚠️ Erro HTTP genérico') { | ||
super(message); | ||
this.name = 'HttpError'; | ||
this.statusCode = statusCode; | ||
this.message = message; | ||
Object.setPrototypeOf(this, HttpError.prototype); | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
|
||
} | ||
|
||
class NotFoundError extends HttpError { | ||
constructor( params?: {statusCode?: number, message?: string}) { | ||
const { statusCode, message} = params || {}; | ||
super(statusCode || 404, message || '⚠️ Servidor Não Conseguiu Encontrar o Recurso Solicitado.'); | ||
this.name = 'NotFoundError'; | ||
} | ||
} | ||
|
||
class UnsupportedMediaTypeError extends HttpError { | ||
constructor( params?: {statusCode?: number, message?: string}) { | ||
const { statusCode, message} = params || {}; | ||
super(statusCode || 415, message || '⚠️ Servidor se Recusou a Aceitar a Requisição Porque o Formato do Payload Não é Um Formato Suportado.'); | ||
this.name = 'UnsupportedMediaTypeError'; | ||
} | ||
} | ||
|
||
const HttpErrors = { | ||
NotFoundError: NotFoundError, | ||
UnsupportedMediaTypeError: UnsupportedMediaTypeError | ||
} | ||
|
||
export { HttpError, HttpErrors } |