From 867a21935d5be606185441601da1118e77be2243 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Tue, 14 Nov 2023 18:01:13 +0100 Subject: [PATCH] fixing problems with error handling --- app/controllers/articles_controller.rb | 15 +-------------- app/controllers/subscriptions_controller.rb | 2 +- app/mailers/subscription_mailer.rb | 6 ------ app/services/subscriptions/subscribe.rb | 15 +++++---------- app/services/subscriptions/unsubscribe.rb | 6 ------ 5 files changed, 7 insertions(+), 37 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 3f6131b..7c1d8f0 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -24,13 +24,6 @@ def create def show @article = Article.find_by(id: params[:id]) - - if @article - render 'show' - else - flash[:alert] = "Error showing the article." - redirect_to articles_path - end end def destroy @@ -56,18 +49,12 @@ def update if @article.update(article_params) redirect_to @article, notice: 'Article was successfully updated.' else + flash[:alert] = "Article not found." render :edit end end def edit @article = Article.find_by(id: params[:id]) - - if @article - render 'edit' - else - flash[:alert] = "Article not found." - redirect_to articles_path - end end def my_articles diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index f185616..85faa69 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -2,7 +2,7 @@ class SubscriptionsController < ApplicationController before_action :authenticate_user! def create - result = Subscriptions::Subscribe.new(params,current_user,article).call + Subscriptions::Subscribe.new(params,current_user,article).call redirect_to article, notice: 'Subscribed to the article.' end diff --git a/app/mailers/subscription_mailer.rb b/app/mailers/subscription_mailer.rb index e639385..00a154b 100644 --- a/app/mailers/subscription_mailer.rb +++ b/app/mailers/subscription_mailer.rb @@ -1,8 +1,5 @@ class SubscriptionMailer < ApplicationMailer def new_subscription_notification(subscriber, article) - raise ArgumentError, "Subscriber cannot be nil" if subscriber.nil? - raise ArgumentError, "Article cannot be nil" if article.nil? - @subscriber = subscriber @article = article @@ -10,9 +7,6 @@ def new_subscription_notification(subscriber, article) mail(to: subscriber.email, subject: "You subscribe new article which title is #{article.title}") end def delete_subscription_notification(subscriber, article) - raise ArgumentError, "Subscriber cannot be nil" if subscriber.nil? - raise ArgumentError, "Article cannot be nil" if article.nil? - @subscriber = subscriber @article = article diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index db403aa..50e5215 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -13,25 +13,20 @@ def call end def new_subscription - if @article - @user.subscribed_articles << @article - else - raise ArgumentError, "Cannot create subscription without an article" + @user.subscribed_articles << @article + + if !@user.subscribed_articles.include?(@article) + flash[:alert] = "Error creating the subscription." + redirect_to @article end - rescue StandardError => e - puts "Error in new_subscription: #{e.message}" end def adding_points @user.update!(points: @user.points + 10) - rescue StandardError => e - puts "Error in adding_points: #{e.message}" end def sending_mails SubscriptionMailer.new_subscription_notification(@user, @article).deliver_now - rescue StandardError => e - puts "Error in sending_mails: #{e.message}" end end end \ No newline at end of file diff --git a/app/services/subscriptions/unsubscribe.rb b/app/services/subscriptions/unsubscribe.rb index 1be2005..9c90f8f 100644 --- a/app/services/subscriptions/unsubscribe.rb +++ b/app/services/subscriptions/unsubscribe.rb @@ -22,15 +22,9 @@ def cancel_subscription end def cancel_points @user.update!(points: @user.points - 10) - - rescue StandardError => e - puts "Error in cancel_points: #{e.message}" end def cancel_sending_mails SubscriptionMailer.delete_subscription_notification(@user, @article).deliver_now - - rescue StandardError => e - puts "Error in sending_mails: #{e.message}" end end end