Skip to content

Commit 58c163f

Browse files
committed
feat: update rescue helper docs
1 parent 00c1af9 commit 58c163f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/docs/routing/error-handling.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,23 @@ That's it! Leaf will no longer log errors or exceptions for your app.
135135
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.
136136

137137
```php
138-
$someValue = rescue(function () {
138+
$someRiskyOperation = function () {
139139
// Code that may throw an exception
140-
return someRiskyOperation();
141-
}, 'default value');
140+
};
141+
142+
$someValue = rescue($someRiskyOperation, 'default value');
142143
```
143144

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+
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. This is particularly useful for handling operations that may fail, such as database queries or API calls, without having to write verbose try-catch blocks, and is still useful even when you don't have to return a value.
145146

146147
```php
147-
$someRiskyOperation = function () {
148+
rescue(function() {
148149
// Code that may throw an exception
149-
};
150-
151-
$someValue = rescue($someRiskyOperation, 'default value');
150+
});
152151
```
153152

153+
In this case, if the callback throws an exception, it will be caught and logged if logging is enabled, and no visible error will be shown to the user. This is useful for important operations that you want to attempt, but don't want to disrupt the user experience if they fail. For example, sending an email notification or logging user activity.
154+
154155
## Leaf DevTools <Badge type="warning" text="BETA" />
155156

156157
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)