OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
added generic error handling for initial and subsequent requests
Jonas Jabari9ef776016809
diff --git a/app/components/work_packages/activities_tab/error_frame_component.html.erb b/app/components/work_packages/activities_tab/error_frame_component.html.erbnew file mode 100644index 00000000000..6d97c63bf8e--- /dev/null+++ b/app/components/work_packages/activities_tab/error_frame_component.html.erb@@ -0,0 +1,7 @@+<%=+ content_tag("turbo-frame", id: "work-package-activities-tab-content") do+ unless error_message.blank?+ render(Primer::Alpha::Banner.new(scheme: :danger)) { error_message }+ end+ end+%>\ No newline at end of filediff --git a/app/components/work_packages/activities_tab/error_frame_component.rb b/app/components/work_packages/activities_tab/error_frame_component.rbnew file mode 100644index 00000000000..4f18eb400ee--- /dev/null+++ b/app/components/work_packages/activities_tab/error_frame_component.rb@@ -0,0 +1,46 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module WorkPackages+ module ActivitiesTab+ class ErrorFrameComponent < ApplicationComponent+ include ApplicationHelper+ include OpPrimer::ComponentHelpers++ def initialize(error_message: nil)+ super++ @error_message = error_message+ end++ attr_reader :error_message+ end+ end+enddiff --git a/app/components/work_packages/activities_tab/error_stream_component.html.erb b/app/components/work_packages/activities_tab/error_stream_component.html.erbnew file mode 100644index 00000000000..bf1996f9805--- /dev/null+++ b/app/components/work_packages/activities_tab/error_stream_component.html.erb@@ -0,0 +1,7 @@+<%=+ component_wrapper do+ unless error_message.blank?+ render(Primer::Alpha::Banner.new(scheme: :danger, dismiss_scheme: :hide)) { error_message }+ end+ end+%>\ No newline at end of filediff --git a/app/components/work_packages/activities_tab/error_stream_component.rb b/app/components/work_packages/activities_tab/error_stream_component.rbnew file mode 100644index 00000000000..b39a22e2d20--- /dev/null+++ b/app/components/work_packages/activities_tab/error_stream_component.rb@@ -0,0 +1,47 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module WorkPackages+ module ActivitiesTab+ class ErrorStreamComponent < ApplicationComponent+ include ApplicationHelper+ include OpPrimer::ComponentHelpers+ include OpTurbo::Streamable++ def initialize(error_message: nil)+ super++ @error_message = error_message+ end++ attr_reader :error_message+ end+ end+enddiff --git a/app/components/work_packages/activities_tab/index_component.html.erb b/app/components/work_packages/activities_tab/index_component.html.erbindex 0235914aa29..927969fe0a7 100644--- a/app/components/work_packages/activities_tab/index_component.html.erb+++ b/app/components/work_packages/activities_tab/index_component.html.erb@@ -1,41 +1,50 @@<%=content_tag("turbo-frame", id: "work-package-activities-tab-content") do- component_wrapper(data: wrapper_data_attributes, class: "work-packages-activities-tab-index-component") do- flex_layout do |activties_tab_container|- activties_tab_container.with_row(mb: 2) do- render(- WorkPackages::ActivitiesTab::Journals::FilterAndSortingComponent.new(- work_package:,- filter:- )- )- end- activties_tab_container.with_row(flex_layout: true, mt: 3) do |journals_wrapper_container|- journals_wrapper_container.with_row(- classes: "work-packages-activities-tab-index-component--journals-container work-packages-activities-tab-index-component--journals-container_with-initial-input-compensation",- data: { "work-packages--activities-tab--index-target": "journalsContainer" }- ) do- render(- WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package:, filter:)- )- end- if adding_comment_allowed?- journals_wrapper_container.with_row(- classes: "work-packages-activities-tab-index-component--input-container work-packages-activities-tab-index-component--input-container_sort-#{journal_sorting}",- mt: 3,- mb: [3, nil, nil, nil, 0],- pt: 2,- pb: 2,- pl: 3,- pr: [3, nil, nil, nil, 2],- border: [nil, nil, nil, nil, :top],- border_radius: [2, nil, nil, nil, 0],- bg: :subtle- ) do+ flex_layout(classes: "work-packages-activities-tab-index-component") do |activties_tab_wrapper_container|+ activties_tab_wrapper_container.with_row(classes: "work-packages-activities-tab-index-component--errors") do+ render(+ WorkPackages::ActivitiesTab::ErrorStreamComponent.new+ )+ end+ activties_tab_wrapper_container.with_row do+ component_wrapper(data: wrapper_data_attributes) do+ flex_layout do |activties_tab_container|+ activties_tab_container.with_row(mb: 2) dorender(- WorkPackages::ActivitiesTab::Journals::NewComponent.new(work_package:)+ WorkPackages::ActivitiesTab::Journals::FilterAndSortingComponent.new(+ work_package:,+ filter:+ ))end+ activties_tab_container.with_row(flex_layout: true, mt: 3) do |journals_wrapper_container|+ journals_wrapper_container.with_row(+ classes: "work-packages-activities-tab-index-component--journals-container work-packages-activities-tab-index-component--journals-container_with-initial-input-compensation",+ data: { "work-packages--activities-tab--index-target": "journalsContainer" }+ ) do+ render(+ WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package:, filter:)+ )+ end+ if adding_comment_allowed?+ journals_wrapper_container.with_row(+ classes: "work-packages-activities-tab-index-component--input-container work-packages-activities-tab-index-component--input-container_sort-#{journal_sorting}",+ mt: 3,+ mb: [3, nil, nil, nil, 0],+ pt: 2,+ pb: 2,+ pl: 3,+ pr: [3, nil, nil, nil, 2],+ border: [nil, nil, nil, nil, :top],+ border_radius: [2, nil, nil, nil, 0],+ bg: :subtle+ ) do+ render(+ WorkPackages::ActivitiesTab::Journals::NewComponent.new(work_package:)+ )+ end+ end+ endendendenddiff --git a/app/components/work_packages/activities_tab/index_component.sass b/app/components/work_packages/activities_tab/index_component.sassindex febca87c159..d4c6750a2db 100644--- a/app/components/work_packages/activities_tab/index_component.sass+++ b/app/components/work_packages/activities_tab/index_component.sass@@ -1,4 +1,12 @@.work-packages-activities-tab-index-component+ &--errors+ position: absolute+ width: calc(100% - 22px)+ z-index: 11+ @media screen and (max-width: $breakpoint-xl)+ position: fixed+ bottom: 20px+ width: calc(100% - 30px)&--journals-containerz-index: 10padding-top: 3pxdiff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex 15c7cee6bbc..bac9f428171 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -51,10 +51,10 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerif params[:last_update_timestamp].present?generate_time_based_update_streams(params[:last_update_timestamp])else- status = :bad_request+ @turbo_status = :bad_requestend- respond_with_turbo_streams(status: status || :ok)+ respond_with_turbo_streamsenddef update_filter@@ -85,13 +85,13 @@ class WorkPackages::ActivitiesTabController < ApplicationController# we need to call replace in order to properly re-init the index stimulus componentreplace_whole_tabelse- status = :bad_request+ @turbo_status = :bad_requestendelse- status = :bad_request+ @turbo_status = :bad_requestend- respond_with_turbo_streams(status: status || :ok)+ respond_with_turbo_streamsenddef edit@@ -104,10 +104,10 @@ class WorkPackages::ActivitiesTabController < ApplicationController))else- status = :forbidden+ @turbo_status = :forbiddenend- respond_with_turbo_streams(status: status || :ok)+ respond_with_turbo_streamsenddef cancel_edit@@ -120,10 +120,10 @@ class WorkPackages::ActivitiesTabController < ApplicationController))else- status = :forbidden+ @turbo_status = :forbiddenend- respond_with_turbo_streams(status: status || :ok)+ respond_with_turbo_streamsenddef create@@ -133,44 +133,72 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerhandle_successful_create_call(call)elsehandle_failed_create_call(call) # errors should be rendered in the form- status = :bad_request+ @turbo_status = :bad_requestend- respond_with_turbo_streams(status: status || :created)+ respond_with_turbo_streamsenddef update- if journal_params[:notes].present?- call = Journals::UpdateService.new(model: @journal, user: User.current).call(- notes: journal_params[:notes]- )+ call = Journals::UpdateService.new(model: @journal, user: User.current).call(+ notes: journal_params[:notes]+ )- if call.success? && call.result- update_item_component(call.result, state: :show)- else- status = handle_failed_update_call(call)- end+ if call.success? && call.result+ update_item_component(call.result, state: :show)else- # disallow empty notes- status = :bad_request- update_item_component(@journal, state: :edit) # rerender form with initial values+ handle_failed_update_call(call)end- respond_with_turbo_streams(status: status || :ok)+ respond_with_turbo_streamsendprivate+ def respond_with_error(error_message)+ respond_to do |format|+ # turbo_frame requests (tab is initially rendered and an error occured) are handled below+ format.html do+ render(+ WorkPackages::ActivitiesTab::ErrorFrameComponent.new(+ error_message:+ ),+ layout: false,+ status: :not_found+ )+ end+ # turbo_stream requests (tab is already rendered and an error occured in subsequent requests) are handled below+ format.turbo_stream do+ @turbo_status = :not_found+ render_error_banner_via_turbo_stream(error_message)+ end+ end+ end++ def render_error_banner_via_turbo_stream(error_message)+ update_via_turbo_stream(+ component: WorkPackages::ActivitiesTab::ErrorStreamComponent.new(+ error_message:+ )+ )+ end+def find_work_package@work_package = WorkPackage.find(params[:work_package_id])+ rescue ActiveRecord::RecordNotFound+ respond_with_error(I18n.t("label_not_found"))enddef find_project@project = @work_package.project+ rescue ActiveRecord::RecordNotFound+ respond_with_error(I18n.t("label_not_found"))enddef find_journal@journal = Journal.find(params[:id])+ rescue ActiveRecord::RecordNotFound+ respond_with_error(I18n.t("label_not_found"))enddef set_filter@@ -219,14 +247,11 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef handle_failed_update_call(call)- status = if call.errors&.first&.type == :error_unauthorized- :forbidden- else- :bad_request- end- update_item_component(call.result, state: :edit) # errors should be rendered in the form-- status+ @turbo_status = if call.errors&.first&.type == :error_unauthorized+ :forbidden+ else+ :bad_request+ endenddef replace_whole_tab