]> git.openstreetmap.org Git - nominatim.git/commitdiff
introduce chksql function for website code and move other fail functions
authorSarah Hoffmann <lonvia@denofr.de>
Sun, 12 Jun 2016 12:34:57 +0000 (14:34 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Sun, 12 Jun 2016 12:34:57 +0000 (14:34 +0200)
The default error screen now reports the full SQL even when not
in debug mode as this message is often more helpful than our
custom error message.

lib/db.php
lib/lib.php
lib/website.php
website/deletable.php
website/details.php
website/hierarchy.php
website/polygons.php
website/reverse.php
website/search.php

index cc84bf9bdaf71df7261aa393721e25c567771336..822acc37d4997f64bca66bc7ea506dcb3ab0f9e3 100644 (file)
@@ -4,13 +4,8 @@
        function &getDB($bNew = false, $bPersistent = false)
        {
                // Get the database object
-               $oDB = DB::connect(CONST_Database_DSN.($bNew?'?new_link=true':''), $bPersistent);
-               if (PEAR::IsError($oDB))
-               {
-                       var_dump(CONST_Database_DSN);
-                       var_Dump($oDB);
-                       fail($oDB->getMessage());
-               }
+               $oDB = chksql(DB::connect(CONST_Database_DSN.($bNew?'?new_link=true':''), $bPersistent),
+                             "Failed to establish database connection");
                $oDB->setFetchMode(DB_FETCHMODE_ASSOC);
                $oDB->query("SET DateStyle TO 'sql,european'");
                $oDB->query("SET client_encoding TO 'utf-8'");
index fc3936851bbb56bd031495761e1fe6f781b703d9..9f017fc4ea83adf48fa79b38044782b0d680cce0 100644 (file)
@@ -1,44 +1,5 @@
 <?php
 
-       function failInternalError($sError, $sSQL = false, $vDumpVar = false)
-       {
-               header('HTTP/1.0 500 Internal Server Error');
-               header('Content-type: text/html; charset=utf-8');
-               echo "<html><body><h1>Internal Server Error</h1>";
-               echo '<p>Nominatim has encountered an internal error while processing your request. This is most likely because of a bug in the software.</p>';
-               echo "<p><b>Details:</b> ".$sError,"</p>";
-               echo '<p>Feel free to report the bug in the <a href="http://trac.openstreetmap.org">OSM bug database</a>. Please include the error message above and the URL you used.</p>';
-               if (CONST_Debug)
-               {
-                       echo "<hr><h2>Debugging Information</h2><br>";
-                       if ($sSQL)
-                       {
-                               echo "<h3>SQL query</h3><code>".$sSQL."</code>";
-                       }
-                       if ($vDumpVar)
-                       {
-                               echo "<h3>Result</h3> <code>";
-                               var_dump($vDumpVar);
-                               echo "</code>";
-                       }
-               }
-               echo "\n</body></html>\n";
-               exit;
-       }
-
-
-       function userError($sError)
-       {
-               header('HTTP/1.0 400 Bad Request');
-               header('Content-type: text/html; charset=utf-8');
-               echo "<html><body><h1>Bad Request</h1>";
-               echo '<p>Nominatim has encountered an error with your request.</p>';
-               echo "<p><b>Details:</b> ".$sError,"</p>";
-               echo '<p>If you feel this error is incorrect feel free to report the bug in the <a href="http://trac.openstreetmap.org">OSM bug database</a>. Please include the error message above and the URL you used.</p>';
-               echo "\n</body></html>\n";
-               exit;
-       }
-
        function fail($sError, $sUserError = false)
        {
                if (!$sUserError) $sUserError = $sError;
index cf228f3823cccf9159a01ac7023bfd74840637d3..22ec08f10ed999bc6c069d402b5fb1bb0ee776d2 100644 (file)
@@ -1,5 +1,88 @@
 <?php
 
+/***************************************************************************
+ *
+ * Error handling functions
+ *
+ */
+       function chksql($oSql, $sMsg = "Database request failed")
+       {
+               if (!PEAR::isError($oSql)) return $oSql;
+
+               header('HTTP/1.0 500 Internal Server Error');
+               header('Content-type: text/html; charset=utf-8');
+
+               $sSqlError = $oSql->getMessage();
+
+               echo <<<INTERNALFAIL
+       <html>
+         <head><title>Internal Server Error</title></head>
+         <body>
+               <h1>Internal Server Error</h1>
+               <p>Nominatim has encountered an internal error while accessing the database.
+                  This may happen because the database is broken or because of a bug in
+                  the software. If you think it is a bug, feel free to report
+                  it over on <a href="https://github.com/twain47/Nominatim/issues">
+                  Github</a>. Please include the URL that caused the problem and the
+                  complete error details below.</p>
+               <p><b>Message:</b> $sMsg</p>
+               <p><b>SQL Error:</b> $sSqlError</p>
+               <p><b>Details:</b> <pre>
+INTERNALFAIL;
+
+               if (CONST_Debug)
+               {
+                       var_dump($oSql);
+               }
+               else
+               {
+                       echo "<pre>\n".$oSql->getUserInfo()."</pre>";
+               }
+
+               echo "</pre></p></body></head></html>";
+               exit;
+       }
+
+       function failInternalError($sError, $sSQL = false, $vDumpVar = false)
+       {
+               header('HTTP/1.0 500 Internal Server Error');
+               header('Content-type: text/html; charset=utf-8');
+               echo "<html><body><h1>Internal Server Error</h1>";
+               echo '<p>Nominatim has encountered an internal error while processing your request. This is most likely because of a bug in the software.</p>';
+               echo "<p><b>Details:</b> ".$sError,"</p>";
+               echo '<p>Feel free to file an issue on <a href="https://github.com/twain47/Nominatim/issues">Github</a>. Please include the error message above and the URL you used.</p>';
+               if (CONST_Debug)
+               {
+                       echo "<hr><h2>Debugging Information</h2><br>";
+                       if ($sSQL)
+                       {
+                               echo "<h3>SQL query</h3><code>".$sSQL."</code>";
+                       }
+                       if ($vDumpVar)
+                       {
+                               echo "<h3>Result</h3> <code>";
+                               var_dump($vDumpVar);
+                               echo "</code>";
+                       }
+               }
+               echo "\n</body></html>\n";
+               exit;
+       }
+
+
+       function userError($sError)
+       {
+               header('HTTP/1.0 400 Bad Request');
+               header('Content-type: text/html; charset=utf-8');
+               echo "<html><body><h1>Bad Request</h1>";
+               echo '<p>Nominatim has encountered an error with your request.</p>';
+               echo "<p><b>Details:</b> ".$sError,"</p>";
+               echo '<p>If you feel this error is incorrect feel file an issue on <a href="https://github.com/twain47/Nominatim/issues">Github</a>. Please include the error message above and the URL you used.</p>';
+               echo "\n</body></html>\n";
+               exit;
+       }
+
+
 /***************************************************************************
  *
  * Functions for parsing URL parameters
index 6521e774442e9a97b5cefdf89559581629e0313b..95a2ec00b8d214c565033600c6d6be184c5593f5 100755 (executable)
        $oDB =& getDB();
 
        $sSQL = "select placex.place_id, calculated_country_code as country_code, name->'name' as name, i.* from placex, import_polygon_delete i where placex.osm_id = i.osm_id and placex.osm_type = i.osm_type and placex.class = i.class and placex.type = i.type";
-       $aPolygons = $oDB->getAll($sSQL);
-       if (PEAR::isError($aPolygons))
+       $aPolygons = chksql($oDB->getAll($sSQL),
+                           "Could not get list of deleted OSM elements.");
+
+       if (CONST_DEBUG)
        {
-               failInternalError("Could not get list of deleted OSM elements.", $sSQL, $aPolygons);
+               var_dump($aPolygons);
+               exit;
        }
-
-//var_dump($aPolygons);
 ?>
 <!DOCTYPE html>
 <html>
index b3c9591be2a18c3b7807f55a2b26a9f864778aba..495db2b40e668277fd18ceb92bfbc10cba5c923f 100755 (executable)
        $iOsmId = getParamInt('osmid', -1);
        if ($sOsmType && $iOsmId > 0)
        {
-               $sPlaceId = $oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc");
+               $sPlaceId = chksql($oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc"));
 
                // Be nice about our error messages for broken geometry
 
                if (!$sPlaceId)
                {
-                       $aPointDetails = $oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1");
+                       $aPointDetails = chksql($oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1"));
                        if (!PEAR::isError($aPointDetails) && $aPointDetails) {
                                if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
                                {
 
        if (CONST_Use_US_Tiger_Data)
        {
-               $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
+               $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
                if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
        }
 
        if (CONST_Use_Aux_Location_data)
        {
-               $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
+               $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID));
                if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
        }
 
        $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
 
-       // Make sure the point we are reporting on is fully indexed
-       //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
-       //$oDB->query($sSQL);
-
        // Get the details for this point
        $sSQL = "select place_id, osm_type, osm_id, class, type, name, admin_level, housenumber, street, isin, postcode, calculated_country_code as country_code, importance, wikipedia,";
        $sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') as indexed_date, parent_place_id, rank_address, rank_search, get_searchrank_label(rank_search) as rank_search_label, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ";
        $sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
        $sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
        $sSQL .= " from placex where place_id = $iPlaceID";
-       $aPointDetails = $oDB->getRow($sSQL);
-       if (PEAR::IsError($aPointDetails))
-       {
-               failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
-       }
+       $aPointDetails = chksql($oDB->getRow($sSQL),
+                               "Could not get details of place object.");
        $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
 
        $aClassType = getClassTypesWithImportance();
 
        if ($sOutputFormat=='html')
        {
-               $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
+               $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
                $sTileURL = CONST_Map_Tile_URL;
                $sTileAttribution = CONST_Map_Tile_Attribution;
        }
index 7740abbc755765adb6769284d88daa0f2a82003b..b14422fa6a80e7ff8a409ecbc8a8dca940b2c4c4 100755 (executable)
        $iOsmId = getParamInt('osmid', -1);
        if ($sOsmType && $iOsmId > 0)
        {
-               $sPlaceId = $oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc");
+               $sPlaceId = chksql($oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc"));
 
                // Be nice about our error messages for broken geometry
                if (!$sPlaceId)
                {
-                       $aPointDetails = $oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1");
-                       if (!PEAR::isError($aPointDetails) && $aPointDetails) {
+                       $aPointDetails = chksql($oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1"));
+                       if ($aPointDetails) {
                                if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
                                {
                                        $aPointDetails['error_x'] = $aMatches[1];
 
        if (CONST_Use_US_Tiger_Data)
        {
-               $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
+               $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
                if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
        }
 
        if (CONST_Use_Aux_Location_data)
        {
-               $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
+               $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID));
                if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
        }
 
                exit;
        }
 
-       $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
+       $aRelatedPlaceIDs = chksql($oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID"));
 
        $sSQL = "select obj.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea,  st_area(geometry) as area, ";
        $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
        $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
        $sSQL .= " where parent_place_id in (".join(',',$aRelatedPlaceIDs).") and name is not null order by rank_address asc,rank_search asc limit 500) as obj";
        $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
-       $aParentOfLines = $oDB->getAll($sSQL);
+       $aParentOfLines = chksql($oDB->getAll($sSQL));
 
        if (sizeof($aParentOfLines))
        {
index bb350e6c4d7e985a668a5bc63b3339afeba17822..b855214a89a6ece1e65e762ef40f67e0a0ef28e8 100755 (executable)
@@ -12,7 +12,7 @@
        $bReduced = getParamBool('reduced', false);
        $sClass = getParamString('class', false);
 
-       $iTotalBroken = (int) $oDB->getOne('select count(*) from import_polygon_error');
+       $iTotalBroken = (int) chksql($oDB->getOne('select count(*) from import_polygon_error'));
 
        $aPolygons = array();
        while($iTotalBroken && !sizeof($aPolygons))
                if ($bReduced) $sSQL .= " and errormessage like 'Area reduced%'";
                if ($sClass) $sSQL .= " and class = '".pg_escape_string($sClass)."'";
                $sSQL .= " order by updated desc limit 1000";
-               $aPolygons = $oDB->getAll($sSQL);
+               $aPolygons = chksql($oDB->getAll($sSQL));
+       }
+
+       if (CONST_Debug)
+       {
+               var_dump($aPolygons);
+               exit;
        }
-//var_dump($aPolygons);
 ?>
 <!DOCTYPE html>
 <html>
index c6c7305fe9d90ae88664f8f9b44d3d30ce94ffe2..17b8a2ca08178a6eb1c15d3f69fce3fabedcafff 100755 (executable)
 
        if ($sOutputFormat=='html')
        {
-               $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
+               $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
                $sTileURL = CONST_Map_Tile_URL;
                $sTileAttribution = CONST_Map_Tile_Attribution;
        }
index 3c85c8f7b4724e6d5ca14eefafe02f6ec19cc9a1..4baf3ef5572b9b9fc288b7e7485aa3778eb1f30c 100755 (executable)
 
        if ($sOutputFormat=='html')
        {
-               $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
+               $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
        }
        logEnd($oDB, $hLog, sizeof($aSearchResults));