Connect with us

How to show a nice message to the user when an error occurs in using Guzzle with Laravel

kizinho

Published

on

Follow
DEVELOPER PROGRAMMING: How to show a nice message to the user when an error occurs in using Guzzle with Laravel [New  Developer Programming] » Naijacrawl
Photo: Naijacrawl
site

Hi Folks, have you encountered an ugly Exception when making a post, get, put, delete to external API when an error like 500 or 404 occurs. Today you are at the right place, after reading this you will be able to show a nice message to your users in case of any of those errors occurs

When making a request to external API using guzzle to solve this problem you will need to catch the Exception by following the steps below.

  1. Prepare your request using Guzzle, I believe you know or have set it up already
  2. Make a post or get request to your external API
    public function create(Request $request) {
    $input = $request->all();
    try {
        $client = new Client();
        $headers = ['API-Key' => env('API_KEY')];
        $url = 'api/post/create';
        $response = $client->request('POST', $url, ['headers' => $headers, 'query' => $input]);
        $res = json_decode($response->getBody());
//here will catch error to aviod ugly message to users  
    } catch (\GuzzleHttp\Exception\RequestException $res) {
        if ($res->hasResponse()) {
            $response = $res->getResponse();
            if ($response->getStatusCode() == 500) {
                abort(500);
            } if ($response->getStatusCode() == 404) {
                abort(404);
            }
        }
    }
}


Congrats , you have saved your users from seeing that ugly message . Share and comment

Click Here To Comment


site


kizinho

Adike Kizito is a top-rated software developer, blogger, sports, and entertainment lover. He is the CEO of these popular websites Naijacrawl.com and Mp3ager.com. He operates his software developing task through Kizzsoft.com,... Read More

Continue Reading
Click to comment

Be first to comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Easy way to Host your Laravel Application on Shared Hosting

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Easy way to Host your Laravel Application on Shared Hosting [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi , Folks have you finished building that your awesome project but hosting it o shared hosting is what remains, don't worry you are on the right track, I have the best way to do that.StepsCreate Database on your online cpanelUpload your project or use git to do thatSet everything up on your ENVnow we need .htaccess that you will create inside your laravel root folderInside the .htaccess add this code RewriteEngine On #ends RewriteCond %{HTTP_HOST} ^(www.)?mydomain...
    Continue Reading

    How to Integrate Flutterwave Rave Payment System in your Laravel Applications

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to Integrate Flutterwave Rave Payment System in your Laravel Applications [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi Folks, I know many laravel developers that have been using Paystack as their payment system have been finding it difficult to integrate Paystack Laravel Package from Laravel 6.0 - 7.0 versions. Today I have a beautiful solution by using Flutterwave Rave Payment System, this process will work on any laravel versions.RequirementsRunning Exiting Project on LaravelCreate Account on FlutterwaveThen you are good to goLets Begincomposer require kingflamez/laravelrave On...
    Continue Reading

    Latest