s3rvac / weechat-notify-send

A WeeChat script that sends highlight and message notifications through notify-send.
MIT License
176 stars 19 forks source link

Report an error unless a notification has been successfully shown #25

Closed ant-222 closed 4 years ago

ant-222 commented 4 years ago

With libnotify installed, but no notification daemon available, weechat-notify-send does not show an error whenever it fails to show a notification. I think it is to be expected that a program should report an error in case of any failure.

I have written a small test program in C that tryies to display a notification via libnotify. It reports the following error:

Show failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown:
The name org.freedesktop.Notifications was not provided by any .service files

from the function notify_notification_show(). Does weechat-notify-send handle and report errors from this function in the Weechat client? If not, then I think it a bug and ask the developers to fix it, so that no error goes unnoticed.

This is my test program:

#include <string.h>
#include <libnotify/notify.h>
#include <glib-2.0/glib.h>

int main( int argc, char** argv )
{  char ERR_INIT[80] = "Initialisation failed.";
   char MSG_OK  [80] = "All is well.";
   char ERR_NEW [80] = "Failed to create a notification.";
   char ERR_SHOW[80] = "Show failed: ";

   GError             *error;
   NotifyNotification *notif;
   gboolean           inited;
   char*              resmsg;

   if( !notify_init("test") )
   {  resmsg = ERR_INIT; goto End;  }
   error = NULL;
   notif = notify_notification_new("Title", "Message", NULL );
   if( notif == NULL )
   {  resmsg = ERR_NEW; goto End;  }
   if( !notify_notification_show(notif, &error ) )
   {  resmsg = strcat( ERR_SHOW, error->message );
      g_error_free( error );
      goto End;
   }
   resmsg = MSG_OK;
End:
   printf("%s\n", resmsg);
}
s3rvac commented 4 years ago

Hi! Thank you for the report. The weechat-notify-send script runs notify-send to send the notification, i.e. it does not use libnotify directly, but rather a program that does the interaction with libnotify. Here is the piece of code that does the call:

try:
    subprocess.check_call(
        notify_cmd,
        stderr=subprocess.STDOUT,
        stdout=devnull,
    )
except Exception as ex:
    error_message = '{} (reason: {!r}). {}'.format(
        'Failed to send the notification via notify-send',
        '{}: {}'.format(ex.__class__.__name__, ex),
        'Ensure that you have notify-send installed in your system.',
    )
    print(error_message, file=sys.stderr)

So, when the notify-send [..] call fails, an error message is reported in the main WeeChat's buffer by writing it to stderr. If you do not see any errors there, it means that notify-send returned 0 in your case (success). What happens if you run the following commands from a shell?

$ notify-send test me
$ echo $?

Does it print 0 or another number? If it prints 0, then I am afraid that there is nothing to do as return code 0 means that everything went OK. If you know a way (apart from rewriting the notification script to C, which I will not do), feel free to re-open and I can incorporate that into the script.

ant-222 commented 4 years ago

@s3rvac:

So, when the notify-send [..] call fails, an error message is reported in the main WeeChat's buffer by writing it to stderr. If you do not see any errors there, it means that notify-send returned 0 in your case (success). What happens if you run the following commands from a shell?

$ notify-send test me
$ echo $?

Does it print 0 or another number? If it prints 0, then I am afraid that there is nothing to do as return code 0 means that everything went OK. If you know a way (apart from rewriting the notification script to C, which I will not do), feel free to re-open and I can incorporate that into the script.

The first command has no visible effect, whereas the second one prints zero even as you suggested. I think I will to contact the maintainers of notify-send about this one. Sorry for bothering you about it.

s3rvac commented 4 years ago

No worries. Maybe some other people will stumble upon this issue in the future, so it is good that we have documented it here.

ant-222 commented 4 years ago

Just for the record—it seems to be a bug in notify-send, which is part of libnotify. They not accepting issues for some reason, I have createad a pull request with a proposed fix. I wonder if they react to extermal PRs...