Skip to content

Commit

Permalink
Admin - Log OGC request if error
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Dec 3, 2024
1 parent cb02310 commit d14e21e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
55 changes: 55 additions & 0 deletions lizmap/modules/lizmap/lib/Request/OGCRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,57 @@ protected function constructUrl()
return Proxy::constructUrl($this->parameters(), $this->services, $url);
}

/**
* Generate a string to identify the target of the HTTP request.
*
* @param array $params The list of HTTP params
* @param int $code The HTTP code of the request
*
* @return string The string to identify the HTTP request, with main OGC parameters first such as MAP, SERVICE...
*/
private function formatHttpErrorString($params, $code)
{
$message = '';

$paramsToLog = array('map', 'repository', 'project', 'service', 'request');
foreach ($paramsToLog as $paramName) {
if (array_key_exists($paramName, $params)) {
$message .= strtoupper($paramName)." '".$params[$paramName]."', ";
}
}

$message = rtrim($message, ', ');

return 'HTTP code '.$code.' on '.$message;
}

/**
* Log if the HTTP code is a 4XX or 5XX error code.
*
* @param int $code The HTTP code of the request
*/
protected function logRequestIfError($code)
{
if ($code < 400) {
return;
}

$message = 'The HTTP OGC request to QGIS Server ended with an error.';

// The master error with MAP parameter
// This user must have an access to QGIS Server logs
$params = $this->parameters();
\jLog::log($message.' Check logs on QGIS Server. '.$this->formatHttpErrorString($params, $code).' : '.json_encode($params), 'error');

// The admin error without the MAP parameter
// but replaced by REPOSITORY and PROJECT parameters
// This user might not have an access to QGIS Server logs
unset($params['map']);
$params['repository'] = $this->project->getRepository()->getKey();
$params['project'] = $this->project->getKey();
\jLog::log($message.' '.$this->formatHttpErrorString($params, $code).' : '.json_encode($params), 'lizmapadmin');
}

/**
* Request QGIS Server.
*
Expand Down Expand Up @@ -236,11 +287,15 @@ protected function request($post = false, $stream = false)
if ($stream) {
$response = \Lizmap\Request\Proxy::getRemoteDataAsStream($querystring, $options);

$this->logRequestIfError($response->getCode());

return new OGCResponse($response->getCode(), $response->getMime(), $response->getBodyAsStream());
}

list($data, $mime, $code) = \Lizmap\Request\Proxy::getRemoteData($querystring, $options);

$this->logRequestIfError($code);

return new OGCResponse($code, $mime, $data);
}

Expand Down
12 changes: 6 additions & 6 deletions lizmap/modules/lizmap/lib/Request/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,14 @@ protected static function fileProxy($url, $options)
* @param int $httpCode The HTTP code of the request
* @param string $url The URL of the request, for logging
*/
public static function logRequestIfError($httpCode, $url)
protected static function logRequestIfError($httpCode, $url)
{
$httpCodeClass = substr($httpCode, 0, 1);
// Change to str_starts_with when PHP 8.1 will be minimum version for all maintained version
if ($httpCodeClass == '4' || $httpCodeClass == '5') {
\jLog::log('An HTTP request ended with an error, please check the main error log. Code '.$httpCode, 'lizmapadmin');
\jLog::log('The HTTP request below ended with an error. Code '.$httpCode.''.$url, 'error');
if ($httpCode < 400) {
return;
}

\jLog::log('An HTTP request ended with an error, please check the main error log. HTTP code '.$httpCode, 'lizmapadmin');
\jLog::log('The HTTP request ended with an error. HTTP code '.$httpCode.''.$url, 'error');
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
startup.error=An error occurred while loading this map. Some necessary resources may temporarily be unavailable. Please try again later.
startup.error.administrator=Please notify the administrator of this map to <strong>connect</strong> and <strong>visit</strong> this map.
startup.error.developer.tools=Maybe you will have a clue by opening your "Developer tools" in your web-browser. It's usually <kbd>F12</kbd>. Check "Console" and "Networks".
startup.error.developer.tools=Maybe you will have a clue by opening your "Developer tools" in your web-browser. It's usually <kbd>F12</kbd>. Check "Console" and "Networks".\n\nVisit the administration panel and check in the log page if an HTTP request ended with an error related to this project.
startup.user_defined_js=This map contains some users additional JavaScript scripts. This can be the cause of this message, try disabling them.
startup.goToProject=Home
startup.goToRepositoryAdmin=Maps management page
Expand Down

0 comments on commit d14e21e

Please sign in to comment.