wakatime/wakatime-mode

Recent regression with not passing API key

alexander-yakushev opened this issue · 14 comments

I've updated to the latest Wakatime (both wakatime-mode package, and the Python code), and started getting constant WakaTime Error (2). It appeared that because of 77dc9a8 API key is no longer passed to the Python CLI, which results in an error. Now, it is expected that 102 error will be caught and retried differently, but for me (and probably for other people) cli.py crashes with error code 2 if key is not passed.

% python cli.py                                                                            
Error: Could not read from config file /home/unlogic/.wakatime.cfg
usage: cli.py [-h] [--entity FILE] [--key KEY] [--write] [--plugin PLUGIN]
              [--time time] [--lineno LINENO] [--cursorpos CURSORPOS]
              [--entity-type ENTITY_TYPE] [--proxy PROXY] [--project PROJECT]
              [--alternate-project ALTERNATE_PROJECT]
              [--alternate-language ALTERNATE_LANGUAGE] [--hostname HOSTNAME]
              [--disableoffline] [--hidefilenames] [--exclude EXCLUDE]
              [--include INCLUDE] [--extra-heartbeats] [--logfile LOGFILE]
              [--apiurl API_URL] [--timeout TIMEOUT] [--config CONFIG]
              [--verbose] [--version]
cli.py: error: Missing api key
% echo $?
2

Exit code 2 happens when a required command line argument is missing, or invalid. Exit code 102 happens when the api key was defined but is incorrect. We can change this to return 102 from any api key related error.

% /usr/bin/python2 ~/Software/wakatime/wakatime/cli.py --file "/home/unlogic/projects/elisp/test-prs/README.md" --write --plugin emacs-wakatime/1.0.2  --time 1476112056.41 --key xxxx-xx-xxx-xxx           
Error: Could not read from config file /home/unlogic/.wakatime.cfg
% echo $?                                                                                                              
0
% /usr/bin/python2 ~/Software/wakatime/wakatime/cli.py --file "/home/unlogic/projects/elisp/test-prs/README.md" --write --plugin emacs-wakatime/1.0.2  --time 1476112056.41                               
Error: Could not read from config file /home/unlogic/.wakatime.cfg
usage: wakatime [-h] [--entity FILE] [--key KEY] [--write] [--plugin PLUGIN]
                [--time time] [--lineno LINENO] [--cursorpos CURSORPOS]
                [--entity-type ENTITY_TYPE] [--proxy PROXY]
                [--project PROJECT] [--alternate-project ALTERNATE_PROJECT]
                [--alternate-language ALTERNATE_LANGUAGE]
                [--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
                [--exclude EXCLUDE] [--include INCLUDE] [--extra-heartbeats]
                [--logfile LOGFILE] [--apiurl API_URL] [--timeout TIMEOUT]
                [--config CONFIG] [--verbose] [--version]
wakatime: error: Missing api key
% [2] echo $?                                                                                                          
2

Exit code 102 happens when the api key was defined but is incorrect.

% /usr/bin/python2 ~/Software/wakatime/wakatime/cli.py --file "/home/unlogic/projects/elisp/test-prs/README.md" --write --plugin emacs-wakatime/1.0.2  --time 1476112056.41 --key wrong                              
Error: Could not read from config file /home/unlogic/.wakatime.cfg
% echo $?
104

Oops, I meant exit code 104 not 102. I've updated wakatime cli to exit with status 104 when the api key is missing now and wakatime-mode to use the correct 104 exit code.

To get the new version of wakatime cli, you can upgrade it using pip:

sudo pip install --upgrade wakatime

As you can see in the logs I've posted, it exists with 104 if the key is wrong, and with 2 if the --key whatever is not present. The change in the Emacs code doesn't supply --key whatever if dont-use-key is true. It is visible from the code, and I added the logging of the CLI command being run (that's where I got the command I use for tests from).

I don't install wakatime from pip, I download the latest release from github and unpack it. Could it be the reason for the difference in exit codes?

I see you've tackled both issues now. I will update and report. Thank you!

BTW, is config file obligatory now? If so, it would be a good idea to include it in the Emacs installation guide (on the website).

I'm the author of the bogus(?) commit, and I made the API key configuration optional. If it is set in .emacs, it should use it. If that option is nil, and no config file exists, it should ask for one, and set the relevant variable accordingly. If the problem still persists with the new CLI version, let me know and I'll take a look into it.

Fabbi commented

With wakatime 6.1.0 from pip I'm getting an error 103 though..

➤  /opt/local/bin/python /Library/Python/2.7/site-packages/wakatime/cli.py --file \"/Users/Fabbi/.emacs.d/elpa/wakatime-mode-20161011.101/wakatime-mode.el\"  --plugin emacs-wakatime/1.0.2  --time 1476877032.43 --key wrong
Error: Could not read from config file /Users/Fabbi/.wakatime.cfg
➤  echo $?
103

You have to have an empty cfg file for 104 to occur.
But if you pass the wrong key it returns 0:

➤  /opt/local/bin/python /Library/Python/2.7/site-packages/wakatime/cli.py --file \"/Users/Fabbi/.emacs.d/elpa/wakatime-mode-20161011.101/wakatime-mode.el\"  --plugin emacs-wakatime/1.0.2  --time 1476877032.43 --key wrong
➤  echo $?
0

also since the call always tells to dont-use-key, it will always fail, won't it?

@Fabbi that's right, we should be prompting for an api key if we get a 103 or 104 exit status:

https://github.com/wakatime/wakatime/blob/master/wakatime/constants.py

Fabbi commented

either way I don't get a prompt. I have the api key custom variable set and created the cfg. I now get 104 but no prompt is happening.

Because the prompt comes up only if the exit code is 102. Let me check the
exit codes and update this piece of code.

On Wed, Oct 19, 2016, 14:25 f.schweinfurth notifications@github.com wrote:

either way I don't get a prompt. I have the api key custom variable set
and created the cfg. I now get 104 but no prompt is happening.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#25 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAQJJanfLScefszSUho4HlzUWmAX0g6uks5q1gw4gaJpZM4KSrT5
.

Fabbi commented

@gergelypolonkai it says

         (when (= 104 exit-status)
           ; If we are retrying already, error out
           (if ,retrying
               (error "WakaTime Error (%s)" exit-status)
             ; otherwise, ask for an API key and call ourselves
             ; recursively
             (wakatime-prompt-api-key)
             (wakatime-call ,savep t)
           )
         )

so this part seems to be right.
Even if I call (wakatime-prompt-api-key) it does not work.

4a99c5a adds a check for 103 to prompt for the api key when the API says it's incorrect and when the config file could not be read.

@alanhamlett msg corresponds to error 103 is An error occured while parsing ~/.wakatime.cfg. I manually created the file ~/.wakatime.cfg with its content:

[settings]
api_key = my-api

and everything is ok!

This is fixed in the latest wakatime cli release. To get the update, run:
pip install --upgrade wakatime