Hands-On Lab & Phase Exit
Error matrix, correlation exercise, engineering challenges, oral exam, and completion
checklist
Phase 13 - Professional Error Handling
Volume 04 - Professional Backend
Architecture milestone
implement
↓
trigger errors
↓
correlate logs
↓
verify contract
↓
explain architecture
Phase 13 creates one professional exception boundary and a correlation story that spans client response and server logs.
Hands-on lab
The exit lab proves that TaskFlow has one coherent error boundary instead of a collection of framework-default
responses.
Lab A - Add the error contract
• Create ApiErrorResponse.
• Add TaskNotFoundException and ProjectNotFoundException.
• Update TasksService to throw domain-specific exceptions where context is known.
• Keep unknown exceptions re-thrown.
Lab B - Add global filter and request logging
• Create ApiExceptionFilter using HttpAdapterHost.
• Create RequestLoggingInterceptor using Fastify request.id.
• Register both globally with APP_FILTER and APP_INTERCEPTOR.
• Verify successful responses expose x-request-id.
• Verify 4xx/5xx error bodies contain requestId.
Lab C - Error matrix
Scenario Expected
GET /tasks/not-a-uuid 400 BAD_REQUEST, requestId
GET valid missing Task UUID 404 TASK_NOT_FOUND, requestId
POST missing/invalid body 400 BAD_REQUEST + details
POST valid body, missing Project 404 PROJECT_NOT_FOUND
Known unique conflict (where available) 409 UNIQUE_CONSTRAINT_CONFLICT
P2025 escaping contextual service handling 404 RESOURCE_NOT_FOUND fallback
Deliberate unexpected Error in dev test route 500 INTERNAL_ERROR, no stack in response
Lab D - Correlation exercise
• Trigger one failing request.
• Copy requestId from the JSON response.
• Find the matching NestJS log entry.
• Confirm the client response is sanitized while the log has diagnostic context.
• Remove any temporary route used to create an artificial 500.
Exercise levels
Level Challenge
1 - Follow Implement the reference filter/interceptor and pass the smoke-test matrix.
Level Challenge
2 - Modify Add a stable PROJECT_NOT_FOUND client handling branch in the Next.js API helper demo.
3 - Implement Add a Task title uniqueness rule inside one Project and map its P2002 conflict safely.
4 - Engineer Design an error-code naming convention for a future HRIS with payroll, leave, recruitment, and employee
domains; explain which codes are public contracts and which diagnostics remain internal.
Architecture oral exam
• Why should a global error filter not contain Task business rules?
• Why is requestId useful if the client never sees a stack trace?
• When should a service catch a Prisma error instead of relying on the global filter?
• Why is P2002 usually 409 but an unavailable database usually 500?
• What is the difference between a network failure and an API error response?
• Which data should never be written to logs even during debugging?
Phase exit checklist
• all error responses have statusCode, code, message, requestId, timestamp, and path.
• validation details remain useful without exposing internals.
• Task/Project not-found failures have domain codes.
• known Prisma conflicts are translated safely.
• unknown errors return sanitized 500 responses.
• requestId correlates client response and server logs.
• successful responses expose x-request-id.
• NestJS production build passes.
• you can explain which layer owns classification, translation, formatting, and logging.
Figure 1 - Phase 13 complete architecture
Request / Fastify request.id
↓
Validation + Controller + Service
↓
Known domain or infrastructure exception
↓
ApiExceptionFilter
↓
Stable ApiErrorResponse + requestId
↓
Structured server log
TaskFlow now has a professional error boundary that remains useful as authentication, authorization, and business rules become
more complex.
Next phase
Phase 14 will deepen Service & Business Logic Architecture: application invariants, cross-model rules, workflow
methods, transactions where needed, orchestration vs persistence, and keeping controllers thin without turning
services into giant “god classes.”