setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $db; } catch (PDOException $ex) { # An error may occur here if the db connection is down (it happens), the connection variables # are incorrect for the machine this PHP file is being ran on, etc. Note that don"t usually want to # output specific information from the $ex variable, since the client should really know # about information in our database, so we make a generic message. handle_db_error("Can not connect to the database. Please try again later."); } } /** * Prints out a plain text 400 error message given $msg. Remember that 400 errors * indicate an invalid request from a client, but that should be separate from * any PDO-related (database) errors. Use handle_db_error for anything related * to the database. * @param $msg {string} - Plain text 400 message to output. */ function handle_request_error($msg) { process_error("HTTP/1.1 400 Invalid Request", $msg); } /** * Prints out a plain text 503 error message given $msg. If given a second (optional) argument as * an PDOException, prints details about the cause of the exception. See process_error for * note about responding with PDO errors to a client. * @param $msg {string} - Plain text 503 message to output * @param $ex {PDOException} - (optional) Exception object with additional exception details */ function handle_db_error($msg, $ex=NULL) { # we can do default parameters in PHP! NULL is default if not given a second parameter. process_error("HTTP/1.1 503 Service Unavailable", $msg, $ex); } /** * Terminates the program after printing out a JSON error message given $msg after * sending the given header error code (handy to factor out error-handling between * 400 request errors and 503 db errors). * If given an (optional) third argument as an Exception, prints details about * the cause of the exception. * * @param $type {string} - The HTTP error header string. * @param $msg {string} - Message to output. */ function process_error($type, $msg, $ex=NULL) { header($type); # e.g. "HTTP/1.1 400 Invalid Request" header("Content-type: application/json"); if ($ex) { # Note that in practice, you probably don"t want to print details about your # database errors in a response to a client. But for testing your code, this can # help pinpoint bugs in your PDO functions/database connections. echo ("Error details: $ex \n"); } die($msg); # die will print the argument and terminate the program. } ?>