From the course: Building React and ASP.NET Core Applications

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Handling errors in React

Handling errors in React

From the course: Building React and ASP.NET Core Applications

Start my 1-month free trial

Handling errors in React

- [Instructor] The way we have built our app so far assumes that our code is perfect and it will always work but any developer can tell you that is never the case. So, let us learn how to handle errors in React. For that, let's go to Visual Code and see the action. In here, we are going to start from the API, so let's go to the Controller. So, I'll choose Controller and in here, I'll just assume that something will go wrong with the GetTrips. Now, to handle that case in here, we need to put all the code inside the API endpoint GetTrips inside a try-catch block. So, I'll just write in here try and then we write in here catch and then Exception to catch any type of exception, and add ex. And then we want to put all the logic inside the try block. And in the catch, you're now going to return a BadRequest in this case. And in the catch block, I'll just return a BadRequest. So I'll just write in here, something went wrong,…

Contents