Skip to content

Commit

Permalink
Merge pull request #70 from metno/error_handling
Browse files Browse the repository at this point in the history
Error handling and return report to user
  • Loading branch information
mortenwh authored Jun 4, 2021
2 parents cb82220 + 801e456 commit 7c2ab45
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 372 deletions.
39 changes: 23 additions & 16 deletions dmci/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import uuid

from lxml import etree
from flask import request, Flask, after_this_request
from flask import request, Flask

from dmci.api.worker import Worker

Expand Down Expand Up @@ -55,33 +55,40 @@ def __init__(self):

# Set up api entry points
@self.route("/v1/insert", methods=["POST"])
def base():
def post_insert():
max_permitted_size = self._conf.max_permitted_size

if request.content_length > max_permitted_size:
return f"File bigger than permitted size: {max_permitted_size}", 413

data = request.get_data()

# Cache the job file
file_uuid = uuid.uuid4()
path = self._conf.distributor_cache
full_path = os.path.join(path, f"{file_uuid}.Q")
msg, code = self._persist_file(data, full_path)
if code != 200:
return msg, code

# Run the validator
worker = Worker(full_path, self._xsd_obj)

@after_this_request
def dist(response):
nonlocal worker
worker.distribute()
return response

result, msg = worker.validate(data)
if result:
return self._persist_file(data, full_path)
valid, msg = worker.validate(data)
if not valid:
return msg, 400

# Run the distributors
err = []
status, valid, _, failed, skipped = worker.distribute()
if not status:
err.append("The following distributors failed: %s" % ", ".join(failed))
if not valid:
err.append("The following jobs were skipped: %s" % ", ".join(skipped))

if err:
return "\n".join(err), 500
else:
return msg, 500

# TODO: shouldn't msg be logged?
return "Everything is OK", 200

return

Expand All @@ -99,7 +106,7 @@ def _persist_file(data, full_path):

except Exception as e:
logger.error(str(e))
return "Can't write to file", 507
return "Cannot write xml data to cache file", 507

return "", 200

Expand Down
12 changes: 9 additions & 3 deletions dmci/api/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from lxml import etree

from dmci import CONFIG
from dmci.mmd_tools import full_check
from dmci.tools import CheckMMD
from dmci.distributors import FileDist, PyCSWDist

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -111,13 +111,15 @@ def distribute(self):
**self._kwargs
)
valid &= obj.is_valid()
if obj.is_valid:
if obj.is_valid():
obj_status = obj.run()
status &= obj_status
if obj_status:
called.append(dist)
else:
failed.append(dist)
else:
skipped.append(dist)

return status, valid, called, failed, skipped

Expand All @@ -139,11 +141,15 @@ def _check_information_content(self, data):

# Check XML file
logger.info("Performing in depth checking.")
valid = full_check(xml_doc)
checker = CheckMMD()
valid = checker.full_check(xml_doc)
if valid:
msg = "Input MMD XML file is ok"
else:
_, _, err = checker.status()
msg = "Input MMD XML file contains errors, please check your file"
if err:
msg += "\n" + "\n".join(err)

return valid, msg

Expand Down
237 changes: 0 additions & 237 deletions dmci/mmd_tools/check_mmd.py

This file was deleted.

4 changes: 2 additions & 2 deletions dmci/mmd_tools/__init__.py → dmci/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
limitations under the License.
"""

from dmci.mmd_tools.check_mmd import full_check
from dmci.tools.check_mmd import CheckMMD

__all__ = [
"full_check",
"CheckMMD",
]
Loading

0 comments on commit 7c2ab45

Please sign in to comment.