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 *

    Laravel Backup Your Mysql Database With Delete Old Files and Notification Functionalities

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Laravel Backup Your Mysql Database With Delete Old Files and Notification Functionalities [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, folks am happy to share this with you as you already know how backing up your database is very important to avoid data loss, if you are looking for a way to backup your database using Laravel version 5 to 8, you are at the right place. At the end of this article I will provide the source code, GitHub repo and youtube video to make sure I put you through without facing any challenges, and if you face any don't forget to reach me via the comment box.How you will l...
    Continue Reading

    Mastering laravel Routes

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Mastering laravel Routes [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, folks, we have successfully mastered MVC in the last class, if you missed it go back here.We have talked about routes and where they can be found in the project, will do a quick freshened up in routes.Laravel routes simply accept a URI and a Closure, providing a very simple and expressive method of defining routes:Before we start using a route, take note of the following :Available Router Methodsget - you use get when you want to get resources from the databasep...
    Continue Reading

    How to Upgrade Laravel Application from Version 5 to latest Version 8

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to Upgrade Laravel Application from Version 5 to latest Version 8 [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, Folks, have you feels like Laravel continuous released of new versions has let you down and you can't cope with it any longer, don't worry this article is a quick guide for you, that has I upgrade naijacrawl from 5.8 to the latest 8 versions.All you need to know before upgrading your old laravel.External packages might fail to install, so isolate your packages firstInstall fresh laravel applicationThank me later.First, all you will do to be successful when upgra...
    Continue Reading

    Latest


    Download Naijacrawl App today

    Fastest way to read on the go

    Download the Naijacrawl App.