Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
One of the most commonly overlooked parts of designing an API is proper error messaging. We all get excited to get our API out the door for its amazing functionality, and we forget to take care of informing callers of our API about errors that they might encounter. Because clients don’t have access to our error logs, we need to give them appropriate error messages.
Exception Constants
const COURSE_NOT_FOUND = 'Course Not Found';
const COURSE_INFO_REQUIRED = 'Required course data missing';
const COURSE_CREATION_FAILED = 'Unable to create course';
const COURSE_UPDATE_FAILED = 'Unable to update course';
const COURSE_DELETE_FAILED = 'Unable to delete course';
const REVIEW_NOT_FOUND = 'Review Not Found';
const REVIEW_INFO_REQUIRED = 'Required review data missing';
const REVIEW_CREATION_FAILED = 'Unable to create review';
const REVIEW_UPDATE_FAILED = 'Unable to update review';
const REVIEW_DELETE_FAILED = 'Unable to delete review';
Error Handler
To change how the handler, we have to add it to our dependencies as the "errorHandler"
$container['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
$data = [
'status' => 'error',
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
];
return $response->withJson($data,$statusCode);
};
};
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up