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 9.18.0 New Features Detailed

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Laravel 9.18.0 New Features Detailed [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Laravel 9.18.0 comes with a new feature that you will love to upgrade and use, the new features come with 4 main features shipped.Rule objectsQuery timeAttachable objects Nested eager load syntaxRule objects rules that make it simpler to define custom validation rules using simple, single method invokable objects. Very similar API to Closure rules.Query timeintroduced a new method that invokes a closure if cumulative time you spend querying the database during a req...
    Continue Reading

    Laravel 9 Release Date and New Features

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Laravel 9 Release Date and New Features [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Laravel community has updated about the new release of another version of Laravel called laravel 9. With laravel 9 it brings new features that you will love and it will be released earlier next year in 2022 due to some dependencies of Symphony they depend on which will be available by November 2021 and Laravel normally gives 2 months after symphony updates to release a new version of laravel.Let's look at the new features Laravel introduced a new query builder which...
    Continue Reading

    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

    Latest


    Download Naijacrawl App today

    Fastest way to read on the go

    Download the Naijacrawl App.