diff --git a/autotest/gdrivers/ogcapi.py b/autotest/gdrivers/ogcapi.py
index 7fec70675248..fdebf3a42427 100644
--- a/autotest/gdrivers/ogcapi.py
+++ b/autotest/gdrivers/ogcapi.py
@@ -382,7 +382,10 @@ def test_ogr_ogcapi_raster(api, collection, tmp_path):
)
def test_ogc_api_wrong_collection(api, of_type):
- with pytest.raises(Exception, match="Invalid data collection"):
+ with pytest.raises(
+ Exception,
+ match=r"HTTP error code : 400,
GNOSIS Map Server \(OGCAPI\) - 400 Bad Request
Invalid data collection
",
+ ):
gdal.OpenEx(
f"OGCAPI:http://127.0.0.1:{gdaltest.webserver_port}/fakeogcapi/collections/NOT_EXISTS",
of_type,
diff --git a/autotest/ogr/ogr_oapif.py b/autotest/ogr/ogr_oapif.py
index b9b76ab58e71..53156723229e 100755
--- a/autotest/ogr/ogr_oapif.py
+++ b/autotest/ogr/ogr_oapif.py
@@ -66,7 +66,15 @@ def test_ogr_oapif_errors():
handler = webserver.SequentialHandler()
handler.add("GET", "/oapif/collections", 404)
with webserver.install_http_handler(handler):
- with pytest.raises(Exception):
+ with pytest.raises(Exception, match="HTTP error code : 404"):
+ ogr.Open("OAPIF:http://localhost:%d/oapif" % gdaltest.webserver_port)
+
+ handler = webserver.SequentialHandler()
+ handler.add("GET", "/oapif/collections", 404, {}, "unavailable resource")
+ with webserver.install_http_handler(handler):
+ with pytest.raises(
+ Exception, match="HTTP error code : 404, unavailable resource"
+ ):
ogr.Open("OAPIF:http://localhost:%d/oapif" % gdaltest.webserver_port)
# No Content-Type
diff --git a/frmts/ogcapi/gdalogcapidataset.cpp b/frmts/ogcapi/gdalogcapidataset.cpp
index da3f74d6ac5e..7157d1979104 100644
--- a/frmts/ogcapi/gdalogcapidataset.cpp
+++ b/frmts/ogcapi/gdalogcapidataset.cpp
@@ -511,10 +511,15 @@ bool OGCAPIDataset::Download(const CPLString &osURL, const char *pszPostContent,
if (psResult->pszErrBuf != nullptr)
{
- CPLError(CE_Failure, CPLE_AppDefined, "%s",
- psResult->pabyData
- ? reinterpret_cast(psResult->pabyData)
- : psResult->pszErrBuf);
+ std::string osErrorMsg(psResult->pszErrBuf);
+ const char *pszData =
+ reinterpret_cast(psResult->pabyData);
+ if (pszData)
+ {
+ osErrorMsg += ", ";
+ osErrorMsg.append(pszData, CPLStrnlen(pszData, 1000));
+ }
+ CPLError(CE_Failure, CPLE_AppDefined, "%s", osErrorMsg.c_str());
CPLHTTPDestroyResult(psResult);
return false;
}
diff --git a/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp b/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp
index 5036731e408b..652d4a0d816e 100644
--- a/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp
+++ b/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp
@@ -431,10 +431,15 @@ bool OGROAPIFDataset::Download(const CPLString &osURL, const char *pszAccept,
if (psResult->pszErrBuf != nullptr)
{
- CPLError(CE_Failure, CPLE_AppDefined, "%s",
- psResult->pabyData
- ? reinterpret_cast(psResult->pabyData)
- : psResult->pszErrBuf);
+ std::string osErrorMsg(psResult->pszErrBuf);
+ const char *pszData =
+ reinterpret_cast(psResult->pabyData);
+ if (pszData)
+ {
+ osErrorMsg += ", ";
+ osErrorMsg.append(pszData, CPLStrnlen(pszData, 1000));
+ }
+ CPLError(CE_Failure, CPLE_AppDefined, "%s", osErrorMsg.c_str());
CPLHTTPDestroyResult(psResult);
return false;
}