From ca128dfd9b4631b0a638b322563c98811b17586d Mon Sep 17 00:00:00 2001 From: Miha Rekar Date: Sun, 8 Dec 2024 11:54:42 +0100 Subject: [PATCH] Show errors in coffee bag scraping --- app/controllers/coffee_bags_controller.rb | 5 ++-- .../coffee_bag_scraper_controller.js | 26 +++++++++++++++---- .../controllers/notification_controller.js | 10 +++++-- app/lib/coffee_bag_scraper.rb | 2 +- app/views/coffee_bags/_form.html.erb | 20 ++++++++------ 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/app/controllers/coffee_bags_controller.rb b/app/controllers/coffee_bags_controller.rb index 0ef9177f..ec1b8d1a 100644 --- a/app/controllers/coffee_bags_controller.rb +++ b/app/controllers/coffee_bags_controller.rb @@ -64,10 +64,11 @@ def scrape_info Rails.logger.info "Scraping coffee bag info for #{params[:url]} by #{Current.user.id}" info = CoffeeBagScraper.new.get_info(params[:url]) - if info + if info && info[:error].blank? render json: info else - render json: {error: "Could not extract information from URL"}, status: :unprocessable_entity + error = info[:error].presence || "Could not extract information from URL" + render json: {error:}, status: :unprocessable_entity end end diff --git a/app/javascript/controllers/coffee_bag_scraper_controller.js b/app/javascript/controllers/coffee_bag_scraper_controller.js index f1a88948..d8e9fcc2 100644 --- a/app/javascript/controllers/coffee_bag_scraper_controller.js +++ b/app/javascript/controllers/coffee_bag_scraper_controller.js @@ -3,7 +3,11 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { static targets = ["url", "loader"] - async fetch() { + async fetch(event) { + if (event.type === "click") { + event.preventDefault() + } + const url = this.urlTarget.value if (!url) return @@ -24,12 +28,23 @@ export default class extends Controller { body: JSON.stringify({ url }) }) - if (!response.ok) throw new Error('Failed to fetch coffee info') - const data = await response.json() + + if (!response.ok) { + throw new Error(data.error || 'Failed to fetch coffee info') + } + this.populateFields(data) } catch (error) { - console.error('Error fetching coffee info:', error) + const template = document.getElementById("scraper-error-template") + const notificationHtml = template.innerHTML + const updatedNotification = notificationHtml.replace( + 'Something went wrong', + error.message + ) + + const notificationsContainer = document.getElementById("notifications-container") + notificationsContainer.insertAdjacentHTML("beforeend", updatedNotification) } finally { this.loaderTarget.classList.add("hidden") @@ -42,7 +57,8 @@ export default class extends Controller { populateFields(data) { const fields = [ - 'name', 'roast_level', 'country', 'region', 'farm', 'farmer', 'variety', 'elevation', 'processing', 'harvest_time', 'quality_score', 'tasting_notes' + 'name', 'roast_level', 'country', 'region', 'farm', 'farmer', 'variety', + 'elevation', 'processing', 'harvest_time', 'quality_score', 'tasting_notes' ] fields.forEach(field => { diff --git a/app/javascript/controllers/notification_controller.js b/app/javascript/controllers/notification_controller.js index e683e735..ed898d39 100644 --- a/app/javascript/controllers/notification_controller.js +++ b/app/javascript/controllers/notification_controller.js @@ -6,8 +6,10 @@ export default class extends Controller { static values = { timeout: { type: Number, default: 10000 } } connect() { - enter(this.element) - this.timeout = setTimeout(() => this.hideNotification(), this.timeoutValue) + if (!this.parentHidden()) { + enter(this.element) + this.timeout = setTimeout(() => this.hideNotification(), this.timeoutValue) + } } close(event) { @@ -21,4 +23,8 @@ export default class extends Controller { this.element.remove() }) } + + parentHidden() { + return this.element.parentElement.classList.contains('hidden') + } } diff --git a/app/lib/coffee_bag_scraper.rb b/app/lib/coffee_bag_scraper.rb index 7cef81eb..2529819f 100644 --- a/app/lib/coffee_bag_scraper.rb +++ b/app/lib/coffee_bag_scraper.rb @@ -6,7 +6,7 @@ def get_info(url) JSON.parse(response["content"][0]["text"]).compact_blank rescue StandardError => e Appsignal.report_error(e) - nil + {error: e.message} end private diff --git a/app/views/coffee_bags/_form.html.erb b/app/views/coffee_bags/_form.html.erb index 165b988d..4743b475 100644 --- a/app/views/coffee_bags/_form.html.erb +++ b/app/views/coffee_bags/_form.html.erb @@ -3,15 +3,16 @@
-
+
<%= f.label :url, "Web page with information", class: "standard-label" %> - <%= f.text_field :url, - class: "mt-1 standard-input w-full", - data: { - coffee_bag_scraper_target: "url", - scrape_url: scrape_info_roaster_coffee_bags_path(@roaster), - action: "change->coffee-bag-scraper#fetch" - } %> +
+
+ <%= f.text_field :url, class: "mt-0 standard-input", data: {coffee_bag_scraper_target: "url",scrape_url: scrape_info_roaster_coffee_bags_path(@roaster),action: "change->coffee-bag-scraper#fetch"} %> + +
+
<% end %>
+