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

OperationalError from SQlite that indicates a permissions problem. #2508

Merged
merged 12 commits into from Apr 19, 2017
7 changes: 5 additions & 2 deletions beets/ui/__init__.py
Expand Up @@ -1159,10 +1159,13 @@ def _open_library(config):
)
lib.get_item(0) # Test database connection.
except (sqlite3.OperationalError, sqlite3.DatabaseError):
message = ""
log.debug(u'{}', traceback.format_exc())
raise UserError(u"database file {0} could not be opened".format(
if e.args[0] == "unable to open database file":
message = "It might be a permissions problem."
raise UserError(u"database file {0} could not be opened.%s".format(
util.displayable_path(dbpath)
))
)%message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handler already catches errors when we open the database. The bug report on this thread arises after the database is already opened, when it is accessed. So I think the new exception handling bit will need to go in the main function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
I add this in main but it gives me the message from the first UserError excpetion that raised before.

    except sqlite3.OperationalError as e:
        if e.args[0] == "unable to open database file":
            raise UserError(u"unable to open database file. It might be a permissions problem")

Is there a way to do it like db_query.InvalidQueryError in order to check the mutate function in db.py?

Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, cool! Looks like you're almost there. There are actually two ways to go with this:

  • Inside mutate, raise a UserError and let the top-level (main) exception handling print the message.
  • Inside main, just catch the OperationalError directly and print the error message there. (But don't raise a new UserError.)

It's actually a difficult call to say what's better, but I like your idea of raising a new, specialized exception in mutate. In fact, maybe we can invent a new error exception class, sort of like InvalidQueryError, for filesystem access problems like this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
I' ve tried both ways but i have the same problem. In the first way i just add in mutate raise UserError("unable to open database file. It might be a permissions problem") and in the second way i wrote in main this:

    except sqlite3.OperationalError as e:
        if e.args[0] == "unable to open database file":
           print("unable to open database file. It might be a permissions problem")

I wrote also a new error exception class

class AccessFileError(Exception):
     """Permission Exception. Exception that Specifically inidcates the
     possibility of a permissions problem on database file.
     """

In order to work with this way i have to raise an AccessFileError in mutate and catch it in the main?

Thank you very much!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i fixed it and i tried it with all ways. But it works when i deny the write permission in my database file and the traceback that gives me is attempt to write a readonly database . Is this right??
Thank you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to work with this way i have to raise an AccessFileError in mutate and catch it in the main?

Yes, exactly!

But it works when i deny the write permission in my database file and the traceback that gives me is attempt to write a readonly database. Is this right??

It's somewhat hard to know exactly what causes specific kinds of errors in the SQLite library, which hides the precise details of what went wrong. But if you see that error when setting the database file to have permissions that prevent writing by your user, that does sound like something we should handle—perhaps in addition to the existing "unable to open database file" error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But when it gives me the "unable to open datapase file" error it prints the message of UserError that raise the _open_library. Maybe the way i change my settings is wrong?

Thank you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm… can you try pushing your updated code so it appears here in the PR? Then I can take a look at how it's actually working.

log.debug(u'library database: {0}\n'
u'library directory: {1}',
util.displayable_path(lib.path),
Expand Down