Skip to content

Commit 00c1af9

Browse files
committed
feat: add rescue helper docs
1 parent 1197c58 commit 00c1af9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/docs/routing/error-handling.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ That's it! Leaf will no longer log errors or exceptions for your app.
130130

131131
:::
132132

133+
## Rescue Helper <Badge>New</Badge>
134+
135+
Leaf provides an elegant way to handle exceptions using the `rescue()` function. This function automically catches any exceptions thrown within the provided callback and logs them if logging is enabled, and then returns a default value. This way, you can use try-catch with a more inline syntax.
136+
137+
```php
138+
$someValue = rescue(function () {
139+
// Code that may throw an exception
140+
return someRiskyOperation();
141+
}, 'default value');
142+
```
143+
144+
In this example, if `someRiskyOperation()` throws an exception, the `rescue()` function will catch it, log it if logging is enabled, and return `'default value'` instead. While it may seem similar to using a try-catch block, `rescue()` provides a more concise and readable way to handle exceptions in your code, especially if you need to provide a default value:
145+
146+
```php
147+
$someRiskyOperation = function () {
148+
// Code that may throw an exception
149+
};
150+
151+
$someValue = rescue($someRiskyOperation, 'default value');
152+
```
153+
133154
## Leaf DevTools <Badge type="warning" text="BETA" />
134155

135156
Leaf provides DevTools to give you more insight into your app than you can get from the error page. It has a beautiful and intuitive interface that give you information about your Leaf application, and a light-weight library that you can use to interact with the devtools frontend.

0 commit comments

Comments
 (0)