Skip to content

Commit 86278b9

Browse files
committed
feat: add rescue global
1 parent fce40d3 commit 86278b9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/functions.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,22 @@ function make($service)
8888
return \Leaf\Config::get("classes.$serviceName");
8989
}
9090
}
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

Comments
 (0)