|
| 1 | +# Bugfix Instructions - Kit Module |
| 2 | + |
| 3 | +> **Last Updated**: February 2026 |
| 4 | +
|
| 5 | +--- |
| 6 | + |
| 7 | +## 🔍 Bug Investigation Process |
| 8 | + |
| 9 | +### Phase 1: Reproduce |
| 10 | + |
| 11 | +**Before writing any code:** |
| 12 | + |
| 13 | +1. **Understand the issue** - Read bug report carefully |
| 14 | +2. **Reproduce locally** - Create minimal test case |
| 15 | +3. **Verify it's a bug** - Not expected behavior or user error |
| 16 | +4. **Check documentation** - Is feature documented correctly? |
| 17 | + |
| 18 | +**Create failing test FIRST:** |
| 19 | + |
| 20 | +```typescript |
| 21 | +describe("Bug: Service returns null unexpectedly", () => { |
| 22 | + it("should return data when ID exists", async () => { |
| 23 | + mockRepository.findById.mockResolvedValue(mockData); |
| 24 | + |
| 25 | + // This SHOULD pass but currently FAILS |
| 26 | + const result = await service.findById("existing-id"); |
| 27 | + expect(result).toBeDefined(); |
| 28 | + }); |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +### Phase 2: Identify Root Cause |
| 33 | + |
| 34 | +**Investigation tools:** |
| 35 | + |
| 36 | +- **Logging** - Add temporary debug logs |
| 37 | +- **Debugger** - Use VS Code debugger |
| 38 | +- **Unit tests** - Isolate failing component |
| 39 | +- **Git blame** - Check when code was added |
| 40 | + |
| 41 | +```typescript |
| 42 | +// Add debug logging |
| 43 | +this.logger.debug(`Input: ${JSON.stringify(input)}`, "ServiceName"); |
| 44 | +this.logger.debug(`Result: ${JSON.stringify(result)}`, "ServiceName"); |
| 45 | +``` |
| 46 | + |
| 47 | +### Phase 3: Understand Impact |
| 48 | + |
| 49 | +**Critical questions:** |
| 50 | + |
| 51 | +- How many users affected? |
| 52 | +- Is this a security issue? (Priority: CRITICAL) |
| 53 | +- Is there a workaround? |
| 54 | +- Does this affect other features? |
| 55 | +- What version introduced this? |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 🐛 Common Bug Categories |
| 60 | + |
| 61 | +### 1. Database Issues |
| 62 | + |
| 63 | +| Bug Type | Symptoms | Solution | |
| 64 | +| ----------------------- | --------------------- | ----------------------------------------- | |
| 65 | +| **Query returns null** | Expected data missing | Check populate, fix query filter | |
| 66 | +| **Duplicate key error** | Cannot create record | Add validation, handle unique constraints | |
| 67 | +| **Population error** | Relations missing | Fix populate path, check ref | |
| 68 | + |
| 69 | +**Example fix:** |
| 70 | + |
| 71 | +```typescript |
| 72 | +// ❌ BUG - Missing populate |
| 73 | +async findUserWithRoles(id: string) { |
| 74 | + return this.userModel.findById(id); // roles = [ObjectId(...)] |
| 75 | +} |
| 76 | + |
| 77 | +// ✅ FIX - Populate relations |
| 78 | +async findUserWithRoles(id: string) { |
| 79 | + return this.userModel |
| 80 | + .findById(id) |
| 81 | + .populate('roles') // roles = [{ name: 'admin', ... }] |
| 82 | + .lean(); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### 2. Async/Promise Issues |
| 87 | + |
| 88 | +| Bug Type | Symptoms | Solution | |
| 89 | +| ----------------------- | --------------------- | --------------------- | |
| 90 | +| **Missing await** | Unexpected Promise | Add await keyword | |
| 91 | +| **Unhandled rejection** | Crash/silent failure | Add try-catch | |
| 92 | +| **Race condition** | Intermittent failures | Use proper async flow | |
| 93 | + |
| 94 | +**Example fix:** |
| 95 | + |
| 96 | +```typescript |
| 97 | +// ❌ BUG - Missing await |
| 98 | +async processItems(items: Item[]) { |
| 99 | + items.forEach(item => this.process(item)); // Fire and forget! |
| 100 | +} |
| 101 | + |
| 102 | +// ✅ FIX - Proper async handling |
| 103 | +async processItems(items: Item[]) { |
| 104 | + await Promise.all(items.map(item => this.process(item))); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### 3. Validation Errors |
| 109 | + |
| 110 | +| Bug Type | Symptoms | Solution | |
| 111 | +| ---------------------- | ---------------------- | -------------------- | |
| 112 | +| **Missing validation** | Invalid data accepted | Add DTO validation | |
| 113 | +| **Wrong type** | Type errors at runtime | Fix TypeScript types | |
| 114 | +| **Edge case** | Crashes on null/undef | Add null checks | |
| 115 | + |
| 116 | +**Example fix:** |
| 117 | + |
| 118 | +```typescript |
| 119 | +// ❌ BUG - No null check |
| 120 | +function getName(user: User): string { |
| 121 | + return user.profile.name; // Crashes if profile is null |
| 122 | +} |
| 123 | + |
| 124 | +// ✅ FIX - Defensive programming |
| 125 | +function getName(user: User | null): string { |
| 126 | + return user?.profile?.name ?? "Unknown"; |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### 4. Guard/Auth Issues |
| 131 | + |
| 132 | +| Bug Type | Symptoms | Solution | |
| 133 | +| ----------------------- | -------------------------- | -------------------- | |
| 134 | +| **Unauthorized access** | Wrong users can access | Fix guard logic | |
| 135 | +| **Token rejection** | Valid tokens rejected | Fix token validation | |
| 136 | +| **Role check fails** | Permission check incorrect | Fix role comparison | |
| 137 | + |
| 138 | +**Example fix:** |
| 139 | + |
| 140 | +```typescript |
| 141 | +// ❌ BUG - Comparing ObjectId to string |
| 142 | +if (user.roles.includes(requiredRoleId)) { |
| 143 | + // Always false |
| 144 | + return true; |
| 145 | +} |
| 146 | + |
| 147 | +// ✅ FIX - Convert to strings |
| 148 | +const roleIds = user.roles.map((r) => r.toString()); |
| 149 | +if (roleIds.includes(requiredRoleId)) { |
| 150 | + return true; |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### 5. Error Handling |
| 155 | + |
| 156 | +| Bug Type | Symptoms | Solution | |
| 157 | +| -------------------- | --------------------- | ---------------------------- | |
| 158 | +| **Swallowed errors** | Silent failures | Throw or log errors | |
| 159 | +| **Wrong error type** | Incorrect HTTP status | Use correct NestJS exception | |
| 160 | +| **Missing logs** | Can't debug issues | Add structured logging | |
| 161 | + |
| 162 | +**Example fix:** |
| 163 | + |
| 164 | +```typescript |
| 165 | +// ❌ BUG - Error swallowed |
| 166 | +async sendEmail(email: string) { |
| 167 | + try { |
| 168 | + await this.mail.send(email); |
| 169 | + } catch (error) { |
| 170 | + console.error(error); // ❌ Swallows error! |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// ✅ FIX - Proper error handling |
| 175 | +async sendEmail(email: string) { |
| 176 | + try { |
| 177 | + await this.mail.send(email); |
| 178 | + this.logger.log(`Email sent to ${email}`, 'MailService'); |
| 179 | + } catch (error) { |
| 180 | + this.logger.error( |
| 181 | + `Failed to send email: ${error.message}`, |
| 182 | + error.stack, |
| 183 | + 'MailService' |
| 184 | + ); |
| 185 | + throw new InternalServerErrorException('Email service unavailable'); |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## 🔧 Fix Implementation Process |
| 193 | + |
| 194 | +### 1. Write Failing Test |
| 195 | + |
| 196 | +```typescript |
| 197 | +// Test that currently fails |
| 198 | +it("should fix the bug", async () => { |
| 199 | + const result = await service.buggyMethod(); |
| 200 | + expect(result).toBe(expectedValue); |
| 201 | +}); |
| 202 | +``` |
| 203 | + |
| 204 | +### 2. Implement Fix |
| 205 | + |
| 206 | +```typescript |
| 207 | +// Fix the code |
| 208 | +async buggyMethod() { |
| 209 | + // New corrected implementation |
| 210 | + return correctValue; |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +### 3. Verify Test Passes |
| 215 | + |
| 216 | +```bash |
| 217 | +npm test -- buggy-service.spec.ts |
| 218 | +``` |
| 219 | + |
| 220 | +### 4. Test Edge Cases |
| 221 | + |
| 222 | +```typescript |
| 223 | +it("should handle edge case", async () => { |
| 224 | + const result = await service.buggyMethod(edgeCaseInput); |
| 225 | + expect(result).toBeDefined(); |
| 226 | +}); |
| 227 | +``` |
| 228 | + |
| 229 | +### 5. Update Documentation |
| 230 | + |
| 231 | +```typescript |
| 232 | +/** |
| 233 | + * Method that was buggy |
| 234 | + * |
| 235 | + * @fixed Version 1.2.3 - Fixed null pointer issue |
| 236 | + * @param input - The input parameter |
| 237 | + * @returns The expected result |
| 238 | + */ |
| 239 | +async buggyMethod(input: string): Promise<Result> |
| 240 | +``` |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | +## ⚠️ Common Gotchas |
| 245 | + |
| 246 | +### 1. Timezone Issues |
| 247 | + |
| 248 | +```typescript |
| 249 | +// ❌ Potential bug - Timezone-dependent |
| 250 | +const date = new Date(); |
| 251 | + |
| 252 | +// ✅ Better - Use UTC |
| 253 | +const date = new Date().toISOString(); |
| 254 | +``` |
| 255 | + |
| 256 | +### 2. Floating Point Comparison |
| 257 | + |
| 258 | +```typescript |
| 259 | +// ❌ Bug - Direct comparison |
| 260 | +if (price === 10.2) { |
| 261 | +} // Might fail due to precision |
| 262 | + |
| 263 | +// ✅ Fix - Use tolerance |
| 264 | +if (Math.abs(price - 10.2) < 0.01) { |
| 265 | +} |
| 266 | +``` |
| 267 | + |
| 268 | +### 3. MongoDB ObjectId Comparison |
| 269 | + |
| 270 | +```typescript |
| 271 | +// ❌ Bug - Comparing objects |
| 272 | +if (user._id === userId) { |
| 273 | +} // Always false |
| 274 | + |
| 275 | +// ✅ Fix - Convert to string |
| 276 | +if (user._id.toString() === userId) { |
| 277 | +} |
| 278 | +``` |
| 279 | + |
| 280 | +--- |
| 281 | + |
| 282 | +## 📋 Bugfix Checklist |
| 283 | + |
| 284 | +- [ ] Bug reproduced locally |
| 285 | +- [ ] Failing test created |
| 286 | +- [ ] Root cause identified |
| 287 | +- [ ] Fix implemented |
| 288 | +- [ ] All tests pass |
| 289 | +- [ ] Edge cases tested |
| 290 | +- [ ] Documentation updated |
| 291 | +- [ ] CHANGELOG updated |
| 292 | +- [ ] No regression (other features still work) |
0 commit comments