We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fce40d3 commit 86278b9Copy full SHA for 86278b9
src/functions.php
@@ -88,3 +88,22 @@ function make($service)
88
return \Leaf\Config::get("classes.$serviceName");
89
}
90
91
+
92
+if (!function_exists('rescue')) {
93
+ /**
94
+ * Run the given callback and return its result. If an exception occurs, report it and return the default value.
95
+ *
96
+ * @template T
97
+ * @param callable $callback
98
+ * @param mixed $default
99
+ * @return T|mixed
100
+ */
101
+ function rescue(callable $callback, $default = null)
102
+ {
103
+ try {
104
+ return $callback();
105
+ } catch (Throwable $e) {
106
+ return $default instanceof Closure ? $default($e) : $default;
107
+ }
108
109
+}
0 commit comments