Loading
Current section: Error Handling 7 exercises
solution

Improving Error Messages for User

Transcript

00:00 Let's give users a much better error message here. So let's go to our username. We've got our invariant response that's throwing the error that's causing this. So we can come down here, and I want to show them a message that says, hey, you went to this username that doesn't exist. So let's have a default right here.

00:17 So this is our fallback if this was some other error. So I'm going to extract this to our error message, and we'll assign that to let. Because if the isErrorResponse is true from remix run react,

00:36 so isRouteErrorResponse error, and the error.status is 404, now we have enough information to say no user is found by that username. But we want to do even better and say params, use params.

00:53 And then we can say no user by username, and that will be params.username was found. And yeah, from there, that's it actually. No user by username not found was found. I suppose that makes sense.

01:12 And so yeah, whatever we put in here, that will show up right there. And yeah, that's actually a much better error message. And of course, this will still work if we have those throw new error right here. Now we're going to, because that isn't a 404 or anything like that, it's just like some error

01:32 we don't know what to do with, we're going to show them a generic error message. But because there's nothing they can do about it, there's not really anything useful they can report to us, so we'll just do something generic. But if we know what is really triggering the error, then yeah, sure, we can send some more error information.

01:50 Now once we know that this is a route error response, we actually can access a bunch of stuff off of this. So we can access the data, we know whether it's internal to a remix, we can get the status text, all of that stuff is accessible to us. But this is the way that I typically manage those types of errors.

02:09 And in the next step, we're actually going to use a utility that Kelly, the co-worker, is going to build for us. You can feel free to build a generic utility out of this if you would like. But yeah, Kelly is going to build that for us, and we're going to start using it all over the place. So it's going to be sweet. Have a good time.