From 76b428a28b2875a2caaad0ca894bf14234340e28 Mon Sep 17 00:00:00 2001 From: nicolasleger <570901+nicolasleger@users.noreply.github.com> Date: Thu, 26 Dec 2024 11:59:50 +0100 Subject: [PATCH] feat: empty file error --- app/services/quote_check_service.rb | 50 +++++++++++-------- .../mon-devis-sans-oublis_api_v1_swagger.yaml | 5 +- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/app/services/quote_check_service.rb b/app/services/quote_check_service.rb index ad409c7..d10ef76 100644 --- a/app/services/quote_check_service.rb +++ b/app/services/quote_check_service.rb @@ -53,6 +53,13 @@ def read_quote ) quote_reader.read + unless quote_reader.text&.strip.presence + add_error("file_reading_error", + category: "file", + type: "error") + return + end + quote_check.assign_attributes( text: quote_reader.text, anonymised_text: quote_reader.anonymised_text, @@ -64,27 +71,13 @@ def read_quote read_attributes: quote_reader.read_attributes ) rescue QuoteReader::ReadError - quote_check.assign_attributes( - validation_errors: ["file_reading_error"], - validation_error_details: [{ - id: "#{quote_check.id}#1", - code: "file_reading_error", - category: "file", - type: "error", - title: I18n.t("quote_validator.errors.file_reading_error") - }] - ) + add_error("file_reading_error", + category: "file", + type: "error") rescue QuoteReader::UnsupportedFileType - quote_check.assign_attributes( - validation_errors: ["unsupported_file_format"], - validation_error_details: [{ - id: "#{quote_check.id}#1", - code: "unsupported_file_format", - category: "file", - type: "error", - title: I18n.t("quote_validator.errors.unsupported_file_format") - }] - ) + add_error("unsupported_file_format", + category: "file", + type: "error") end # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/AbcSize @@ -99,4 +92,21 @@ def validate_quote validation_version: quote_validator.version ) end + + # rubocop:disable Metrics/AbcSize + def add_error(code, + category: nil, type: nil, + title: nil) + quote_check.validation_errors ||= [] + quote_check.validation_errors << code + + quote_check.validation_error_details ||= [] + quote_check.validation_error_details << { + id: [quote_check.id, quote_check.validation_error_details.count + 1].compact.join("-"), + code:, + category:, type:, + title: title || I18n.t("quote_validator.errors.#{code}") + } + end + # rubocop:enable Metrics/AbcSize end diff --git a/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml b/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml index a8bbb2d..8e09c59 100644 --- a/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml +++ b/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml @@ -220,9 +220,10 @@ components: type: string enum: - admin + - file - gestes - description: 'admin: Mentions administratives | gestes: Descriptif technique - des gestes' + description: 'admin: Mentions administratives | file: Fichier | gestes: Descriptif + technique des gestes' quote_check_error_code: type: string description: code d'erreur