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

Fixed Nonetype error! #3233

Merged
merged 3 commits into from
Mar 24, 2025
Merged

Fixed Nonetype error! #3233

merged 3 commits into from
Mar 24, 2025

Conversation

mAmanullah7
Copy link
Contributor

Problem

Screenshot 2025-03-21 at 10 48 44 AM

I encountered an issue in the artist_similarity function within listenbrainz/webserver/views/explore.py at line 36. The specific error was a TypeError: 'NoneType' object is not subscriptable, which occurred when I tried to access the first element ([0]) of a database query result using artist_mbid = result.fetchone()[0]. The root cause was that the fetchone() method returned None when the database query didn’t find any matching records, likely because the artist name or ID I was searching for didn’t exist in the database, or there might be a logic issue in the SQL query itself. This caused the application to crash since I was attempting to index a None value, which isn’t possible.

This issue could affect users trying to explore artist similarity if the queried artist isn’t found, leading to an unhandled error instead of a graceful response.

Solution

To fix this, I modified the artist_similarity function to handle the case where fetchone() returns None. I introduced a new variable, result_row, to store the result of result.fetchone(). Then, I added a conditional check: if result_row is None, I flash a message to the user saying "Artist not found" and redirect them to the explore index page. If result_row is not None, I proceed to extract the artist_mbid from result_row[0] and continue with the rest of the function as before. This ensures the application doesn’t crash when the artist isn’t found and provides a better user experience by redirecting with a helpful message.

Here’s the updated code for clarity:

result_row = result.fetchone()
if result_row is None:
    flash("Artist not found")
    return redirect(url_for('explore.index'))
else:
    artist_mbid = result_row[0]
data = {
    "algorithm": "session_based_days_7500_session_300_contribution_5_threshold_10_limit_100_filter_True_skip_30",
    "artist_mbid": artist_mbid
}
return jsonify(data) 

I considered other approaches, like adding a default value for artist_mbid, but I felt that redirecting with a flash message was more appropriate for the user experience in this context. I also didn’t modify the underlying SQL query since the issue seems to be more about handling the absence of results rather than a query error, but this could be revisited if needed.

Action

I’d appreciate it if the reviewers could take a look at the conditional logic I added to ensure it aligns with the project’s standards for error handling and user feedback. Additionally, if there are any suggestions for improving the flash message or the redirect behavior, I’d be happy to incorporate them. No other actions are needed beyond merging the change.

@MonkeyDo
Copy link
Member

MonkeyDo commented Mar 21, 2025

the issue seems to be more about handling the absence of results rather than a query error

This is absolutely right.

However I think the solution of the flash message + redirect is perhaps not what we want.
In all other cases where we don't have data, we simply send back a 404, sometimes with a simple error message, and adapt the front-end to expect such an error if required.

Take for example this non-existing artist: https://listenbrainz.org/artist/37e9d7b2-7779-41b2-b2eb-3685351caad2/
image

Sure, it's pretty ugly and the error page could definitely be improved, but I mainly wanted to show that this is the usual mechanism we go for.
So the server should indeed ensure the server code does not raise an exception, and instead return a 404 response if there are no results.

See the implementation of the above example here:

if len(artist_data) == 0:
return jsonify({"error": f"artist {artist_mbid} not found in the metadata cache"}), 404

@mAmanullah7
Copy link
Contributor Author

To fix this, I modified the artist_similarity function to handle the case where fetchone() returns None. I introduced a new variable, result_row, to store the result of result.fetchone(). Then, I added a conditional check: if result_row is None, I return a 404 response with a JSON error message, following the pattern used elsewhere in the project (e.g., in entity_pages.py for non-existing artists). If result_row is not None, I proceed to extract the artist_mbid from result_row[0] and continue with the rest of the function as before. This ensures the application doesn’t crash when the artist isn’t found and returns a consistent 404 response, which the front-end can handle as needed.

Here’s the updated code for clarity:

result_row = result.fetchone()
if result_row is None:
    return jsonify({"error": f"Artist not found in the database"}), 404
else:
    artist_mbid = result_row[0]
data = {
    "algorithm": "session_based_days_7500_session_300_contribution_5_threshold_10_limit_100_filter_True_skip_30",
    "artist_mbid": artist_mbid
}
return jsonify(data)

I initially considered using a flash message and redirect, but after reviewing the project’s conventions (e.g., in entity_pages.py at lines 159–161), I realized that returning a 404 with a JSON error message is the standard approach for handling missing data. This aligns with how ListenBrainz handles similar cases, such as a non-existing artist (e.g., https://listenbrainz.org/artist/37e9d7b2-7779-41b2-b2eb-3685351caad2/), where a 404 is returned. I didn’t modify the underlying SQL query since the issue is more about handling the absence of results rather than a query error, as noted.

@MonkeyDo
Copy link
Member

Hey, regarding your previous comment, I want to raise an important topic: the comment seems to me to have been written by a large language model, and AI-detection tools seem to support that theory.
Of course, ignore this if you did not use AI to write.

We do not accept any use of AI or LLMs for communication, for problem solving or for generating code.
I will refer you to our GSOC getting started page:

We discourage the use of AI tools in communications and contributions.
The goal of GSoC is for you to learn and improve your skills, not just to finish your project at any cost.
However, if you do use any AI or LLMs, you must disclose their use in your PRs and/or conversations with your mentor. Additionally, you must fully understand and be able to explain any AI-generated code you submit.
Any undisclosed use of AI or Large Language Models, or inability to explain AI-generated contributions, will be grounds for instant disqualification.

@MonkeyDo MonkeyDo merged commit 7893558 into metabrainz:master Mar 24, 2025
2 checks passed
@mAmanullah7
Copy link
Contributor Author

Thanks for the review @MonkeyDo
i just wants to apologize for using ai unintentionally to rewrite my comment in .md format which i should avoided and write myself!
Thanks for pointing out the the gsoc starting getting page i've gone through it & i'll definitely take care from now onwards.

@MonkeyDo
Copy link
Member

I would like some clarification: did you use AI to generate text for the PR, comments or code, or only to format your own text content?

@mAmanullah7
Copy link
Contributor Author

Sure, i just written my comment and converted it to .md format for my own text content thats it!

@MonkeyDo
Copy link
Member

Well, it definitely feels like that is a lie.
There is a very clear difference in the language you used for the description and first comment on this PR compared to the last two comments. That is not just formatting to markdown.

For clarity, I am here to evaluate you on the quality of your code and your problem-solving skills, NOT on how well you speak english.

Please understand that when you use an LLM for something, even if it is just to rephrase your comments, it is practically impossible for me to evaluate if you used an LLM for all the rest or not, making me doubt if you solved the problems by yourself.

I am not interested in mentoring an LLM, and it puts you in a bad light.

@mAmanullah7
Copy link
Contributor Author

Sure i understand and again i wants to applogize for use of LLM for formatting my own content.
Yeah i can also feel the difference because of the way i'm writing now and the way i wrote before! its because when i converted .md format it automatically changed my original text to better way and i thought its better so i used this one rather than my own just thinking my english is not much fluent!

I accept my mistake and i understand that posting original is far better and trustable than posting polished !!
@MonkeyDo from now i'll make sure not to use any kind of LLM to rephrase and correct my format or rephrase my grammatical mistakes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants