Debugging Guide
This guide explains how to debug issues in the Plus261 project. Effective debugging requires understanding the tools and techniques available.
What You’ll Learn
Section intitulée « What You’ll Learn »This guide will help you understand:
- ✅ Available debugging tools and techniques
- ✅ How to debug API and Web components
- ✅ How to debug GraphQL queries and mutations
- ✅ How to debug database issues
- ✅ How to debug authentication and authorization
- ✅ Common issues and solutions
Overview
Section intitulée « Overview »Effective debugging requires understanding the tools and techniques available. This guide covers debugging for both API and Web components.
Debugging Tools
Section intitulée « Debugging Tools »VS Code Debugger
Section intitulée « VS Code Debugger »VS Code provides excellent debugging support for TypeScript.
- Create
.vscode/launch.json:
{ "version": "0.2.0", "configurations": [ { "name": "Debug API", "type": "node", "request": "launch", "runtimeExecutable": "bun", "runtimeArgs": ["--inspect", "dev"], "cwd": "${workspaceFolder}/apps/api", "console": "integratedTerminal" }, { "name": "Debug Tests", "type": "node", "request": "launch", "runtimeExecutable": "bun", "runtimeArgs": ["--inspect", "test"], "cwd": "${workspaceFolder}", "console": "integratedTerminal" } ]}- Set breakpoints in your code
- Press F5 to start debugging
Browser DevTools
Section intitulée « Browser DevTools »For debugging the web client:
| Tool | Purpose |
|---|---|
| Console | JavaScript debugging |
| Network | API requests inspection |
| React DevTools | Component inspection |
| Performance | Performance profiling |
GraphQL Playground
Section intitulée « GraphQL Playground »The GraphQL Playground is excellent for debugging API issues:
- ✅ Test queries and mutations
- ✅ Inspect errors
- ✅ View request/response
- ✅ Explore schema
Access: http://localhost:4000/graphql
Common Debugging Scenarios
Section intitulée « Common Debugging Scenarios »1. API Not Responding
Section intitulée « 1. API Not Responding »Symptoms:
- Requests timeout
- 500 errors
- No response
Debugging Steps:
# 1. Check if API is runningcurl http://localhost:4000/graphql
# 2. Check logstail -f apps/api/logs/app.log
# 3. Check database connectionkubectl -n plus261-app get pods
# 4. Check environment variablescat apps/api/.env2. GraphQL Errors
Section intitulée « 2. GraphQL Errors »Symptoms:
- GraphQL errors in response
- Field resolution errors
- Type errors
Debugging Steps:
- Check Error Details in GraphQL Playground:
query { products { id name }}- Check Server Logs:
# View API logstail -f apps/api/logs/app.log- Add Logging:
@Query(() => [Product])async products(@Ctx() context: Context) { console.log('Query products', { userId: context.userId }); try { return await this.service.query({ context, query: {} }); } catch (error) { console.error('Error querying products', error); throw error; }}3. Database Issues
Section intitulée « 3. Database Issues »Symptoms:
- Database connection errors
- Query failures
- Data not persisting
Debugging Steps:
- Check Database Connection:
kubectl -n plus261-app get podskubectl -n plus261-app logs statefulset/postgres- Use Drizzle Studio:
bunx drizzle-kit studio- Check Migrations:
# List migrationsbunx drizzle-kit migrate
# Check migration statusbunx drizzle-kit check- Test Query Directly:
import { db } from '../database';const result = await db.select().from(users);console.log('Users:', result);4. Authentication Issues
Section intitulée « 4. Authentication Issues »Symptoms:
- Unauthorized errors
- Session expired
- Permission denied
Debugging Steps:
- Check Session:
// In API codeconsole.log('Session:', context.session);console.log('User:', context.user);console.log('Permissions:', context.permissions);- Test Authentication:
mutation { signIn(email: "test@example.com", password: "password") { token user { id email } }}5. Performance Issues
Section intitulée « 5. Performance Issues »Symptoms:
- Slow queries
- High memory usage
- Timeouts
Debugging Steps:
- Enable Query Logging:
// Log slow queriesconst start = Date.now();const result = await db.query(...);const duration = Date.now() - start;if (duration > 1000) { console.warn('Slow query:', { duration, query });}- Use Performance Monitoring:
// Check metricsimport { getMetrics } from '../metrics/prometheus';const metrics = getMetrics();console.log('Metrics:', metrics);Debugging Techniques
Section intitulée « Debugging Techniques »1. Logging
Section intitulée « 1. Logging »Add strategic logging:
// Use appropriate log levelslogger.debug('Detailed debug info', { data });logger.info('Informational message', { context });logger.warn('Warning message', { issue });logger.error('Error occurred', { error, context });2. Breakpoints
Section intitulée « 2. Breakpoints »Set breakpoints in VS Code:
- Click in the gutter next to line number
- Run debugger (F5)
- Inspect variables
- Step through code (F10, F11)
3. Console Logging
Section intitulée « 3. Console Logging »For quick debugging:
console.log('Variable value:', variable);console.table(arrayData);console.dir(object, { depth: null });4. Error Inspection
Section intitulée « 4. Error Inspection »Inspect errors thoroughly:
try { // Code} catch (error) { console.error('Error details:', { message: error.message, stack: error.stack, name: error.name, cause: error.cause, }); throw error;}Debugging API Issues
Section intitulée « Debugging API Issues »GraphQL Query Debugging
Section intitulée « GraphQL Query Debugging »- Use GraphQL Playground:
query { products(filter: { status: { eq: "ACTIVE" } }) { id name }}- Check Resolve Info:
@Query(() => [Product])async products( @Ctx() context: Context, @GraphQLResultInfo() resolveInfo: GraphQLResolveInfoResult) { console.log('Requested fields:', resolveInfo.fields); // ...}Database Query Debugging
Section intitulée « Database Query Debugging »- Enable Query Logging:
// In database configconst db = drizzle( postgres(connectionString, { logger: true, // Log all queries }),);- Use Drizzle Studio:
bunx drizzle-kit studioDebugging Web Client Issues
Section intitulée « Debugging Web Client Issues »Component Debugging
Section intitulée « Component Debugging »- Use Svelte DevTools:
<script> import { onMount } from 'svelte';
onMount(() => { console.log('Component mounted'); debugger; // Set breakpoint });</script>- Inspect State:
<script> let state = $state({ count: 0 });
$effect(() => { console.log('State changed:', state); });</script>Network Debugging
Section intitulée « Network Debugging »- Check Network Tab in DevTools
- Inspect Requests:
// Log all requestsfetch('/graphql', { method: 'POST', body: JSON.stringify({ query }),}) .then((response) => { console.log('Response:', response); return response.json(); }) .then((data) => { console.log('Data:', data); });Common Issues and Solutions
Section intitulée « Common Issues and Solutions »| Issue | Solution |
|---|---|
| Cannot find module | rm -rf node_modules && bun install |
| Type errors | bun --bun run check:code (root) or cd apps/web && bun --bun run check |
| Port already in use | lsof -i :4000 && kill -9 <PID> |
| Database connection failed | kubectl -n plus261-app get pods,svc and check Postgres readiness |
Best Practices
Section intitulée « Best Practices »- Add Logging Early - Don’t wait until you need to debug
- Use Appropriate Log Levels - debug, info, warn, error
- Include Context - User ID, request ID, etc.
- Test Locally First - Reproduce issues locally
- Use Debugger - Don’t just use console.log
- Document Issues - Keep notes on common problems
Quick Reference
Section intitulée « Quick Reference »| Tool | Purpose | Command/URL |
|---|---|---|
| VS Code Debugger | Step-through debugging | F5 |
| GraphQL Playground | Test queries | http://localhost:4000/graphql |
| Drizzle Studio | Database inspection | bunx drizzle-kit studio |
| Browser DevTools | Web debugging | F12 |
| Logs | View application logs | tail -f apps/api/logs/app.log |
Next Steps
Section intitulée « Next Steps »Continue learning:
- 🧪 Testing Guide - Testing strategies
- 🛠️ Development Workflow - Development process
- 📊 Observability - Monitoring and logging
Related Topics
Section intitulée « Related Topics »- Testing Guide - Testing strategies
- Development Workflow - Development process
- Observability - Monitoring and logging
- Code Style - Coding conventions