|
174 | 174 | * }; |
175 | 175 | * ``` |
176 | 176 | * |
| 177 | + * ### Handling Unexpected Errors |
| 178 | + * |
| 179 | + * Even with disciplined use of `trySync` and `tryAsync`, unexpected errors can |
| 180 | + * still occur due to programming mistakes, third-party library bugs, or edge |
| 181 | + * cases. These should be logged for debugging while maintaining application |
| 182 | + * stability. |
| 183 | + * |
| 184 | + * #### In Browser Environments |
| 185 | + * |
| 186 | + * ```ts |
| 187 | + * // Global error handler for unexpected errors |
| 188 | + * window.addEventListener("error", (event) => { |
| 189 | + * console.error("Uncaught error:", event.error); |
| 190 | + * // Send to error reporting service |
| 191 | + * errorReportingService.report(event.error); |
| 192 | + * }); |
| 193 | + * |
| 194 | + * // For unhandled promise rejections |
| 195 | + * window.addEventListener("unhandledrejection", (event) => { |
| 196 | + * console.error("Unhandled promise rejection:", event.reason); |
| 197 | + * errorReportingService.report(event.reason); |
| 198 | + * }); |
| 199 | + * ``` |
| 200 | + * |
| 201 | + * #### In Node.js Environments |
| 202 | + * |
| 203 | + * ```ts |
| 204 | + * // Handle uncaught exceptions (avoid process crash) |
| 205 | + * process.on("uncaughtException", (error) => { |
| 206 | + * console.error("Uncaught exception:", error); |
| 207 | + * errorReportingService.report(error); |
| 208 | + * // Gracefully shutdown if needed |
| 209 | + * process.exit(1); |
| 210 | + * }); |
| 211 | + * |
| 212 | + * // Handle unhandled promise rejections |
| 213 | + * process.on("unhandledRejection", (reason) => { |
| 214 | + * console.error("Unhandled promise rejection:", reason); |
| 215 | + * errorReportingService.report(reason); |
| 216 | + * }); |
| 217 | + * ``` |
| 218 | + * |
| 219 | + * These global handlers serve as a safety net for unexpected errors while |
| 220 | + * maintaining the discipline of explicit error handling through the `Result` |
| 221 | + * pattern. |
| 222 | + * |
177 | 223 | * ### FAQ |
178 | 224 | * |
179 | 225 | * #### What if my function doesn't return a value on success? |
|
183 | 229 | * than using `Result<true, E>` or `Result<null, E>` because it communicates |
184 | 230 | * that the function doesn't produce a value but can produce errors. |
185 | 231 | * |
| 232 | + * #### When can a function return `void` instead of `Result<void, E>`? |
| 233 | + * |
| 234 | + * A function can safely return `void` (instead of `Result<void, E>`) when all |
| 235 | + * unsafe code within it is properly wrapped with `trySync` or `tryAsync`. If |
| 236 | + * developers consistently wrap all potentially throwing operations, then any |
| 237 | + * function returning `void` is guaranteed not to throw and can be called |
| 238 | + * without error handling. |
| 239 | + * |
| 240 | + * ```ts |
| 241 | + * // ✅ Safe to return void - all unsafe code is wrapped |
| 242 | + * const processData = (data: string): void => { |
| 243 | + * const parseResult = trySync( |
| 244 | + * () => JSON.parse(data), |
| 245 | + * (error) => ({ type: "ParseError", message: String(error) }), |
| 246 | + * ); |
| 247 | + * |
| 248 | + * if (!parseResult.ok) { |
| 249 | + * logError(parseResult.error); // Handle error appropriately |
| 250 | + * return; |
| 251 | + * } |
| 252 | + * |
| 253 | + * // Continue with safe operations... |
| 254 | + * }; |
| 255 | + * |
| 256 | + * // ✅ Can call without try-catch since it returns void |
| 257 | + * processData(jsonString); |
| 258 | + * ``` |
| 259 | + * |
| 260 | + * This approach creates a clear contract: functions returning `void` are safe |
| 261 | + * to call, while functions returning `Result<T, E>` require explicit error |
| 262 | + * handling. |
| 263 | + * |
186 | 264 | * #### How do I short-circuit processing of an array on the first error? |
187 | 265 | * |
188 | 266 | * If you want to stop processing as soon as an error occurs (short-circuit), |
|
0 commit comments