Repository navigation
fix(leads): return 401 from the search route instead of 500 - #88
Merged
Merged
Conversation
GET /api/leads/search was the only leads route without the apiHandler wrapper, so the UnauthorizedError thrown by withAuth escaped unhandled and the endpoint answered 500 for anonymous callers — including integrations that sent a revoked API key. It now goes through the shared handler and maps to 401 like every other leads route. The route lost its coverage when the original spec tested a re-implemented copy of the filtering logic instead of the handler, so the regression test exercises the real route: 401 without credentials, repository scoped by the API-key user id, externalId matching and leads without email.
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.
Qué
GET /api/leads/searchera la única ruta de leads sin el wrapperapiHandler. ElUnauthorizedErrorque lanzawithAuthse escapaba sin manejar y el endpoint respondía 500 a cualquier caller anónimo, incluido un integrador que mandaba una API key revocada.Ahora pasa por el handler compartido y responde 401 como el resto de las rutas de leads.
Por qué tenía 500 y no lo cazamos
El spec original (
search/__tests__/route.spec.ts) no testea el handler: reimplementa una copia del filtrado y testea esa copia. Nunca ejecutó el código real de la ruta, así que el manejo de errores quedó sin cobertura.Cambios
src/app/api/leads/search/route.ts:GETenvuelto enapiHandler.src/app/api/leads/search/__tests__/route-handler.spec.ts: test nuevo contra el handler real — 401 sin credenciales, repository scopeado por eluser_idde la API key, match porexternalIdy leads sin email.Verificación
jest— 111 suites / 816 teststsc --noEmitlimpiocurl https://crm.juancode.dev/api/leads/search?q=test→ 500Scope
Solo cambio de status code y cobertura. No toca el contrato de respuesta (mismos campos, mismo filtrado).
Risk: none.