Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We can throw exceptions to let our users know when things don't work quite right.
Throwing Exceptions
First we need to tell our Course class to use \App\Exception\ApiException;
. Then we can throw exceptions to let our users know what's going on.
We should throw and exception in the following situations:
- With getCourses
- if there are no courses
- With getCourse
- if we can't find a course
- With createCourse
- check that the proper data has been passed
- check if the course has actually been added
- With updateCourse
- check if the proper data has been passed
- check if the course has actually been added
- check if the course exists before trying to add the course. Instead of writing this again here, we can use the getCourse method
- With deleteCourse
- check if the course exists first. Again we can use getCourse
- check that the database has been affected by our statement
We can close these two files,
and we'll open our course.
0:01
The first thing we need to do is tell our
course to use our new exception, use App,
0:06
\Exception\ApiException.
0:17
So for our first method, getCourses,
if there are no courses,
0:30
we want to throw an exception.
0:34
We'll set this to courses.
0:41
And then we'll return courses.
0:45
Then we'll check if empty courses.
0:50
If so,
we want to throw a new ApiException.
0:57
And then we can use our ApiException with
1:02
COURSE_NOT_FOUND, and 404.
1:07
Let's do the same thing for the getCourse.
1:12
Course =, and return course.
1:21
If not, we'll throw an exception.
1:29
Before we move on,
let's test this out in Postman.
1:32
Line 21.
1:46
Great, we see course not found.
2:04
Let's go back and
add some more exceptions.
2:08
Our next method is createCourse.
2:13
First, we should check that
the proper data has been passed.
2:16
If empty data title.
2:24
Or empty data url.
2:31
Then we're going to throw
a new ApiException.
2:37
And we're gonna call ApiException.
2:42
And we're going to say COURSE,
2:45
_INFO_REQUIRED.
2:52
Also, we want to check if
the course has actually been added.
2:55
After the execute, we can check, If
3:00
the statement affected rowCount,
3:05
and that is less than 1.
3:13
So if nothing changed,
we want to throw a new ApiException,
3:18
and then we can say ApiException,
3:24
COURSE_CREATION_FAILED.
3:31
Next is updateCourse.
3:37
We're going to do the same
thing that we did for create.
3:39
So let's copy these lines here and
paste them here.
3:42
We also need to check if
empty data course_id.
3:48
And again, we'll need the rowCount,
helps if we spell it right.
3:59
So after execute,
we'll check if anything was updated.
4:06
If not, we'll throw another exception,
UPDATE_FAILED.
4:10
Finally, we have our deleteCourse.
4:18
First, we want to check that
our course actually exists.
4:20
So again, we can use this getCourse,
and pass the course_id.
4:24
Once again, we can check that a row
has been affected by our statement.
4:32
So after execute, we'll do our rowCount.
4:36
And if not, COURSE_DELETE_FAILED.
4:41
Let's check this out in Postman.
4:47
So trying to find a course that doesn't
exist gives us the course not found.
4:50
If we want to add a course,
but we don't pass any data,
5:02
It's giving us an error,
COURSE_INFO_REQUIRED.
5:11
Missing an R.
5:15
Course, course, course.
5:18
Okay, course.
5:23
All the rest look good.
5:26
Required course data missing.
5:31
Great, and
if we try to update a course, Without
5:35
any information,
Required course data missing.
5:40
And if we try to delete a course that
doesn't exist, Course Not Found.
5:51
This is just an example.
5:59
You can go ahead and
play around a little bit more.
6:00
Your next challenge is to add
exceptions in your review class.
6:03
Go ahead and give it a try and
I'll show you what I did in the next step.
6:08
You need to sign up for Treehouse in order to download course files.
Sign up