add safeguard for formatting errors (#708)
This commit is contained in:
+15
-8
@@ -52,6 +52,7 @@ from shapely.errors import WKTReadingError
|
||||
from shapely.wkt import loads as shapely_loads
|
||||
|
||||
from pygeoapi import __version__, l10n
|
||||
from pygeoapi.formatter.base import FormatterSerializationError
|
||||
from pygeoapi.linked_data import (geojson2geojsonld, jsonldify,
|
||||
jsonldify_collection)
|
||||
from pygeoapi.log import setup_logger
|
||||
@@ -1413,14 +1414,20 @@ class API:
|
||||
formatter = load_plugin('formatter',
|
||||
{'name': 'CSV', 'geom': True})
|
||||
|
||||
content = formatter.write(
|
||||
data=content,
|
||||
options={
|
||||
'provider_def': get_provider_by_type(
|
||||
collections[dataset]['providers'],
|
||||
'feature')
|
||||
}
|
||||
)
|
||||
try:
|
||||
content = formatter.write(
|
||||
data=content,
|
||||
options={
|
||||
'provider_def': get_provider_by_type(
|
||||
collections[dataset]['providers'],
|
||||
'feature')
|
||||
}
|
||||
)
|
||||
except FormatterSerializationError as err:
|
||||
LOGGER.error(err)
|
||||
msg = 'Error serializing output'
|
||||
return self.get_exception(
|
||||
500, headers, request.format, 'NoApplicableCode', msg)
|
||||
|
||||
headers['Content-Type'] = '{}; charset={}'.format(
|
||||
formatter.mimetype, self.config['server']['encoding'])
|
||||
|
||||
@@ -65,3 +65,13 @@ class BaseFormatter:
|
||||
|
||||
def __repr__(self):
|
||||
return '<BaseFormatter> {}'.format(self.name)
|
||||
|
||||
|
||||
class FormatterGenericError(Exception):
|
||||
"""formatter generic error"""
|
||||
pass
|
||||
|
||||
|
||||
class FormatterSerializationError(FormatterGenericError):
|
||||
"""formatter serialization error"""
|
||||
pass
|
||||
|
||||
+16
-11
@@ -32,7 +32,7 @@ import logging
|
||||
|
||||
import unicodecsv as csv
|
||||
|
||||
from pygeoapi.formatter.base import BaseFormatter
|
||||
from pygeoapi.formatter.base import BaseFormatter, FormatterSerializationError
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -85,17 +85,22 @@ class CSVFormatter(BaseFormatter):
|
||||
|
||||
LOGGER.debug('CSV fields: {}'.format(fields))
|
||||
|
||||
output = io.BytesIO()
|
||||
writer = csv.DictWriter(output, fields)
|
||||
writer.writeheader()
|
||||
try:
|
||||
output = io.BytesIO()
|
||||
writer = csv.DictWriter(output, fields)
|
||||
writer.writeheader()
|
||||
|
||||
for feature in data['features']:
|
||||
fp = feature['properties']
|
||||
if is_point:
|
||||
fp['x'] = feature['geometry']['coordinates'][0]
|
||||
fp['y'] = feature['geometry']['coordinates'][1]
|
||||
LOGGER.debug(fp)
|
||||
writer.writerow(fp)
|
||||
except ValueError as err:
|
||||
LOGGER.error(err)
|
||||
raise FormatterSerializationError('Error writing CSV output')
|
||||
|
||||
for feature in data['features']:
|
||||
fp = feature['properties']
|
||||
if is_point:
|
||||
fp['x'] = feature['geometry']['coordinates'][0]
|
||||
fp['y'] = feature['geometry']['coordinates'][1]
|
||||
LOGGER.debug(fp)
|
||||
writer.writerow(fp)
|
||||
return output.getvalue()
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
Reference in New Issue
Block a user