Commit 325b1c45 authored by Christophe Benz's avatar Christophe Benz

Update some source errors handling

parent a9be9ffb
......@@ -327,41 +327,27 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
files = {'file': (source.filename, source.build_reader())}
data = {"schema": schema_instance.url}
req = requests.post(api_url, data=data, files=files, headers=headers)
# 400
if req.status_code == 400:
json_response = req.json()
if json_response.get('_meta') and json_response.get('_meta').get('args'):
response_args = json_response.get('_meta').get('args')
if response_args.get('code') == 'cant_detect_csv_dialect':
flash_error("Format de fichier CSV non reconnu")
return redirect(compute_validation_form_url(schema_instance))
if response_args.get('code') == 'missing_headers':
flash_error("Impossible d'extraire les noms de colonne du fichier")
return redirect(compute_validation_form_url(schema_instance))
flash_error("Une erreur est survenue durant la validation: {}"
.format(json_response.get('message')))
return redirect(compute_validation_form_url(schema_instance))
if not req.ok:
flash_error("Un erreur s'est produite côté serveur :-(")
return redirect(compute_validation_form_url(schema_instance))
json_response = req.json()
validata_core_report = json_response['report']
schema_dict = json_response['schema']
except requests.ConnectionError as err:
logging.exception(err)
flash_error(str(err))
return redirect(url_for('home'))
if not req.ok:
flash_error("Une erreur s'est produite côté serveur :-(")
return redirect(compute_validation_form_url(schema_instance))
json_response = req.json()
validata_core_report = json_response['report']
# Computes badge from report and badge configuration
badge = compute_badge(validata_core_report, config.BADGE_CONFIG)
badge_url, badge_msg = get_badge_url_and_message(badge)
source_errors = [err for err in validata_core_report['tables'][0]['errors'] if err['code'] == 'source-error']
source_errors = [
err
for err in validata_core_report['tables'][0]['errors']
if err['code'] in {'source-error', 'unknown-csv-dialect'}
]
if source_errors:
err = source_errors[0]
msg = "l'encodage du fichier est invalide. Veuillez le corriger" if 'charmap' in err[
......@@ -375,7 +361,7 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
report_datetime = datetime.fromisoformat(validata_core_report['date']).astimezone()
# Enhance validata_core_report
validata_report = create_validata_ui_report(validata_core_report, schema_dict)
validata_report = create_validata_ui_report(validata_core_report, schema_instance.spec)
# Display report to the user
validator_form_url = compute_validation_form_url(schema_instance)
......@@ -387,7 +373,7 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
validation_date=report_datetime.strftime('le %d/%m/%Y à %Hh%M'),
source=source, source_data=source_data,
schema_current_version=schema_instance.ref,
doc_url = schema_instance.doc_url,
doc_url=schema_instance.doc_url,
print_mode=request.args.get('print', 'false') == 'true',
badge_url=badge_url, badge_msg=badge_msg,
report_str=json.dumps(validata_report, sort_keys=True, indent=2),
......@@ -546,7 +532,7 @@ def custom_validator():
return render_template('validation_form.html', title=title,
schema_info=schema_info, schema_versions=schema_versions,
schema_current_version=schema_instance.ref,
doc_url = schema_instance.doc_url,
doc_url=schema_instance.doc_url,
schema_params=schema_instance.request_parameters(),
breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'}, ])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment