{"id":1366,"date":"2018-01-19T19:22:45","date_gmt":"2018-01-19T19:22:45","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/a-primer-on-postgres-exception-handling-for-the-php-developer\/"},"modified":"2018-01-19T19:24:44","modified_gmt":"2018-01-19T19:24:44","slug":"a-primer-on-postgres-exception-handling-for-the-php-developer","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/a-primer-on-postgres-exception-handling-for-the-php-developer\/","title":{"rendered":"A Primer On Postgres Exception Handling for the PHP Developer"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Robert Bernier<\/div>\n<div class=\"\">on November 6, 2007<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">\nA long time ago in a place far far away, at least from my house, object oriented programming was invented. Included in its paradigm is EXCEPTION HANDLING which guides how one should think and process undesirable\/unexpected events\/inputs. The idea being that any code processing an undesired event should be easy to implement and understood by others.\n    <\/div>\n<div class=\"articlePara\">\nThe objective of this article is to show how you can leverage Postgres exception handling as the backend of your web-enabled application. For the purposes of this article, it&#8217;s understood that PHP 5 on Apache 2 and Postgres 8.2 are used.\n    <\/div>\n<h2>About Exception Handling<\/h2>\n<div class=\"articlePara\">\nFor those who may not be familiar with exceptions; failed function calls normally return an error condition that eventually propagates back to the invoking process, i.e. the web server, which must provide a response to that failure. The beauty of exception handling is that it can intercept the error condition and provide alternate instructions in an elegant manner without the client&#8217;s web browser getting that ugly error message that includes a cryptic error code and requesting that they tell somebody real quick.\n    <\/div>\n<h2>About Postgres Exception Handling<\/h2>\n<div class=\"articlePara\">\nPostgres exceptions are quite different from PHP exceptions; whereas PHP 5 uses the traditional &#8220;Try And Catch&#8221; format, Postgres exceptions can only be defined, executed and processed inside the body of the function that triggered the exception.\n    <\/div>\n<div class=\"articlePara\">\nNotice below how an additional EXCEPTION block is added to the standard plpgsql function:\n    <\/div>\n<h2\/>\n<div class=\"example\">\n<code><\/p>\n<pre>\n[ &lt;&lt;label&gt;&gt; ]\n[ DECLARE\n    declarations ]\nBEGIN\n    statements\nEXCEPTION\n    WHEN condition [ OR condition ... ] THEN\n        handler_statements\n    [ WHEN condition [ OR condition ... ] THEN\n          handler_statements\n      ... ]\nEND;\n<\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nAll statements between the words EXCEPTION and END make up the exception instruction block. The &#8216;condition&#8217; is one of many predefined text string constants. The hander_statements consist of user-defined plpgsql code that is invoked when an error condition occurs. The EXCEPTION block will not be executed until an error condition occurs in the &#8216;statements&#8217; portion of the function.\n    <\/div>\n<div class=\"articlePara\">\nTwo environment variables become available during the execution of an exception i.e. SQLSTATE and SQLERRM. SQLSTATE is an SQL compliant unique 5 character code identifying the raised exception; the first two digits corresponds to the error&#8217;s class while the three remaining characters define the specific error within that class. The SQLERRM variable, which is used as a test condition, returns a constant string detailing the error message. Refer to Appendix A, Table A-1, PostgreSQL Error Codes, for the entire list of supported SQLSTATE and SQLERRM.\n    <\/div>\n<h2>About Raising Messages<\/h2>\n<div class=\"articlePara\">\nRaising messages not only triggers exceptions but, during the execution of an exception, they are an excellent way to record and report details about the error condition itself. Here&#8217;s the general form of the RAISE statement where the &#8216;expression&#8217; is a variable expressed as a percentage symbol in the string, &#8216;format&#8217;:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\nRAISE level 'format' [, expression [, ...]];\n\nRAISE NOTICE 'this is my message: %', msg;\n<\/pre>\n<p><\/code>\n       <\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"robert_bernier200711024658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"robert_bernier200711024658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A long time ago in a place far far away, at least from my house, object oriented programming was invented. Included<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1366","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1366"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1366\/revisions"}],"predecessor-version":[{"id":3241,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1366\/revisions\/3241"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}