Amrith's avatar

Need to Know about Laravel Exception Handling BEST PRACTICE

I m newbie for Laravel .

I' m planning to develop Laravel CRUD based web application using Laravel. its Member's management system.

I want to use SOLID principles in this application. So I DO NOT want to make complexity of controller Class.

Is that possible me to handle CRUD operation's exceptions without using try{} catch() block. if YES , then how . if NO , please suggest me good practice.

0 likes
13 replies
jlrdw's avatar

Yes you can let laravel handle errors, however see the error chapter, usually have a message or firendly page to redirect to. It will be in the chapter on errors, all of this.

1 like
Amrith's avatar

@jlrdw Thanks for your opinion. Yes i read that doc. are there any sample project to view error handling part. Some tutorials guide us to make try catch and handle errors. is that best way to do that ?

I want to learn BEST practice for error handling . could you explain that ? what is best way ? Do i need to use Custom Error handler to do that ? or controller try catch ?

jlrdw's avatar
jlrdw
Best Answer
Level 75

@Amrith I have written business apps and never needed to add a try catch, not including catching for javascript.

I may have something like this:

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

And in code have:

                    if (response.status === 422) {
                        response.json().then(function (data) {
                            var div = document.getElementById('msg');
                            for (var key in data) {
                                div.innerHTML += data[key];
                            }
                        })
                    }

Display the errors in a division. But laravel handles errors well and in non ajax you can return with errors.

I'm sure some custom code in a custom helper you may want to add a try catch, but regular CRUD I have never needed.

Edit:

Have you viewed the free laravel from scratch series by @jeffreyway?

1 like
Amrith's avatar

@jlrdw Can you share sample project to learn it ? i m very new bee for laravel.

jlrdw's avatar

@Amrith I suggest https://laracasts.com/series/laravel-8-from-scratch It's free training. And there is so much sample free code on Github already.

The videos will have Github links when it has code also.

Edit: Like https://github.com/JeffreyWay/Laravel-From-Scratch-Blog-Project//

Edit 2:

He has a try catch example:

    public function __invoke(Newsletter $newsletter)
    {
        request()->validate(['email' => 'required|email']);

        try {
            $newsletter->subscribe(request('email'));
        } catch (Exception $e) {
            throw ValidationException::withMessages([
                'email' => 'This email could not be added to our newsletter list.'
            ]);
        }

        return redirect('/')
            ->with('success', 'You are now signed up for our newsletter!');
    }

https://github.com/JeffreyWay/Laravel-From-Scratch-Blog-Project/blob/a90c6c97f09af4c69f21841d85305553599e4178/app/Http/Controllers/NewsletterController.php

1 like
Amrith's avatar

@jlrdw thanks sir. please go to YouTube and type "Ecommerce Restful API Laravel | Handle Exceptions #16"

he handle his errors without using try catch. So i m going to follow it. is that right or wrong. because i need to complete my interview project on today.please help me . other wise i lost my new job :)

martinbean's avatar

@Amrith We’re not here to help you pass job interviews. An interviewer is setting task to test your skills, not ours.

Amrith's avatar

@martinbean HEY ! ARE YOU THE OWNER OF LARACAST . STAY AWAY ! , DON'T TRY TO ADD GARBAGE .

Amrith's avatar

CC : @jeffreyway ,

@martinbean always try to hurt many people using his UNETHICAL,UNPROFESSIONAL way. i have evidence. He always show his violence in many places . please we need justice. hope you can understand this situation

jlrdw's avatar

If API then if it works it's your choice how to answer so if you want to, then answer like he does it.

Please or to participate in this conversation.