|
26 | 26 | from geoengine.types import InternalDatasetId, ProvenanceOutput, QueryRectangle, ResultDescriptor |
27 | 27 | from geoengine.auth import get_session |
28 | 28 | from geoengine.error import GeoEngineException, MethodNotCalledOnPlotException, MethodNotCalledOnRasterException, \ |
29 | | - MethodNotCalledOnVectorException, SpatialReferenceMismatchException |
| 29 | + MethodNotCalledOnVectorException, SpatialReferenceMismatchException, check_response_for_error |
30 | 30 | from geoengine.datasets import StoredDataset, UploadId |
31 | 31 |
|
32 | 32 |
|
@@ -164,10 +164,10 @@ def get_dataframe(self, bbox: QueryRectangle) -> gpd.GeoDataFrame: |
164 | 164 | wfs_url = self.__get_wfs_url(bbox) |
165 | 165 |
|
166 | 166 | data_response = req.get(wfs_url, headers=session.auth_header) |
167 | | - data = data_response.json() |
168 | 167 |
|
169 | | - if 'error' in data: |
170 | | - raise GeoEngineException(data) |
| 168 | + check_response_for_error(data_response) |
| 169 | + |
| 170 | + data = data_response.json() |
171 | 171 |
|
172 | 172 | def geo_json_with_time_to_geopandas(geo_json): |
173 | 173 | ''' |
@@ -249,23 +249,9 @@ def wms_get_map_as_image(self, bbox: QueryRectangle, colorizer_min_max: Tuple[fl |
249 | 249 | wms_request = self.__wms_get_map_request(bbox, colorizer_min_max) |
250 | 250 | response = req.Session().send(wms_request) |
251 | 251 |
|
252 | | - try: |
253 | | - image = Image.open(BytesIO(response.content)) |
254 | | - return image |
255 | | - except Exception as pil_exception: # pylint: disable=broad-except |
256 | | - exception = pil_exception |
| 252 | + check_response_for_error(response) |
257 | 253 |
|
258 | | - # try to parse it as a Geo Engine error |
259 | | - try: |
260 | | - response_json = response.json() |
261 | | - if 'error' in response_json: |
262 | | - # override exception with `GeoEngineException` |
263 | | - exception = GeoEngineException(response_json) |
264 | | - except Exception: # pylint: disable=broad-except |
265 | | - pass # ignore errors, it seemed not to be JSON |
266 | | - |
267 | | - # we either raise the Geo Engine exception or the PIL exception |
268 | | - raise exception |
| 254 | + return Image.open(BytesIO(response.content)) |
269 | 255 |
|
270 | 256 | def __wms_get_map_request(self, |
271 | 257 | bbox: QueryRectangle, |
@@ -342,10 +328,11 @@ def plot_chart(self, bbox: QueryRectangle) -> VegaLite: |
342 | 328 |
|
343 | 329 | plot_url = f'{session.server_url}/plot/{self}?bbox={spatial_bounds}&time={time}&spatialResolution={resolution}' |
344 | 330 |
|
345 | | - response = req.get(plot_url, headers=session.auth_header).json() |
| 331 | + response = req.get(plot_url, headers=session.auth_header) |
346 | 332 |
|
347 | | - if 'error' in response: |
348 | | - raise GeoEngineException(response) |
| 333 | + check_response_for_error(response) |
| 334 | + |
| 335 | + response = response.json() |
349 | 336 |
|
350 | 337 | vega_spec = json.loads(response['data']['vegaString']) |
351 | 338 |
|
@@ -430,10 +417,11 @@ def save_as_dataset(self, bbox: QueryRectangle, name: str, description: str = '' |
430 | 417 | url=f'{session.server_url}/datasetFromWorkflow/{self.__workflow_id}', |
431 | 418 | json=request_body, |
432 | 419 | headers=session.auth_header, |
433 | | - ).json() |
| 420 | + ) |
434 | 421 |
|
435 | | - if 'error' in response: |
436 | | - raise GeoEngineException(response) |
| 422 | + check_response_for_error(response) |
| 423 | + |
| 424 | + response = response.json() |
437 | 425 |
|
438 | 426 | return StoredDataset( |
439 | 427 | dataset_id=InternalDatasetId.from_response(response['dataset']), |
|
0 commit comments