Resilient Code, Predictable Errors
Learn to handle errors gracefully and build reliable systems with robust error-handling patterns.
1. Error Categories
Recoverable Errors
File not found, invalid input, API timeouts. Use try/catch
blocks or Result()
types.
Unrecoverable Errors
Memory access violations, deadlocks. Implemented as panic()
or abort()
.
2. Propagation Strategies
Manual Propagation
function loadFile(path: String): Result {
const file = try fs.open(path);
return Result.ok(file.read());
}
Automatic Propagation
async function fetchUser(id: number): Promise {
const response = await http.get(`/users/${id}`);
if (!response.ok) Error.throw(`User ${id} not found`);
return response.json();
}
3. Custom Error Objects
Add context to errors using custom types:
class AuthError extends SystemError {
constructor() {
super(401, 'Unauthorized access attempt');
this.suggestion = 'Provide valid API credentials';
}
}
<error type="AuthError">
{
"code": 401,
"message": "Unauthorized access attempt",
"suggestion": "Provide valid API credentials"
}
"code": 401,
"message": "Unauthorized access attempt",
"suggestion": "Provide valid API credentials"
}
4. Error Visualization
Error Propagation Graph
• → ✳️ → ❌
ERROR
Stack Trace
at handleRequest (server.js:142:5)
at receive (router.js:41:3)
at parse (parser.js:17:41)