collectiveidea / interactor

Interactor provides a common interface for performing complex user interactions.
MIT License
3.36k stars 212 forks source link

Where should I log for errors? #177

Closed alextakitani closed 4 years ago

alextakitani commented 5 years ago

I want to log when my Interactor fails, is there a way to do it from inside of the class?

Thanks!

ivandenysov commented 4 years ago

You can put your errors into context.

alextakitani commented 4 years ago

Indeed but that way I have to log the errors on my controller, right?

stulzer commented 4 years ago

Not really, you can do it from an interactor.

        def call
          ActiveRecord::Base.transaction do
            create_resource
          end
        rescue StandardError => e
          Rails.logger.info "Failed to create resource #{e.message}"
          context.fail!(message: 'Could not create your resource')
        end

Another example:

        def call
          log_and_fail unless resource.update(params)
        end

        private

        def log_and_fail
          Rails.logger.info "Failed to update resource #{resource.errors.full_messages}"
          context.fail!(message: 'Fail to update resource resource')
        end

Is pretty much your call.

if you have an organizer for example I would suggest to log inside the interactors it make sense to log. There is no right and wrong for it. But on my code I avoid log errors on the controller level.

alextakitani commented 4 years ago

Thanks!