Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AhmedHelalAhmed/6b23a1138e41e77036b46dc40ab61383 to your computer and use it in GitHub Desktop.
Save AhmedHelalAhmed/6b23a1138e41e77036b46dc40ab61383 to your computer and use it in GitHub Desktop.
exceptions in laravel
try {
$user = User::findOrFail($request->input('user_id'));
} catch (ModelNotFoundException $exception) {
return back()->withError($exception->getMessage())->withInput();
}
$user = User::find($user_id);
if (!$user) {
throw new ModelNotFoundException('User not found by ID ' . $user_id);
}
try {
$user = $this->userService->search($request->input('user_id'));
} catch (ModelNotFoundException $exception) {
return back()->withError($exception->getMessage())->withInput();
}
https://laraveldaily.com/how-to-catch-handle-create-laravel-exceptions/
try {
// Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
// Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
// Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}
public function search(Request $request)
{
try {
$user = $this->userService->search($request->input('user_id'));
} catch (UserNotFoundException $exception) {
report($exception);
return back()->withError($exception->getMessage())->withInput();
}
return view('users.search', compact('user'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment