Part 18-Handle failure gracefully Create endpoint [How to use TDD to build a REST API in Laravel 6] @StudyStreamPlus
Part 18-Handle failure gracefully Create endpoint [How to use TDD to build a REST API in Laravel 6]  @StudyStreamPlus
Uploaded October 2019 | Updated December 2026, 32 minutes ago
We have some endpoints, but they aren’t the best. They don’t fail gracefully. Let’s take the Create endpoint for example. What happens if we provide a blank value for a required attribute?
The code will fail and throw an error but we don’t handle that error.

So let’s write a test in our StoreCest test file. When we have an invalid parameter in our request, the server must respond with a 400 error. So let’s write a test for that.

A good name for this test can be, “itWillReturn400WhenMissingRequiredParameter()”.

Our test should fail, because we are not handling any errors in the controller. Let’s change that.

Open your controller and navigate to the store method. Let’s use Laravel’s manual validator to make the Title field required. We know without this field, we cannot save a record to the database.

So what we need to do next is wrap most of this method in a try/catch block. So if there are any failures, we can handle it. The Laravel validator, will let us know if the validation has failed. If it has failed, we need to throw an exception. We can declare the message and the http status code.

Our catch block, will then return a response using the same http status code as well as the message. Now our test should pass. Run your tests. Great it passes. Good work.

Watch the next video in this series here:
youtu.be/iIrrrUCAM1w

Watch this course in full here:
youtube.com/playlist?list=PLrIm-p2rpV0F-rl4NPjNslEmW-D83AHOr

Laravel Tutorials:
youtube.com/playlist?list=PLrIm-p2rpV0FmbeNsYYh_4Fcb4Ibq29tJ

2 - 3 min Developer Quick Tips Series:
youtube.com/playlist?list=PLrIm-p2rpV0F6yyK99B9TU6yVhnPdzelBWatch this course in full here:
youtube.com/playlist?list=PLrIm-p2rpV0F-rl4NPjNslEmW-D83AHOr
Part 18-Handle failure gracefully Create endpoint [How to use TDD to build a REST API in Laravel 6]SOLID Principles - Liskov Substitution (PHP Tutorial Part 3/5)Part 27 - Install tymon/jwt-auth into Laravel 6 [How to use TDD to build a REST API in Laravel]Codeception Actors, Modules and Helpers - Tutorial [2/6]GCSE Computer Science Python #3 - Sequence and SelectionWeb Developer Interview Questions - How to optimise page load?Git Flow Part 3 - Learn Git Flow commands | How to use Git Flow TutorialGit Flow Part 2 - Using Git Flow in Source Tree TutorialThe Grep Command in Bash | How to search files and directories | Tutorial LinuxHow to change eye colour - Photoshop CC TutorialGCSE Computer Science Python #6 - Casting and String Manipulation
StudyStream+ |

Part 18-Handle failure gracefully Create endpoint [How to use TDD to build a REST API in Laravel 6]

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER