From aafebbb370ecf0c06e8a0d3b799e128f2b48357d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 25 Apr 2022 18:38:13 +0200 Subject: [PATCH] adapt correctly response 400 instead of default 422 --- ereuse_devicehub/api/views.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/api/views.py b/ereuse_devicehub/api/views.py index 297bbbba..b8db0684 100644 --- a/ereuse_devicehub/api/views.py +++ b/ereuse_devicehub/api/views.py @@ -5,7 +5,8 @@ from flask import Blueprint from flask import current_app as app from flask import g, jsonify, request from flask.views import View -from marshmallow import ValidationError +from flask.wrappers import Response +from marshmallow.exceptions import ValidationError from werkzeug.exceptions import Unauthorized from ereuse_devicehub.auth import Auth @@ -51,6 +52,8 @@ class InventoryView(LoginMix, SnapshotMix): self.tmp_snapshots = app.config['TMP_SNAPSHOTS'] self.path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email) snapshot_json = self.validate(snapshot_json) + if type(snapshot_json) == Response: + return snapshot_json try: self.snapshot_json = ParseSnapshotLsHw(snapshot_json).get_snapshot() except Exception as err: @@ -87,11 +90,15 @@ class InventoryView(LoginMix, SnapshotMix): except ValidationError as err: txt = "{}".format(err) uuid = snapshot_json.get('uuid') + sid = snapshot_json.get('sid') error = SnapshotErrors( - description=txt, snapshot_uuid=uuid, severity=Severity.Error + description=txt, snapshot_uuid=uuid, severity=Severity.Error, sid=sid ) error.save(commit=True) - raise err + # raise err + self.response = jsonify(err) + self.response.status_code = 400 + return self.response api.add_url_rule('/inventory/', view_func=InventoryView.as_view('inventory'))