Skip to content

Commit

Permalink
feat: empty file error
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasleger committed Dec 26, 2024
1 parent 8c4fd36 commit 76b428a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
50 changes: 30 additions & 20 deletions app/services/quote_check_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
5 changes: 3 additions & 2 deletions swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76b428a

Please sign in to comment.