🔥 Using Custom Exceptions to write better, clearer code - Laravel

Поделиться
HTML-код
  • Опубликовано: 16 ноя 2024

Комментарии • 26

  • @ward7576
    @ward7576 3 года назад +11

    I could recommend another approach that I learned from Spatie - you can just throw exceptions like so (if you have longer exception messages, but not only):
    `throw CreditsException::notEnoughCredits();`
    and the underlying implementation is just bunch of static methods doing this:
    `return new static('Not enough credits in your account');` where static in this case is the exception class.
    For me it's really neat since you can hide longer messages in the exception class and as well - define some other exceptions that can be related to credits (that way avoiding a ton of exception classes that throw one exception only)!
    I hope whoever sees this, deems it as useful, because that really helped me with more clarity and more readable exceptions since I like to be descriptive with my exception messages.
    Cheers!

    •  3 года назад

      Yep I really enjoy this approach as well! Thanks for commenting!

  • @Flamestriker42
    @Flamestriker42 4 года назад +3

    This is very helpful. Even if you know how to do custom exception this video still helps. How Laravel handles exception was helpful to me.

    •  4 года назад +1

      Happy to help!

  • @mayanksgajjar
    @mayanksgajjar Год назад

    Hey bro, Its a great content video. You have explained it in a very nice way.... Thanks and keep up doing great work...

    •  Год назад

      It's my pleasure

  • @jasonennis4905
    @jasonennis4905 4 года назад +1

    That's some really clean code fham not gonna lie 🇯🇲

    •  4 года назад

      Thanks, Jason!

  • @bryanastacio5167
    @bryanastacio5167 4 года назад +1

    Really like your content is too specific and helpful, keep going, pal!

  • @jongjinkim8587
    @jongjinkim8587 3 года назад +1

    I'm curious why do I have to handle the exception in the controller?
    Are there any drawbacks in common processing in the handlers provided by Laravel?
    If you handle exceptions in the controller, there seems to be too many duplicate codes. What do you think?

    • @mayanksgajjar
      @mayanksgajjar Год назад +1

      its just a personal choice, where to write code. I am not writing my business logic directly in controllers. I try to keep it simple. So I try to move my logic in service class, actions class, etc

  • @thevipinchand4690
    @thevipinchand4690 4 года назад

    Thanks a ton it's very helpful on daily development.

  • @loremipsum5642
    @loremipsum5642 4 года назад +1

    hi, I have a question. What if the custom exception will be placed in the handler?

    •  4 года назад +1

      I don't particularly like it because if I look at a controller I can't clearly see what happens, but yes, you can place it on the exception handler.

  • @bulent2435
    @bulent2435 3 года назад +1

    That was a good lesson. Thanks.

    •  3 года назад

      Very glad you liked it!

  • @logicalsyntaxerror
    @logicalsyntaxerror 4 года назад +2

    Wow, this is very helpful.

    •  4 года назад

      Thanks, man! Much appreciated. 😊

  • @murilolivorato1489
    @murilolivorato1489 2 года назад

    very good !

  • @gugamito
    @gugamito 4 года назад

    Tem alguma maneira de eu contar quantas exceptions foram geradas?

    •  4 года назад

      Fala, Gustavo. Pode ser mais específico?
      Acredito que $exception->getTrace() você consegue o stack trace.

  • @mubinsayed
    @mubinsayed 3 года назад

    Thanks

  • @romeosromo6286
    @romeosromo6286 4 года назад

    Really nice tutorial. However, It would've been really helpful if you showed us the files directories as well as the Ticket file. For new comers to laravel like me, it is not obvious in what directory some files go.

  • @hambaku7318
    @hambaku7318 4 года назад +1

    Your course's link is not working.

    •  4 года назад +2

      Whoops. My bad.
      Try now!