Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Raising NoMethodError in chat_channels with other_user is not Present #5792

Closed
mstruve opened this issue Jan 28, 2020 · 8 comments
Closed
Assignees
Labels
bug always open for contribution external contributors welcome contribution is welcome! good first issue good first issues for anyone new to programming and new to the project.

Comments

@mstruve
Copy link
Contributor

mstruve commented Jan 28, 2020

There are times when a ChatChannel is opened via the controller that a other_user for that chat channel is not present. This causes the below error to be raised when we try to index that chat channel membership and attempt to fetch the channel_image
Honeybadger: https://app.honeybadger.io/fault/66984/f79138e2afe22c7a949dc283ec6beec8
Error:

NoMethodError: undefined method `profile_image_url' for nil:NilClass
profile_image.rb  7 initialize(...)
[PROJECT_ROOT]/app/labor/profile_image.rb:7:in `initialize'
5   def initialize(resource)
6     @resource = resource
7     @image_link = resource.profile_image_url
8     @backup_link = "https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png"
9   end

I don't think we should ever be initializing the ProfileImage class with a nil resource so one fix might be to add a guard clause to check for that before initializing the class. It appears we have a lot of safety operators (.&) all over the ChatChannelMembership ActiveRecord model class which would indicate that we don't always expect to find an other_user so this appears to just be a case we forgot to handle.

@mstruve mstruve added bug always open for contribution good first issue good first issues for anyone new to programming and new to the project. external contributors welcome contribution is welcome! honeybadger labels Jan 28, 2020
@anew-bhav
Copy link

@mstruve I would like to work on this issue

@rhymes
Copy link
Contributor

rhymes commented Feb 1, 2020

@anubhav8421 go ahead and thank you!

@anew-bhav
Copy link

anew-bhav commented Feb 1, 2020

It is my first time exploring the dev.to codebase. I was able to setup the environment as defined in the documentation. I have read through the honeybadger issue plus details provided by @mstruve . I understood that we should not call profile_image_url method on resource when it is nil

So if we just use safety operator in resource&.profile_image_url should suffice.

To test and reproduce the issue, please suggest the steps.

@anew-bhav
Copy link

@rhymes @mstruve can you please provide your feed back on the above comment

@mstruve
Copy link
Contributor Author

mstruve commented Feb 4, 2020

So if we just use safety operator in resource&.profile_image_url should suffice.

That would probably work, or it might cause other issues when loading the page. I would suggest writing a test to find out.

@rhymes
Copy link
Contributor

rhymes commented Feb 4, 2020

hi @anubhav8421, as @mstruve mentioned, the best way to find out for sure is to write a spec about it.

As app/labor/profile_image.rb doesn't have an explicit spec file, you could setup something similar to https://github.com/thepracticaldev/dev.to/blob/master/spec/labor/twilio_token_spec.rb
to test it, for example: spec/labor/profile_image_spec.rb and add a test to see what happens if you pass a user that has no profile_image_url

Let me know if you need further info!

@anew-bhav
Copy link

Thanks for the response @mstruve @rhymes I will create a PR soon.

@mstruve
Copy link
Contributor Author

mstruve commented May 12, 2020

Fixed by preventing a channel from being created for a single user #7773

@mstruve mstruve closed this as completed May 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug always open for contribution external contributors welcome contribution is welcome! good first issue good first issues for anyone new to programming and new to the project.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants