feat: PAV-33: api error contract handler (code / message / details)#169
Merged
Conversation
Signed-off-by: Benevanio <benevaniosantos930@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Padroniza o contrato de erro da API no formato { code, message, details? } e ajusta backend e frontend para produzir/consumir esse envelope de forma consistente, centralizando validação e serialização de erros.
Changes:
- Backend: introduz
AppError,validatemiddleware e atualiza controllers/services +errorHandlerpara retornarem{ code, message, details? }. - Backend: atualiza rotas para validar payload/params com Zod e propagar erros via
next()(async controllers). - Frontend: adiciona
ApiError/parseApiErrore integra noapiClient(axios) eauthApi, com novos/ajustados testes.
Reviewed changes
Copilot reviewed 36 out of 36 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| frontend/tests/unit/utils/apiError.test.ts | Novos testes unitários para parsing do envelope {code/message/details} e formatos legados. |
| frontend/tests/unit/services/auth.service.test.tsx | Ajusta/expande testes para verificar ApiError e code vindo do envelope. |
| frontend/src/shared/lib/apiError.ts | Implementa ApiError e parseApiError para normalizar erros do backend. |
| frontend/src/shared/lib/apiClient.ts | Integra parseApiError no interceptor de resposta do axios. |
| frontend/src/domains/auth/infrastructure/authApi.ts | Substitui criação de Error genérico por parseApiError em respostas não-OK. |
| backend/tests/unit/modules/users/users.service.test.ts | Atualiza expectativas para erros tipados (ex.: NOT_FOUND). |
| backend/tests/unit/modules/users/user.controller.test.ts | Atualiza testes para controllers async lançarem AppError ao invés de escreverem diretamente no res. |
| backend/tests/unit/modules/savedJobs/savedJobs.service.test.ts | Atualiza expectativas para erros tipados (CONFLICT, NOT_FOUND). |
| backend/tests/unit/modules/savedJobs/savedJobs.controller.test.ts | Atualiza testes para controllers async lançarem AppError e propagarem erros inesperados. |
| backend/tests/unit/modules/auth/credentials.service.test.ts | Atualiza expectativas para erros tipados (CONFLICT, UNAUTHORIZED, NOT_FOUND). |
| backend/tests/unit/modules/auth/credentials.controller.test.ts | Ajusta testes para controllers propagarem AppError ao invés de mapear status manualmente. |
| backend/tests/unit/modules/auth/auth.controller.test.ts | Remove testes de validação local (agora delegada a middleware/rotas) e simplifica mocks. |
| backend/tests/unit/middleware/validate.test.ts | Novos testes para middleware validate (body/query/params e conversão de ZodError). |
| backend/tests/unit/middleware/requireAuth.test.ts | Novos testes para requireAuth retornando envelope padronizado. |
| backend/tests/unit/middleware/errorHandler.test.ts | Expande testes para serialização de AppError, conversão de ZodError e comportamento por ambiente. |
| backend/tests/unit/lib/errors.test.ts | Novos testes para factories do AppError e fromZodError. |
| backend/tests/unit/app.test.ts | Atualiza asserts para respostas de CORS no envelope {code,message}. |
| backend/tests/integration/routes/user.routes.test.ts | Ajusta teste de validação para verificar code/message/details. |
| backend/tests/integration/routes/savedJobs.routes.test.ts | Ajusta integração para envelope de erro e uso de AppError em mocks. |
| backend/tests/integration/routes/auth.routes.test.ts | Ajusta integração para envelope de erro em validações e falhas internas. |
| backend/src/routes/users.routes.ts | Adiciona validate e .catch(next) para controllers async; valida schemas do domínio. |
| backend/src/routes/savedJobs.routes.ts | Adiciona validate e .catch(next); valida schemas de saved jobs. |
| backend/src/routes/auth.routes.ts | Adiciona validate em params/body e .catch(next) para controllers async. |
| backend/src/modules/users/users.service.ts | Troca Error genérico por AppError.notFound(...). |
| backend/src/modules/users/users.controller.ts | Remove try/catch local e lança AppError (delegando serialização ao errorHandler). |
| backend/src/modules/users/schemas/user.schemas.ts | Expõe createPreferencesSchema alinhado ao update. |
| backend/src/modules/savedJobs/schemas/savedJobs.schemas.ts | Novo schema Zod para create/update de saved jobs. |
| backend/src/modules/savedJobs/savedJobs.service.ts | Troca Error genérico por AppError.conflict/notFound. |
| backend/src/modules/savedJobs/savedJobs.controller.ts | Remove validação/handler local e lança AppError (delegando ao errorHandler). |
| backend/src/modules/auth/credentials.service.ts | Migra para AppError e ajusta fluxo de register/login (com mudanças estruturais). |
| backend/src/modules/auth/credentials.controller.ts | Remove parsing/try-catch local e passa a propagar erros (tratados pelo errorHandler). |
| backend/src/modules/auth/auth.controller.ts | Remove validação local em getUrl (agora validada na rota) e tipa oauth_state. |
| backend/src/middleware/validate.ts | Novo middleware para validar body/query/params e converter ZodError em AppError. |
| backend/src/middleware/requireAuth.ts | Padroniza resposta 401 no envelope {code,message} via AppError. |
| backend/src/middleware/errorHandler.ts | Centraliza serialização do envelope e trata CORS/ZodError/AppError. |
| backend/src/lib/errors.ts | Implementa AppError + factories + fromZodError + toJSON. |
Comment on lines
+8
to
10
| import { AppError } from "../../lib/errors"; | ||
| import { generateUsername } from "../../utils/generateUsername"; | ||
| import type { Session } from "../types/auth.types"; |
Comment on lines
10
to
14
| @@ -7,8 +13,25 @@ export function errorHandler( | |||
| _next: NextFunction, | |||
| ): void { | |||
Comment on lines
1
to
5
| import { Router } from "express"; | ||
| import { validate } from "../middleware/validate"; | ||
| import { AuthController } from "../modules/auth/auth.controller"; | ||
| import { AuthService } from "../modules/auth/auth.service"; | ||
| import { CredentialsController } from "../modules/auth/credentials.controller"; |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Benevanio <benevaniosantos930@gmail.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.